最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

plugins - How to Add custom subtitle as og:title to Facebook Open Graph Meta Data in WordPress Themes?

programmeradmin0浏览0评论

I am using wp subtitle plugin for headings in my Local language, and default wordpress title as English headings. I need the plugin generated subtitle as title of shared article on facebook.

get_the_subtitle();

above code is the plugin specific function to call sub headings.

I use code shown below (in function.php) as Facebook Open Graph Meta Data to achieve My goal.

  echo '<meta property="og:title" content="' . get_the_subtitle() . '"/>'; 

but only I get default wordpress title as og:title content. Here is the code in function php.

//Adding the Open Graph in the Language Attributes
function add_opengraph_doctype( $output ) {
    return $output . ' xmlns:og="/" xmlns:fb=""';
}
add_filter('language_attributes', 'add_opengraph_doctype');

//Lets add Open Graph Meta Info

function insert_fb_in_head() {
    global $post;
    if ( !is_singular()) //if it is not a post or a page
        return;
    echo '<meta property="fb:admins" content="XXXXXXX"/>';
    echo '<meta property="og:title" content="' . get_the_subtitle() . '"/>';
    echo '<meta property="og:type" content="article"/>';
    echo '<meta property="og:url" content="' . get_permalink() . '"/>';
    echo '<meta property="og:site_name" content="KERALA NEWS"/>';
    if(!has_post_thumbnail( $post->ID )) { //the post does not have featured image, use a default image
        $default_image=".png"; //replace this with a default image on your server or an image in your media library
        echo '<meta property="og:image" content="' . $default_image . '"/>';
    }
    else{
        $thumbnail_src = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'medium' );
        echo '<meta property="og:image" content="' . esc_attr( $thumbnail_src[0] ) . '"/>';
    }
    echo "
    ";
}
add_action( 'wp_head', 'insert_fb_in_head', 5 );

I am using wp subtitle plugin for headings in my Local language, and default wordpress title as English headings. I need the plugin generated subtitle as title of shared article on facebook.

get_the_subtitle();

above code is the plugin specific function to call sub headings.

I use code shown below (in function.php) as Facebook Open Graph Meta Data to achieve My goal.

  echo '<meta property="og:title" content="' . get_the_subtitle() . '"/>'; 

but only I get default wordpress title as og:title content. Here is the code in function php.

//Adding the Open Graph in the Language Attributes
function add_opengraph_doctype( $output ) {
    return $output . ' xmlns:og="http://opengraphprotocol/schema/" xmlns:fb="http://www.facebook/2008/fbml"';
}
add_filter('language_attributes', 'add_opengraph_doctype');

//Lets add Open Graph Meta Info

function insert_fb_in_head() {
    global $post;
    if ( !is_singular()) //if it is not a post or a page
        return;
    echo '<meta property="fb:admins" content="XXXXXXX"/>';
    echo '<meta property="og:title" content="' . get_the_subtitle() . '"/>';
    echo '<meta property="og:type" content="article"/>';
    echo '<meta property="og:url" content="' . get_permalink() . '"/>';
    echo '<meta property="og:site_name" content="KERALA NEWS"/>';
    if(!has_post_thumbnail( $post->ID )) { //the post does not have featured image, use a default image
        $default_image="http://www.example/wp-content/themes/dw-focus-master/assets/img/logo.png"; //replace this with a default image on your server or an image in your media library
        echo '<meta property="og:image" content="' . $default_image . '"/>';
    }
    else{
        $thumbnail_src = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'medium' );
        echo '<meta property="og:image" content="' . esc_attr( $thumbnail_src[0] ) . '"/>';
    }
    echo "
    ";
}
add_action( 'wp_head', 'insert_fb_in_head', 5 );
Share Improve this question edited Jan 12, 2017 at 6:57 kabeer shefy asked Jan 12, 2017 at 5:52 kabeer shefykabeer shefy 114 bronze badges 2
  • there is no such thing as "do not work". Things always work but sometimes in unexpected ways.... Please edit the question and include what actually happens, error from the error log and any other relevant debugging information – Mark Kaplun Commented Jan 12, 2017 at 6:22
  • most likely that function can be called only at specific places. You should ask the plugin developer, or just debug the function itself – Mark Kaplun Commented Jan 12, 2017 at 8:26
Add a comment  | 

1 Answer 1

Reset to default 1

It could be that there are duplicate Open Graph meta tags being generated AFTER your special hook. Priority would be given to the later tags, which means your customizations don't happen.

Try setting the order of your hook later, so that it happens after whatever else might be generated:

// default is 10; I'm using 11 arbitrarily
add_action( 'wp_head', 'insert_fb_in_head', 11 ); 

More importantly, though, you need to confirm nothing else is generating OG. Check your plugins, and consider using on that easily lets you customize OG data as you need, like Complete Open Graph:

https://wordpress/plugins/complete-open-graph/

发布评论

评论列表(0)

  1. 暂无评论