How to create a simple shortcode

When you write articles in WordPress on a daily basis, there are times when you repeat yourself and when you have to type something over and over again. It doesn’t matter if it’s a web site URL, a long sentence or maybe a piece of HTML code which you constantly write from scratch, you should consider writing a shortcode for it.

And with this simple function you don’t have to be a programmer or you won’t even hire one to make things simple for you. Follow the next few steps and make yourself a custom shortcode:

  1. Open function.php file in the Appearance -> Editor menu
  2. Place the following code at the end of the file:
  3. function wploop_shortURL() {
    return 'http://www.firstsiteguide.com';
    }
    add_shortcode('wploop', 'wploop_shortURL');
  4. Go to Text Editor and type [wploop] to get the URL instead of the shortcode

It wasn’t that hard, right? But if you want to make it even easier, check out how to add custom quicktag buttons to your Text Editor and make everything appear in a click of a button.

More advanced example: Add image shortcode

By placing the following code in the functions.php, you can create the shortcode for placing an image with custom width and height attributes:

  1. Go to functions.php file
  2. Paste the following code:
  3. function img_shortcode( $atts , $content = null ) {
    
    	extract( shortcode_atts(
    		array(
    			'width' => '',
    			'height' => '',
    		), $atts )
    	);
    return '';
    }
    add_shortcode( 'img', 'img_shortcode' );
    
    
  4. Open Text Editor:

Here you can use [img width=’x’ height=’y’] http://… [/img] shortcode where “x” and “y” are numbers which represent width and height in pixels.

You can easily add other image attributes if you need any; check out the list off all image attributes on W3 Schools.

START YOUR OWN BLOG

This guide is an introduction to mastering the art of blogging. It provides easy to follow steps to start, maintain, and grow your blog.

Read the guide

Leave a Reply

Your email address will not be published. Required fields are marked *