Image for post
Image for post

Facebook Open Graph meta tags in WordPress

How to get your page or post content in the description tag

dustin caruso
Mar 29, 2016 · 1 min read

Getting the Open Graph meta tags (that Facebook parses when sharing a webpage) to be populated with the proper content can sometimes be tricky when working with HTML code that is dynamically generated by WordPress.

Below is the quickest and simplest way I’ve come up with to do this. As always, I like to share my discoveries in hopes that it saves other people the time I had to spend Googling and testing my solutions.

In the “header.php” file of your theme, add the following code, just above your Open Graph meta tags…

<?php $d = strip_tags(get_post($post->ID)->post_content); ?>

* the “d” variable stands for description. ;-)
* the “strip_tags” bit ensures your description doesn’t include HTML tags.

Then output the description in your meta tag like so…

<meta property="og:decription" content="<?php echo $d; ?>">

Please, let me know in the comments if this little tip helped you at all.

Originally published at dustincaruso.com.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store