Advertisement
armin_san

WP-OGP with fb:page_id

Jul 7th, 2011
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.74 KB | None | 0 0
  1. <?php
  2. /*
  3. Plugin Name: WP-OGP
  4. Plugin URI: http://www.millerswebsite.co.uk/2011/01/23/wordpress-plugin-wp-ogp/
  5. Description: This is a plugin to add Open Graph Protocol Data to the metadata of your WordPress blog.
  6. Version: 1.0.5
  7. Author: David Miller
  8. Contributor: Joe Crawford
  9. Author URI: http://www.millerswebsite.co.uk
  10. License: GPL2
  11. */
  12. // http://opengraphprotocol.org/
  13.  
  14. /*  This program is free software; you can redistribute it and/or modify
  15.     it under the terms of the GNU General Public License, version 2, as
  16.     published by the Free Software Foundation.
  17.  
  18.     This program is distributed in the hope that it will be useful,
  19.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  20.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  21.     GNU General Public License for more details.
  22.  
  23.     You should have received a copy of the GNU General Public License
  24.     along with this program; if not, write to the Free Software
  25.     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  26. */
  27.  
  28.  
  29. /**
  30.  * These are initially blank, to have a like button, at least one of these must be set
  31.  */
  32.  
  33. $ogpt_settings = array(
  34.     'fb:admins' => '',
  35.     'fb:appid' => '',
  36.     'fb:page_id' => '',
  37. );
  38.  
  39. define('OGPT_DEFAULT_TYPE', 'website');
  40. define('OGPT_SETTINGS_KEY_FB_APPID', 'wpogp-fb:appid');
  41. define('OGPT_SETTINGS_KEY_FB_ADMINS', 'wpogp-fb:admins');
  42. define('OGPT_SETTINGS_KEY_FB_PAGEID', 'wpogp-fb:page_id');
  43.  
  44. $wpogp_keys = array(
  45.     OGPT_SETTINGS_KEY_FB_APPID => 'A Facebook Platform application ID that administers this site.',
  46.     OGPT_SETTINGS_KEY_FB_ADMINS => 'A comma-separated list of Facebook user IDs that administers this site. You can find your user id by visiting <a href="http://apps.facebook.com/what-is-my-user-id/" target="_blank">http://apps.facebook.com/what-is-my-user-id/</a>',
  47.     OGPT_SETTINGS_KEY_FB_PAGEID => 'Gain access to insights for your website by adding a fb:page_id tag to your root webpage, linking it to a Page.'
  48. );
  49.  
  50. add_theme_support( 'post-thumbnails' );
  51.  
  52. function get_the_post_thumbnail_src($img)
  53. {
  54.   return (preg_match('~\bsrc="([^"]++)"~', $img, $matches)) ? $matches[1] : '';
  55. }
  56.  
  57. function wpogp_plugin_menu() {
  58.     add_submenu_page('options-general.php', 'WP-OGP', 'WP-OGP', 'manage_options', 'WP-OGP', 'wpogp_plugin_options');
  59.  
  60. }
  61.  
  62. function wpogp_plugin_options() {
  63.  
  64.     global $wpogp_keys;
  65.     echo '<div class="wrap">';
  66.     echo '<form method="post" action="options.php">';
  67.     echo '<input type="hidden" name="action" value="update" />';
  68.     echo '<input type="hidden" name="page_options" value="'.implode(',',array_keys($wpogp_keys)).'" />';
  69.     echo wp_nonce_field('update-options');
  70.     echo '<p>To include the Facebook "like" code on your page, you must first include values for the below items. Your Facebook User ID is a number. You may specify multiple user IDs if you like.</p>';
  71.     echo '<table class="form-table">';
  72.     foreach ($wpogp_keys as $key => $desc) {
  73.         echo '<tr valign="top">';
  74.         echo '<th scope="row">';
  75.         echo array_pop(explode('-',$key));
  76.         echo '</th>';
  77.         echo '<td><input type="text" name="';
  78.         echo $key;
  79.         echo '" value="';
  80.         echo get_option($key);
  81.         echo '" size="30"/><br />';
  82.         echo $desc;
  83.         echo '</td>';
  84.         echo '</tr>';
  85.     }
  86.     echo '</table>';
  87.     echo '<p class="submit">';
  88.     echo '<input type="submit" class="button-primary" value="Save Changes" />';
  89.     echo '</p>';
  90.     echo '</form>';
  91.     echo '</div>';
  92. }
  93.  
  94.  
  95. function load_wpogp_settings() {
  96.     global $ogpt_settings;
  97.     $ogpt_settings['fb:appid']  = get_option(OGPT_SETTINGS_KEY_FB_APPID);
  98.     $ogpt_settings['fb:admins'] = get_option(OGPT_SETTINGS_KEY_FB_ADMINS);
  99.     $ogpt_settings['fb:page_id'] = get_option(OGPT_SETTINGS_KEY_FB_PAGEID);
  100. }
  101.  
  102. function wpogp_plugin_path() {
  103.     return get_option('siteurl') .'/wp-content/plugins/' . basename(dirname(__FILE__));
  104. }
  105.  
  106. function wpogp_image_url_default() {
  107.     // default image associated is in the plugin directory named "default.png"
  108.     return wpogp_plugin_path() . '/default.jpg';
  109. }
  110.  
  111. function wpogp_image_url() {
  112.     global $post;
  113.  
  114.     $image = get_the_post_thumbnail_src(get_the_post_thumbnail($post->ID));
  115.  
  116.     if ( empty($image) )
  117.         { return wpogp_image_url_default();}
  118.     else
  119.         { return $image; }
  120.  
  121. }
  122.  
  123. function wpogp_set_data() {
  124.     global $wp_query;
  125.     load_wpogp_settings();
  126.     $data = array();
  127.     if (is_home()) :
  128.         $data['og:title'] = get_bloginfo('name');
  129.         $data['og:type'] = OGPT_DEFAULT_TYPE;
  130.         $data['og:image'] = wpogp_image_url_default();
  131.         $data['image_src'] = wpogp_image_url_default();  
  132.         $data['og:url'] = get_bloginfo('url');
  133.         $data['og:site_name'] = get_bloginfo('name');
  134.     elseif (is_single() || is_page()):
  135.         $data['og:title'] = get_the_title();
  136.         $data['og:type'] = 'article';
  137.         $data['og:image'] = wpogp_image_url();
  138.         $data['image_src'] = wpogp_image_url();
  139.         $data['og:url'] = get_permalink();
  140.         $data['og:site_name'] = get_bloginfo('name');
  141.     else:
  142.         $data['og:title'] = get_bloginfo('name');
  143.         $data['og:type'] = 'article';
  144.         $data['og:image'] = wpogp_image_url();  
  145.         $data['image_src'] = wpogp_image_url();  
  146.         $data['og:url'] = get_bloginfo('url');
  147.         $data['og:site_name'] = get_bloginfo('name');
  148.     endif;
  149.    
  150.     global $ogpt_settings;
  151.    
  152.     foreach($ogpt_settings as $key => $value) {
  153.         if ($value!='') {
  154.             $data[$key] = $value;
  155.         }
  156.     }
  157.     return $data;
  158. }
  159.  
  160. function wpogp_add_head() {
  161.     $data = wpogp_set_data();
  162.     echo get_wpogp_headers($data);
  163. }
  164.  
  165. function get_wpogp_headers($data) {
  166.     if (!count($data)) {
  167.         return;
  168.     }
  169.     $out = array();
  170.     $out[] = "\n<!-- BEGIN: WP-OGP by http://www.millerswebsite.co.uk Version: 1.0.5  -->";
  171.     foreach ($data as $property => $content) {
  172.         if ($content != '') {
  173.             $out[] = get_wpogp_tag($property, $content);
  174.         } else {
  175.             $out[] = "<!--{$property} value was blank-->";
  176.         }
  177.     }
  178.     return implode("\n", $out);
  179. }
  180.  
  181. function get_wpogp_tag($property, $content) {
  182.     return "<meta property=\"{$property}\" content=\"".htmlentities($content, ENT_NOQUOTES, 'UTF-8')."\" />";
  183. }
  184. function wpogp_add_head_desc() {
  185.     $default_blog_desc = ''; // default description (setting overrides blog tagline)
  186.     $post_desc_length  = 75; // description length in # words for post/Page
  187.     $post_use_excerpt  = 1; // 0 (zero) to force content as description for post/Page
  188.     $custom_desc_key   = 'description'; // custom field key; if used, overrides excerpt/content
  189.  
  190.     global $cat, $cache_categories, $wp_query, $wp_version;
  191.     if(is_single() || is_page()) {
  192.         $post = $wp_query->post;
  193.         $post_custom = get_post_custom($post->ID);
  194.         $custom_desc_value = $post_custom["$custom_desc_key"][0];
  195.  
  196.         if($custom_desc_value) {
  197.             $text = $custom_desc_value;
  198.         } elseif($post_use_excerpt && !empty($post->post_excerpt)) {
  199.             $text = $post->post_excerpt;
  200.         } else {
  201.             $text = $post->post_content;
  202.         }
  203.         $text = str_replace(array("\r\n", "\r", "\n", "  "), " ", $text);
  204.         $text = str_replace(array("\""), "", $text);
  205.         $text = trim(strip_tags($text));
  206.         $text = explode(' ', $text);
  207.         if(count($text) > $post_desc_length) {
  208.             $l = $post_desc_length;
  209.             $ellipsis = '...';
  210.         } else {
  211.             $l = count($text);
  212.             $ellipsis = '';
  213.         }
  214.         $description = '';
  215.         for ($i=0; $i<$l; $i++)
  216.             $description .= $text[$i] . ' ';
  217.  
  218.         $description .= $ellipsis;
  219.     } elseif(is_category()) {
  220.         $category = $wp_query->get_queried_object();
  221.         $description = trim(strip_tags($category->category_description));
  222.     } else {
  223.         $description = (empty($default_blog_desc)) ? trim(strip_tags(get_bloginfo('description'))) : $default_blog_desc;
  224.     }
  225.  
  226.     if($description) {
  227.         echo "\n<meta property=\"og:description\" content=\"".htmlentities($description, ENT_NOQUOTES, 'UTF-8')."\" />\n";
  228.         echo "<!-- END: WP-OGP by http://www.millerswebsite.co.uk Version: 1.0.5 -->\n";
  229.     }
  230. }
  231.  
  232.  
  233. add_action('wp_head', 'wpogp_add_head');
  234. add_action('wp_head', 'wpogp_add_head_desc');
  235. add_action('admin_menu', 'wpogp_plugin_menu');
  236. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement