Overriding Open Graph Content in Wordpress

in utopian-io •  7 years ago  (edited)

Any web solution that needs to be properly fit into today's social sharing content has to implement OG tags - which stands for Open Graph tags.

Og_tags.png
Image Credit: Own Creation


What is Open Graph

Open Graph is a protocol that allows the inclusion of special meta data into the header of the page, so as when shared on social media channels, such as facebook, twitter,... its different components would be properly identified, but also plays a role in SEO optimization.
Those components include, for example, the title, description, url, site name, key image,... and they would come in the shape of meta data, in format similar to the below, and normally preceded by the "og" identifier

<meta property="og:type" content="website" />
<meta property="og:title" content="website title" />
<meta property="og:description" content="website description" />
<meta property="og:url" content="http://www.mysiteurl.com" />

This standard is widely accepted, so providing the proper og content is a sure way to have your site display properly when shared on social media. Specific social media, such as twitter, can also provide particular additional tags for its use, which they call the open card protocol, in a format preceded by twitter, so those would be twitter:title, twitter:description,...


How to add OG tags to a wordpress installation?

In order to implement OG tags into a wordpress site, one can easily deploy a plugin for this purpose. And one of those essential plugins for such purposes, would be Yoast SEO plugin

While Yoast and other similar plugins allow to set general standard for values of this content from the administration interface, yet many times one needs to perform additional adjustments that just cannot be done from the interface, and would require coding, which is basically what we are doing in the code below.


Coding our way into adjusting OG tags

For example, let us try to modify the og:title content within the single post display, of a custom post type we have called "bank_entry", and add the post type into the title. The code (PHP since it's wordpress) would go as follows:

/* overriding og:title format for bank entry post types */
function gk_bank_entry_wpseo_og_title( $title ) {
    //only perform this action on single posts
    if ( is_singular() ) {
      //grab currently available post
      $post = get_queried_object();
      //check for match of the intended post type
      if (get_post_type($post) == 'bank_entry'){
        //append the post type to the title
        $title .= ' '.get_post_type($post);
      }
    }
    return $title;
}
add_filter( 'wpseo_opengraph_title', 'gk_bank_entry_wpseo_og_title' );

Let's take another example and make it more interesting, whereby we want to modify the description content, to make it dynamic based on another custom text field 'bank_entry_sharing_description' we had created within the custom post type. The code (again PHP-wordpress) would go as follows:

/* overriding og:description field with custom content from a custom field */
function gk_bank_entry_wpseo_opengraph_desc( $desc ) {
    //only perform this action on single posts
    if ( is_singular() ) {
      //grab currently available post
      $post = get_queried_object();
      //check for match of the intended post type
      if (get_post_type($post) == 'bank_entry'){
        //replace the description with the content of the custom field if the field has content, otherwise use the default value
        $overriding_desc = get_post_meta( $post->ID, 'bank_entry_sharing_description', true );
        if (!empty($overriding_desc) && $overriding_desc != ''){
          $desc = $overriding_desc;
        }
      }
    }
    return $desc;
}
add_filter( 'wpseo_opengraph_desc', 'gk_bank_entry_wpseo_og_desc' );

The full code can be found under my following gist:
https://gist.github.com/mcfarhat/64c3e0d4334e8ef58a76aa016f2d1c37#file-custom_og_content-php

Hope you found this useful !

@mcfarhat



Posted on Utopian.io - Rewarding Open Source Contributors

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  

Thanks for the info, have no idea my wife, says you are so nice so I thought to come see, but I know nothing of this coding. But promised and will follow to see maybe not that difficult must just do a bit of studying. By the way she is bigbear lol

oh thank you !
haha while reading your words i thought it must be bigbear, she told me she was going to tell you to check my blog, probably some of the recent health posts I was writing about.
So happy to meet you! she is a great person, and I'm sure so are you :)
You seem to have some great posts too, i just checked quickly the titles seem interesting, will keep an eye!
Thanks again :)

I knew about meta data when building a website, but "OG tags" are new to me. Thanks for the information!

#steemitbloggers

Glad to hear that was helpful ! :)

Following you @mcfarhat.
Upvoted and reesteemed your post.
I am following @mokh-tar. He mentioned you in his post. And according to him you are a nice selfless guy. So i thought of getting in touch with you

thank you @xabi for your nice words :)

welcome sir

Nice to meet you @mcfarhat
⚡Nice💔 and 💧also💲 informative👌 post

thank you !

thanks a lot

pleasure!

Thank you for the contribution. It has been approved.

You can contact us on Discord.
[utopian-moderator]

Thank you !

thanks for saring this informative nd educational article dear brother.; looking forward to see helpful posts from you. God bless you

Thank you Khaled ! :)

Great post :) @mcfarhat

Hey @mcfarhat I am @utopian-io. I have just super-voted you at 15% Power!

Suggestions https://utopian.io/rules

  • Utopian has detected 1 bot votes. I am the only bot you should love!!

Achievements

  • I am a bot...I love developers... <3
  • Much more informative than others in this category. Good job!
  • You have less than 500 followers. Just gave you a gift ;)
    Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x

Congratulations! This post has been upvoted from the communal account, @minnowsupport, by mcfarhat from the Minnow Support Project. It's a witness project run by aggroed, ausbitbank, teamsteem, theprophet0, someguy123, neoxian, followbtcnews/crimsonclad, and netuoso. The goal is to help Steemit grow by supporting Minnows and creating a social network. Please find us in the Peace, Abundance, and Liberty Network (PALnet) Discord Channel. It's a completely public and open space to all members of the Steemit community who voluntarily choose to be there.

I would like to join you
good topic