benhuson / wp-subtitle

Development of the WordPress WP Subtitle plugin. http://wordpress.org/plugins/wp-subtitle/
33 stars 16 forks source link

How to call wp subtitle function as og:title to Facebook Open Graph Meta Data in function.php #28

Closed kabeerTHanath closed 5 years ago

kabeerTHanath commented 7 years ago

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.

I use code shown below (in function.php) as Facebook Open Graph Meta Data to achieve My goal. 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.org/schema/" xmlns:fb="http://www.facebook.com/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.com/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 );
benhuson commented 7 years ago

Try:

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