5 Helpful WordPress PHP Functions

In this post I decided to put together a small collection of some helpful WordPress functions. I've included functions that will, load jquery from Google instead of your own server, make the current category menu item active when you're in a post associated with that category. Remove Jetpack's OG tags, and add a thumbnail to your WP RSS feed. I also included a shortcode, that will add a permalink to the current page.

Great WordPress Functions

Suggested Reading: Add Permalink to Genesis Post Title

Switch out your jQuery with Google API's Automatically

This code will remove your current (local) jQuery inclusion, and make it load from Google instead. This is a great feature, because this simple PHP function will help decrease the load time of your website.

function modify_jquery() {
	if (!is_admin()) {
		// comment out the next two lines to load the local copy of jQuery
		wp_deregister_script('jquery');
		wp_register_script('jquery', 'http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js', false, '1.9.1');
		wp_enqueue_script('jquery');
	}
}
add_action('init', 'modify_jquery');

Make your Category Menu Active in Posts

As you can see in the menu at the top of this page, there's a category highlighted. That's the category this post is associated with. This PHP function is what makes it possible.


function sgr_show_current_cat_on_single($output) {
    global $post;
    if( is_single() ) {
        $categories = wp_get_post_categories($post->ID);
        foreach( $categories as $catid ) {
            $cat = get_category($catid);

    // Find cat-item-ID in the string
    if(preg_match('#cat-item-' . $cat->cat_ID . '#', $output)) {
	$output = str_replace('cat-item-'.$cat->cat_ID, 'cat-item-'.$cat->cat_ID . ' current-cat', $output);
    }
	}

    }
    return $output;
}

add_filter('wp_list_categories', 'sgr_show_current_cat_on_single');

Duplicate OG Tags With Jetpack

If you, like me and many others, have the Yoast SEO Plugin or any other OG tag plugin, you might have run into the duplicate title/description on Google+ etc if you're using Jetpack as well. This code will remove the Jetpack OG tags.


remove_action( 'wp_head', 'jetpack_og_tags' );

Add a Thumbnail to your RSS Feed

This code will add an image to your RSS Feed. You can change the size of the thumbnail on line 5.


function diw_post_thumbnail_feeds($content) {
    global $post;
    if(has_post_thumbnail($post->ID)) {
        $content = '<div style="text-align:center;">' . get_the_post_thumbnail($post->ID) . '</div>' . $content;
        set_post_thumbnail_size( 150, 150 );
	}
    return $content;
}
add_filter('the_excerpt_rss', 'diw_post_thumbnail_feeds');
add_filter('the_content_feed', 'diw_post_thumbnail_feeds');

Create a Permalink Shortcode

This will simply add a link to the current page when you write [link] in a post.


function permalink_func() {
    $link = get_permalink();

return "$link";
}
add_shortcode( 'link', 'permalink_func' )

Genesis: Add a Facebook Like-Button to your Front-page Posts

If you go to our homepage, you'll see that there are Facebook-like buttons on every post, to show how many people liked that post. That feature can be added with this function. Big thanks to Greg Rickaby!


add_action( 'genesis_after_post_content', 'child_social_media_icons', 5 );
/**
 * Social Media Icons
 *
 * @author Greg Rickaby
 * @since 1.0.0
 */
function child_social_media_icons() {
	if ( is_front_page() ) { ?>
	<div id="social-media-icons">
		<div class="facebook-button">
		<div class="fb-like" data-href="<?php the_permalink(); ?>" data-send="true" data-layout="button_count" data-width="100" data-show-faces="false"></div>
		</div>
	</div> <!-- end #social-media-icons -->

<?php } }

Helpful WordPress Links

I hope these WordPress functions have been helpful, if so, let me know in the comments.

Speak Your Mind

*