Support » Plugin: Facebook » Customize Open Graph protocol for your site or network

  • Plugin Author Facebook

    (@facebook)


    WordPress filters allow themes or plugins to customize the functionality of WordPress software including activated themes or plugins. The Facebook plugin for WordPress exposes filter interfaces throughout its code for customization by other plugins including plugins specific to your site.

    Customizing Open Graph protocol output is a common request from site owners. The Facebook plugin for WordPress builds Open Graph protocol outputs targeting the configuration of most sites. It is possible to customize Open Graph protocol properties before output to the page through the fb_meta_tags filter.

    Example: add a fallback image for all pages on your site or network.

    /**
     * Add site-specific Open Graph protocol customizations to the Facebook plugin
     *
     * @param array $meta_tags an associative array of Open Graph protocol properties with keys expressed as full IRIs
     * @return array plugin default values with site-specific customizations
     */
    function my_ogp_filter( $meta_tags ) {
    	$meta_tags['http://ogp.me/ns#image'][] = array(
    		'url' => 'http://s.wordpress.org/about/images/logos/wordpress-logo-notext-rgb.png',
    		'type' => 'image/png',
    		'width' => 500,
    		'height' => 500
    	);
    	return $meta_tags;
    }
    add_filter( 'fb_meta_tags', 'my_ogp_filter' );

    The above example asks WordPress to pass requests for modifications to the Open Graph properties (identified by the fb_meta_tags filter) through the my_ogp_filter function. Your function modifies the passed-in variable and returns the modification. The example function above adds the WordPress logo as a featured image on every page. If a post does not have its own featured image the WordPress logo would appear in a shared story summary on Facebook.com. Posts with a featured image would offer the WordPress logo as an alternate choice inside a story preview.

    You might place this custom function inside your theme’s functions.php file or as a separate PHP file in your site’s must use plugins directory (e.g. /wp-content/mu-plugins/facebook-custom.php).

Viewing 3 replies - 16 through 18 (of 18 total)
  • CarolWP

    (@carolwp)

    Okay, I answered my own question. Here’s the code:

    function change_fb_meta_type($metatags) {
    if (get_post_type($post) == 'clg_library') {
    $metatags['http://ogp.me/ns#title'] = 'Employee Benefits Library: '. get_the_title();
    }
    return $metatags;
    }
    add_filter('fb_meta_tags', 'change_fb_meta_type');

    Hi, is it possible to

    ‘url’ => ‘http://s.wordpress.org/about/images/logos/wordpress-logo-notext-rgb.png’,

    this content instead be a fixed url be a php variable like:

    ‘url’ => ‘$url’,

    ?

    Plugin Author Niall Kennedy

    (@niallkennedy)

    filimm: in PHP you should pass the $url variable as the value of the associative array. The Facebook plugin for WordPress will output the string as your page is generated.

Viewing 3 replies - 16 through 18 (of 18 total)
  • The topic ‘Customize Open Graph protocol for your site or network’ is closed to new replies.