Open Graph : how to provide author details ?

Dear all,

I’d like to add author profiles to the Open Graph API.

I guess this is the way to start : http://ogp.me/#type_profile

Currently, in my posts, there is only an array containing an ID.

https://developers.example.com/tools/debug/sharing/?q=https%3A%2F%2Fblog.defi-ecologique.com%2Fbanc-refuge%2F

Would you know how to provide a clear name to the OpenGraph ?

Is it part of SmarCrawl ?

Thank you for your help,

Greg

  • Huberson
    • Recruit

    Hello Greg

    If you want to include the post/page author/profile name, SmarCrawl automaticatically insert that in the page meta tag once you enable OpenGraph under 'Social' and 'Title&Meta > Post Types' options.

    [attachments are only viewable by logged-in members]

    In case you're referring to author Facebook profile, I'd suggest checking this article – How to Add Facebook Author Tag in WordPress

    If you want to add those using the profile tags as shown from the link you shared above, using a filter like this one might help:

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

    //Adds Open Graph Meta Info
    function insert_og_tags() {
    global $post;
    //if not a post or a page, abort
    if ( !is_singular())
    return;

    echo '<meta property="profile:first_name" content="[AUTHOR FIRST_NAME]" >';
    echo '<meta property="profile:last_name" content="[AUTHOR LAST_NAME]" >';
    echo '<meta property="profile:username" content="[USERNAME]" >';
    echo '<meta property="profile:gender" content="[GENDER]" >';
    echo "
    ";
    }
    add_action( 'wp_head', 'insert_og_tags', 5 );

    Note, you will need to test and change the variables between brackets([AUTHOR FIRST_NAME]) to their real values.

    Cheers,

    Huberson

    • Greg
      • HummingBird

      Dear Huberson,

      Thank you for your help.

      I already had a function to import meta data on the head.

      Here is its current version :

      function insert_fb_in_head() {
      global $post;
      $t_id = '';
      if( !empty( get_queried_object() ) && isset( get_queried_object()->term_id ) )
      $t_id = get_queried_object()->term_id;
      if( !empty( $t_id ) ){
      echo '<meta property="og:image" content="' . services_taxonomies_thumbnails_url( 'http' ) . 'featured/' . get_option( "taxonomy_$t_id" )['image']. '.png' . '"/>';
      }
      elseif ( !is_singular() || !has_post_thumbnail() ){
      if( is_singular( 'service' ) ){
      $src = services_thumbnails_url( 'http' ) . get_meta_box_fields( get_the_ID() )['icone']['value'];
      }
      else
      $src = get_theme_url( get_current_subdomain() , 'http' ) . 'images/defi-ecologique-facebook.png';
      echo '<meta property="og:image" content="' . $src . '"/>';
      }
      else{
      echo '<meta property="og:image" content="' . get_the_post_thumbnail_url() . '"/>';
      }
      echo '<meta property="fb:admins" content="1"/>';
      echo '<meta property="fb:app_id" content="1935451610077356"/>';

      if( !is_single() )
      echo '<meta property="og:type" content="website"/>';
      else
      echo '<meta property="og:type" content="article"/>';

      if( get_current_subdomain() == 'blog' && is_single() ){
      global $wpdb;
      $req = "SELECT facebook FROM " . prefix() . "personne WHERE user_id=" . $post->post_author;
      $f_url = $wpdb->get_var( $req );
      if( !empty( $f_url ) )
      echo "<meta property='article:author' content='$f_url' />";
      }

      }
      add_action( 'wp_head', 'insert_fb_in_head', 1 );

      As you can see, I went with WPBeginners idea of using the facebook profile URL.

      I wanted to test it, but as you can see this method conflicts with SmartCrawl, since they are two article:author fields :

      https://developers.example.com/tools/debug/sharing/?q=https%3A%2F%2Fblog.defi-ecologique.com%2Fbanc-refuge%2F

      How may I tweak this code so that on my blog subdomain and posts only, if the author has a facebook profile then I use it, otherwise or everywhere else use the native SmartCrawl option ?

      Thank you for your help.

      Greg

  • Kostas
    • CTO

    Hi Greg ,

    You can ‘remove’ the SmartCrawl “article:author” meta tag via this filter:

    // This filter removes the article:author.
    add_filter(
    'wds-model-user-full_name',
    function( $name ) {
    return;
    },
    15
    );

    Unfortunately when you do that the Schema will have a name of ‘null’ as well and that’s not accepted via Google etc. To overcome that we can filter the Schema output as well to add the name again even if the article:author tag was removed by running this filter:

    // This filter adds the name back into the schema when article:author is removed.
    add_filter(
    'wds-schema-post-data',
    function( $data ) {
    global $post;

    $first = get_the_author_meta( 'first_name', $post->post_author );
    $last = get_the_author_meta( 'last_name', $post->post_author );

    if ( ! empty( $first ) && ! empty( $last ) ) {
    $name = "{$first} {$last}";
    }

    if ( empty( $name ) ) {
    $name = get_the_author_meta( 'display_name', $post->post_author );
    }

    $data['author']['name'] = $name;

    return $data;
    },
    15
    );

    From reading your code I guess the best place would be to add these filters along with your if( !empty( $f_url ) ) that you print your custom article:author so you can hide the one generated by SmartCrawl.

    Note, these ( along with your code ) have to be transferred on a mu-plugin preferably to work properly.

    Tell me if you need further help.

    Regards,

    Konstantinos

    • Greg
      • HummingBird

      Dear Konstantinos Xenos ,

      Thank you for your help.

      I’ve added your code right below the code I shared above, in one of my custom plug-ins.

      It doesn’t work : https://developers.example.com/tools/debug/sharing/?q=https%3A%2F%2Fblog.defi-ecologique.com%2Fmarion-esnault%2F

      Maybe it is due to the fact that I didn’t “hook” this filter ? I’m not exactly sure how it works.

      If possible, I’d like this code to run only on the blog and when is_single().

      //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.example.com/2008/fbml"';
      }
      add_filter('language_attributes', 'add_opengraph_doctype');
      //Open Graph Meta Info
      function insert_fb_in_head() {
      global $post;
      $t_id = '';
      if( !empty( get_queried_object() ) && isset( get_queried_object()->term_id ) )
      $t_id = get_queried_object()->term_id;
      if( !empty( $t_id ) ){
      echo '<meta property="og:image" content="' . services_taxonomies_thumbnails_url( 'http' ) . 'featured/' . get_option( "taxonomy_$t_id" )['image']. '.png' . '"/>';
      }
      elseif ( !is_singular() || !has_post_thumbnail() ){
      if( is_singular( 'service' ) ){
      $src = services_thumbnails_url( 'http' ) . get_meta_box_fields( get_the_ID() )['icone']['value'];
      }
      else
      $src = get_theme_url( get_current_subdomain() , 'http' ) . 'images/defi-ecologique-facebook.png';
      echo '<meta property="og:image" content="' . $src . '"/>';
      }
      else{
      echo '<meta property="og:image" content="' . get_the_post_thumbnail_url() . '"/>';
      }
      echo '<meta property="fb:admins" content="1"/>';
      echo '<meta property="fb:app_id" content="1935451610077356"/>';

      if( !is_single() )
      echo '<meta property="og:type" content="website"/>';
      else
      echo '<meta property="og:type" content="article"/>';

      if( get_current_subdomain() == 'blog' && is_single() ){
      global $wpdb;
      $req = "SELECT facebook FROM " . prefix() . "personne WHERE user_id=" . $post->post_author;
      $f_url = $wpdb->get_var( $req );
      if( !empty( $f_url ) )
      echo "<meta property='article:author' content='$f_url' />";
      }

      }
      add_action( 'wp_head', 'insert_fb_in_head', 1 );
      // This filter removes the article:author.
      add_filter(
      'wds-model-user-full_name',
      function( $name ) {
      return;
      },
      15
      );
      // This filter adds the name back into the schema when article:author is removed.
      add_filter(
      'wds-schema-post-data',
      function( $data ) {
      global $post;
      global $wpdb;
      $req = $wpdb->prepare( "SELECT facebook FROM " . prefix() . "personne WHERE user_id=%d" , $post->post_author );
      $name = $wpdb->get_var( $req );

      if( empty( $name ) ){
      $first = get_the_author_meta( 'first_name', $post->post_author );
      $last = get_the_author_meta( 'last_name', $post->post_author );

      if ( ! empty( $first ) && ! empty( $last ) ) {
      $name = "{$first} {$last}";
      }

      if ( empty( $name ) ) {
      $name = get_the_author_meta( 'display_name', $post->post_author );
      }
      }

      $data['author']['name'] = $name;

      return $data;
      },
      15
      );

      //Hreflang
      function insert_hreflang_in_head() {
      echo '<link rel="alternate" href="' . get_site_array( get_current_subdomain() )['url'] . '" hreflang="fr" />';

      }
      add_action( 'wp_head', 'insert_hreflang_in_head', 1 );

      Thank you for your help.

      Regards,

      Greg

  • Kostas
    • CTO

    Hi Greg as I’ve mentioned in my last post I see that you’re printing your article:author on if( !empty( $f_url ) ) so the filters to remove SmartCrawls should be in there to only print yours basically.

    Using your 1st code snippet ( not your last reply one so take care ) it should most probably look like this:

    <?php

    function insert_fb_in_head() {
    global $post;

    $t_id = '';

    if ( ! empty( get_queried_object() ) && isset( get_queried_object()->term_id ) ) {
    $t_id = get_queried_object()->term_id;
    }
    if ( ! empty( $t_id ) ){
    echo '<meta property="og:image" content="' . services_taxonomies_thumbnails_url( 'http' ) . 'featured/' . get_option( "taxonomy_$t_id" )['image']. '.png' . '"/>';
    } elseif ( ! is_singular() || ! has_post_thumbnail() ) {
    if ( is_singular( 'service' ) ) {
    $src = services_thumbnails_url( 'http' ) . get_meta_box_fields( get_the_ID() )['icone']['value'];
    } else {
    $src = get_theme_url( get_current_subdomain() , 'http' ) . 'images/defi-ecologique-facebook.png';
    }

    echo '<meta property="og:image" content="' . $src . '"/>';
    } else {
    echo '<meta property="og:image" content="' . get_the_post_thumbnail_url() . '"/>';
    }

    echo '<meta property="fb:admins" content="1"/>';
    echo '<meta property="fb:app_id" content="1935451610077356"/>';

    if ( !is_single() ) {
    echo '<meta property="og:type" content="website"/>';
    } else {
    echo '<meta property="og:type" content="article"/>';
    }

    if ( get_current_subdomain() == 'blog' && is_single() ) {
    global $wpdb;
    $req = "SELECT facebook FROM " . prefix() . "personne WHERE user_id=" . $post->post_author;
    $f_url = $wpdb->get_var( $req );
    if ( ! empty( $f_url ) ) {

    // This filter removes the article:author.
    add_filter(
    'wds-model-user-full_name',
    function( $name ) {
    return;
    },
    15
    );

    // This filter adds the name back into the schema when article:author is removed.
    add_filter(
    'wds-schema-post-data',
    function( $data ) {
    global $post;

    $first = get_the_author_meta( 'first_name', $post->post_author );
    $last = get_the_author_meta( 'last_name', $post->post_author );

    if ( ! empty( $first ) && ! empty( $last ) ) {
    $name = "{$first} {$last}";
    }

    if ( empty( $name ) ) {
    $name = get_the_author_meta( 'display_name', $post->post_author );
    }

    $data['author']['name'] = $name;

    return $data;
    },
    15
    );

    echo "<meta property='article:author' content='$f_url' />";
    }
    }

    }
    add_action( 'wp_head', 'insert_fb_in_head', 1 );

    Again note that the filters are added at the point where you print your custom author tag.

    Since I can’t replicate this on my end as I don’t know your setup exactly I’m just speculating by just viewing your code. You can again alter the filters to use any name you want as you did on your second reply with the custom wpdb query.

    I hope this clears things up a bit!

    Regards,

    Konstantinos

  • Kostas
    • CTO

    Hey Greg ,

    We included filters for Title, Description and Images on this release as they were mostly requested but we will include for all the other tags as well in an upcoming release. So for the time being the code that we’ve come up here is what should stay for the Author tags.

    To use or check the new filters though you can easily use them with these examples:

    add_filter(
    'wds_custom_og_title',
    function( $title ) {
    $title = 'A new title';

    return $title;
    },
    15
    );

    add_filter(
    'wds_custom_og_description',
    function( $description ) {
    $description = 'A new description';

    return $description;
    },
    15
    );

    add_filter(
    'wds_custom_og_image',
    function( $images ) {
    $images[0] = 'https://my-domain.com/some-new-image.jpg';

    return $images;
    },
    15
    );

    Regards,

    Konstantinos