WIDGET_HTML ); //=========================================================================== // // class StockTwits { private $stocktwits_op_name = 'stocktwits_options'; private $stocktwits_options; //------------------------------------------ public function __construct () { $this->LoadOptions(); } //------------------------------------------ //------------------------------------------ public function ContentFilter ($content) { if (is_feed()) return ($this->ContentFilter_stub ($content)); if (!preg_match ('|\[stocktwits([^\]]*)\]|i', $content, $matches)) return $content; $widget_title = 0; $username = 0; $limit = 0; if (isset($matches[1])) { $params = explode (',', $matches[1]); if (isset($params[0])) $widget_title = trim($params[0]); if (isset($params[1])) { $username = trim($params[1]); if ($username[0] == '$') $username[0] = '!'; // Replace $ with ! in ticker symbol to avoid conflicts with ticker_links plugin that replaces everything with $ABCD onto links. } if (isset($params[2])) $limit = trim($params[2]); } $widget_html = $this->GetWidgetHTML ($widget_title, $username, $limit); $content = preg_replace ('|\[stocktwits([^\]]*)\]|i', $widget_html, $content); return ($content); } //------------------------------------------ //------------------------------------------ // Stub just suppresses [stocktwits] tags. Useful for article excerpts of RSS feeds. public function ContentFilter_stub ($content) { $content = preg_replace ('|\[stocktwits([^\]]*)\]|i', '', $content); return ($content); } //------------------------------------------ public function PrintAdminPage () { $stocktwits_options = $this->LoadOptions(); if (isset($_POST['stocktwits_update_settings'])) { $stocktwits_options['widget_title'] = apply_filters ('content_save_pre', $_POST ['widget_title']); $stocktwits_options['username'] = apply_filters ('content_save_pre', $_POST ['username']); $stocktwits_options['limit'] = apply_filters ('content_save_pre', $_POST ['limit']); update_option($this->stocktwits_op_name, $stocktwits_options); $this->stocktwits_options = $stocktwits_options; // Reinitialize member var with fresh options. ?>

">

StockTwits

Using StockTwits widget

There are 3 simple ways to add the StockTwits widget to your Wordpress site. All options are customizable but you can only use 1 type of StockTwits widget on your site.

Simple Tag for Posts

The StockTwits widget can be inserted directly inside of any post and page by adding the simple tag [stocktwits] into the content body. Here are some other options you can use to customize the default:

  [stocktwits] - uses all default settings (from this admin panel)
  [stocktwits Stock Twits] - uses custom title Stock Twits and other settings at default
  [stocktwits Intel Pulse,$INTC] - Set title to Intel Pulse and only shows twits that mention Intel Corporation
  [stocktwits ,$AAPL] - Use default title and only show twits that mention Apple Corporation
  [stocktwits Intel Pulse,$INTC,5] - shows only 5 most recent twits
  [stocktwits Intel Pulse,$INTC,5,60] - refreshes once a minute (every 60 seconds)
  [stocktwits Market Watch,all] - shows twits from all StockTwits users
  [stocktwits ,,,5] - shows widget with all defaults but set to update rate every 5 seconds

Embed in a Theme

The StockTwits widget can be embedded directly into the theme files via any of the template function calls below.

  <?php stocktwits_widget(); ?> - all default settings
  <?php stocktwits_widget ("", "", 0, 10); ?> - use defaults for title, username/ticker and twits number, but set custom refresh
  <?php stocktwits_widget ("Intel twits", "$intc", 5, 60); ?> - use all custom settings

Sidebar Widget

Easily embed the StockTwits widget into your theme's sidebars via Appearance->Widgets administrative menu. Your theme must be widget-ready. Just add the widget to your sidebar.

StockTwits Widget Settings

