dustin caruso

dustin caruso

Front-End / JavaScript / UI / WordPress / Shopify Developer

Facebook Open Graph meta tags in WordPress

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

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.

get the top parent category in WordPress

Here’s a cool little scrap of code to get the name of the top parent category in WordPress. This is another one of those things i spent a good deal of time trying to find through a lot of web searching. I finally came across a post somewhere (can’t remember where anymore) which had the solution I was looking for. I’ve just changed some of the wording (for my own benefit) and have posted it here. This will get the top parent category name, no matter how many levels deep in the category heirarchy you might be. To get this up and running, just add the code below to your functions.php file…

<?php

function get_parent_category_name($cat) {
    $parentCategoryList = get_category_parents($cat, false, ',');
    $parentCategoryListArray = split(',', $parentCategoryList);
    $parentName = $parentCategoryListArray[0];
    $stuffToReplace = array(' ' => '-', '(' => '', ')' => '');
    $parent = strtolower(strtr($parentName,$stuffToReplace));

    return $parent;
}

?>

Then, put this bit in your template where you want to place the top parent category name…

<?php get_parent_category_name($cat); ?>

This function returns the category “slug”.
To return the actual category name, replace the last line in the block of code above with this…

return $parentName;

Have fun…

capitalize page titles in WordPress

(without using a Plugin)

While working on a custom WordPress theme for a client this morning, I came across a peculiar problem. The words I was using for custom tags and categories which (in this particular theme design) get displayed in the page’s <title> in breadcrumb-like fashion, were being displayed just as they were typed in – with all lower-case characters. After doing some Google searching, I discovered there was no documented easy solution to capitalizing these text values. I found that there were many Plugins available that do this, but I really felt like installing a Plugin just to get some words capitalized seemed like overkill. Eventually, by using some of the code I found in those plugins, I hand-rolled the simple solution I was looking for, and thought I’d share it with y’all here…

First, let me explain that I did not have an issue with getting some things capitalized, like category name, for instance. To do that i simply used the PHP string function, ucwords() inline like so…

<title>Site Name - <?php echo ucwords($category); ?></title>

The problem I was having was with the WordPress template tags: wp_title and the_title. You can’t use ucwords() directly on these because they’re not a string. So, the solution I came up with was to write a small PHP function at the top of my “header.php” file that takes the text values from wp_title and the_title, turns them into a string, and applies capitalization with the ucwords() function…

<?php
function captitle($title) {
$title = ucwords($title);
return $title;
}
add_filter('wp_title', 'captitle');
add_filter('the_title', 'captitle');
?>

enjoy.