WordPress function to check if the current post is a custom post type

July 19, 2013 at 3:34 pm

Introduced in version 3.0, custom post types allows you to hold and display many different types of content. Today’s recipe is super useful for those who frequently work with custom post types: a function to check if the current post is a custom post type.

Simply paste this code into your functions.php file:

function is_custom_post_type() {
	global $wp_query;
	$post_types = get_post_types(array('public'   => true,'_builtin' => false),'names','and');
	foreach ($post_types  as $post_type ) {
		if (get_post_type($post_type->ID) == get_post_type($wp_query->post->ID)) {
			return true;
		} else {
			return false;

Once done, you can use the function as shown below. Please note that the function can be used outside the loop:

if (is_custom_post_type()) {
    //Current post is a custom post type

Thanks to Jonas Ethomsen for the function!

WordPress SEO: Automatically remove short words from the URL

July 16, 2013 at 5:20 pm

By default, WordPress generate the slug (url) from your post title. It do not remove any words, even shorter ones which don’t add any value to your seo. Here’s a super useful tip to automatically remove short words from your urls.

Paste the code below into your functions.php file. Once you saved the file, WordPress will automatically remove short (less than 3 characters) words from the generated permalink.

add_filter('sanitize_title', 'remove_short_words');
function remove_short_words($slug) {
    if (!is_admin()) return $slug;
    $slug = explode('-', $slug);
    foreach ($slug as $k => $word) {
        if (strlen($word) < 3) {
    return implode('-', $slug);

Thanks to Kevin Chard for this awesome piece of code!

WordPress tip: check if a post has a gallery

July 11, 2013 at 4:38 pm

Sometimes it can be useful to quickly be able to check if a post has a gallery. Here is a handy function to do it.

Simply paste the function below into your functions.php file:

function hasgallery(){
    global $post;
    return (strpos($post->post_content,'[gallery') !== false);

Once done, you can use it to detect if a post has a gallery.

Thanks to WordPress Stackexchange for the cool tip!

WordPress tip: Check whether a plugin is active

July 9, 2013 at 10:05 pm

When developing a WordPress theme or plugin, it can be really useful to be able to know if a specific plugin is activated. Here is a simple way to check wheher a plugin is activated or not.

Just paste the code below in your theme file, wherever you’d like to check out if a plugin is activated or not. Update the code with the plugin directory and name on line 4.

   include_once( ABSPATH . 'wp-admin/includes/plugin.php' );

   if(is_plugin_active('plugin-directory/plugin-file.php')) {
      //plugin is activated

Thanks to Jean galea for this brillant tip!

WordPress tip: Force specific pages to be SSL secure

July 2, 2013 at 3:26 pm

If SSL is enabled on your webserver, you should definitely use it to protect your blog. Activating SSL on your specific pages on a WordPress blog is definitely easy: Just read on.

Just add the following snippet to the functions.php file of your WordPress theme and specify the post or page ID desired.

function wps_force_ssl( $force_ssl, $post_id = 0, $url = '' ) {
    if ( $post_id == 25 ) {
        return true
    return $force_ssl;
add_filter('force_ssl' , 'wps_force_ssl', 10, 3);

Thanks to Kevin Chard for this recipe!

How to crop uploaded images instead of scaling them

June 26, 2013 at 5:59 pm

Would you like to crop your thumbnails instead of scaling them? If yes, I have a very handy snippet for you today. Just read on and enjoy!

Just add the code below to your functions.php file:

// Standard Size Thumbnail
if(false === get_option("thumbnail_crop")) {
     add_option("thumbnail_crop", "1"); }
     else {
          update_option("thumbnail_crop", "1");

// Medium Size Thumbnail
if(false === get_option("medium_crop")) {
     add_option("medium_crop", "1"); }
     else {
          update_option("medium_crop", "1");

// Large Size Thumbnail
if(false === get_option("large_crop")) {
     add_option("large_crop", "1"); }
     else {
          update_option("large_crop", "1");

Thanks to wp-snippet.com for the tip!

WordPress shortcode to embed Google trends graphs

June 24, 2013 at 5:45 pm

Google trends is a service which allow you to track the popularity of specific keywords. Here’s the code to create a WordPress shortcode that will embed a Google trends graph of any comma separated query on your blog.

The first step is to create the shortcode. To do so, open your functions.php file and paste the code below in it:

function wps_trend($atts){
        extract( shortcode_atts( array(
                'w' => '500',           // width
                'h' => '330',           // height
                'q' => '',              // query
                'geo' => 'US',          // geolocation
        ), $atts ) );
        //format input
<script type="text/javascript" src="http://www.google.com/trends/embed.js?hl=en-US&q=<?php echo $q;?>&geo=<?php echo $geo;?>&cmpt=q&content=1&cid=TIMESERIES_GRAPH_0&export=5&w=<?php echo $w;?>&h=<?php echo $h;?>"></script>
return ob_get_clean();

Once you saved your functions.php file, you can now use the shortocode in your posts and pages. Here is an example of usage:

[trends h="450" w="500" q="wpsnipp,wordpress,+wordpress+theme,+wordpress+plugin,+wordpress+snippets" geo="US"]

Thanks to Kevin Chard for the shortcode!

How to setup different admin and theme languages on your WordPress blog

June 21, 2013 at 11:25 am

I like me, you’re blogging in a language which is not your mother tongue, you might like to have the dashboard in your language. Here is a quick tip to use different languages on your WordPress blog or website.

Simply set the desired locale (on line 6) then add the code to your functions.php file.

     // setup one language for admin and the other for theme
     // must be called before load_theme_textdomain()

     function set_my_locale($locale) {
          $locale = ( is_admin() ) ? "en_US" : "it_IT";
          setlocale(LC_ALL, $local );
          return $locale;
     add_filter( 'locale', 'set_my_locale' );

Thanks to wp-snippet for the cool tip!

How to use WordPress shortcodes in theme files

June 6, 2013 at 7:22 pm

WordPress shortcodes are very useful and easy to use. But unfortunately, you can only shortcodes in post/page editor, not theme files. But there’s a little trick to do it easily. Just read on!

To use a shortcode in a theme file, simply use the do_shortcode() function as shown below:

<?php echo do_shortcode("[your_shortcode]"); ?>

That’s it. Super simple, and really useful at times!

Automatically link Twitter usernames in WordPress

June 4, 2013 at 6:02 pm

Are you using Twitter a lot? Today’s recipe is a cool piece of code to automatically link Twitter usernames on your posts, pages, and comments.

Paste the code below into your functions.php file:

function twtreplace($content) {
	$twtreplace = preg_replace('/([^a-zA-Z0-9-_&])@([0-9a-zA-Z_]+)/',"$1<a href=\"http://twitter.com/$2\" target=\"_blank\" rel=\"nofollow\">@$2</a>",$content);
	return $twtreplace;

add_filter('the_content', 'twtreplace');   
add_filter('comment_text', 'twtreplace');

Once you saved the file all twitter usernames in posts and comments will automatically be linked to their Twitter profiles. Usernames have to be written under the form @username.

Thanks to ederwp for the tip!