Widget Title:
Enter a Username or $TICKER: username - for all user messages, $ticker - for all ticker messages
Number of messages to display: Number of messages to be displayed inside the widget. Use values from: 1 to 30
DEFAULT_WIDGET_TITLE, 'username' => DEFAULT_USERNAME, 'limit' => DEFAULT_LIMIT ); if (is_array($saved_options = get_option ($this->stocktwits_op_name))) $this->stocktwits_options = $saved_options; else { add_option ($this->stocktwits_op_name, $default_options); $this->stocktwits_options = $default_options; } } //------------------------------------------ //------------------------------------------ public function GetOptions () { return ($this->stocktwits_options); } //------------------------------------------ //------------------------------------------ public function GetWidgetHTML ($widget_title=0, $username=0, $limit=0) { $widget_html = WIDGET_HTML_TEMPLATE; $widget_html = preg_replace ('|__TITLE__|', $widget_title?$widget_title:$this->stocktwits_options['widget_title'], $widget_html); if ($this->stocktwits_options['username'][0] == '$') { $username = substr(trim($this->stocktwits_options['username']), 1); $widget_html = preg_replace ('|__SYMBOL__|', $username, $widget_html); $widget_html = preg_replace ('|__USERNAME__|', '', $widget_html); } else { $widget_html = preg_replace ('|__USERNAME__|', $username?$username:$this->stocktwits_options['username'], $widget_html); $widget_html = preg_replace ('|__SYMBOL__|', '', $widget_html); } $widget_html = preg_replace ('|__LIMIT__|', $limit?$limit:$this->stocktwits_options['limit'], $widget_html); return ($widget_html); } //------------------------------------------ } //=========================================================================== //= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = // Instantiate plugin object. global $g_StockTwits_Plugin; $g_StockTwits_Plugin = new StockTwits(); //Initialize the admin panel add_action ('admin_menu', 'StockTwits_AdminPanel'); add_action ('init', 'init_action' ); add_action ('plugins_loaded', 'RegisterStockTwitsWidget'); add_filter ('the_content', array(&$g_StockTwits_Plugin, 'ContentFilter'), 9); add_filter ('the_content_limit', array(&$g_StockTwits_Plugin, 'ContentFilter'), 9); add_filter ('the_excerpt', array(&$g_StockTwits_Plugin, 'ContentFilter_stub'), 8); add_filter ('the_excerpt_rss', array(&$g_StockTwits_Plugin, 'ContentFilter_stub'), 8); add_filter ('the_content_rss', array(&$g_StockTwits_Plugin, 'ContentFilter_stub'), 8); //= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = //=========================================================================== function RegisterStockTwitsWidget() { register_sidebar_widget(__('StockTwits Widget'), 'StockTwitsWidget'); } // Actual widget HTML emitting function. function StockTwitsWidget ($args) { global $g_StockTwits_Plugin; $widget_title = $g_StockTwits_Plugin->GetOptions(); $widget_title = $widget_title['widget_title']; extract($args); echo $before_widget; echo $before_title; echo $widget_title; echo $after_title; echo $g_StockTwits_Plugin->GetWidgetHTML (); echo $after_widget; } //=========================================================================== //=========================================================================== // Template function to display StockTwits.com widget // Could be embedded into index.php, single.php, etc... as: // Syntax: // stocktwits_widget(); // stocktwits_widget("Intel Inside", "$INTC"); // stocktwits_widget("Intel Inside", "$INTC", 20, 10); function stocktwits_widget ($widget_title=0, $username=0, $limit=0) { global $g_StockTwits_Plugin; echo $g_StockTwits_Plugin->GetWidgetHTML ($widget_title, $username, $limit); } //=========================================================================== //=========================================================================== function StockTwits_AdminPanel () { global $g_StockTwits_Plugin; add_options_page('StockTwits', 'StockTwits Plugin', 9, basename(__FILE__), array(&$g_StockTwits_Plugin, 'PrintAdminPage')); } //=========================================================================== //=========================================================================== function init_action () { wp_enqueue_script('st-widget', 'http://stocktwits.com/addon/widget/2/widget-loader.min.js'); } //=========================================================================== //=========================================================================== function enforce_values ($input, $min, $max) { if ($input < $min) return $min; if ($input > $max) return $max; return $input; } //=========================================================================== //=========================================================================== // // Returns no-slashed WEB URL of directory where this file is. function get_base_dir_url () { $base_dir_url = get_bloginfo ('wpurl') . preg_replace ('#^.*[/\\\\](.*?)[/\\\\].*?$#', "/wp-content/plugins/$1", __FILE__); return ($base_dir_url); } //=========================================================================== //=========================================================================== // log_event (__FILE__, __LINE__, "Message", "extra data"); if (!function_exists('log_event')) { function log_event ($filename, $linenum, $message, $extra_text="") { $log_filename = dirname(__FILE__) . '/__log.php'; $logfile_header = '"; // Delete too long logfiles. if (@file_exists ($log_filename) && @filesize($log_filename)>1000000) unlink ($log_filename); $filename = basename ($filename); if (file_exists ($log_filename)) { // 'r+' non destructive R/W mode. $fhandle = fopen ($log_filename, 'r+'); if ($fhandle) fseek ($fhandle, -strlen($logfile_tail), SEEK_END); } else { $fhandle = fopen ($log_filename, 'w'); if ($fhandle) fwrite ($fhandle, $logfile_header); } if ($fhandle) { fwrite ($fhandle, "\r\n// " . $_SERVER['REMOTE_ADDR'] . ' -> ' . date("Y-m-d, G:i:s.u") . "|$filename($linenum)|: " . $message . ($extra_text?"\r\n// Extra Data: $extra_text":"") . $logfile_tail); fclose ($fhandle); } } } //=========================================================================== ?>