Tutorials

Customize our premium WordPress themes and stand out from the crowd.

How to Create WordPress Shortcodes

http://colorlabs.co/VgOM76

Shortcode in WordPress is a statements represented an instruction that will be executed within posts or pages. With shortcodes, user has the ability to publish dynamic content using macros, without the need for programming skills.

We will give you three examples on how to create shortcodes in WordPress.

1. Simple Shortcode

Create this function below in functions.php file.


Function myshortcode_func($atts){

Return "This is my shortcode";

}

add_shortcode("nameshortcode, "myshortcode_func");

To use this shortcode you may insert this shortcode below inside the content on your post or page.


[nameshortcode]

The shortcode will automatically executed when you view your post, it would show “This is my shortcode” text on your post.

Tutorial-Shortcodes-simple

2. Shortcode with Attributes

Create this function on your functions.php file.


Function myshortcode_func($atts){

Extract(

shortcode_atts(

array("mytext"=>"default text"),$atts

)

)

Return $mytext;

};

add_shortcode("nameshortcode", "myshortcode_func");

To use this shortcode you may insert this shortcode below inside the content on your post or page.


[nameshortcode mytext="This text will showing"]

The shortcode will automatically executed when you view your post, it would show “This text will showing” text on your post.

Tutorial-Shortcodes-with-attribute

if “mytext” is empty then it would show “default text” by default.

Tutorial-Shortcodes-with-attribute-default

3. Shortcode That Has a Content

Create this function on your functions.php file.

Function myshortcode_func($atts,$content=null){

Return do_shortcode($content);

}

add_shortcode("nameshortcode", "myshortcode_func");

To use this shortcode you may insert this shortcode below inside the content on your post or page.


[nameshortcode] My Content [/nameshortcode]

The shortcode will automatically executed when you view your post, it would show “My Content” text on your post.

Tutorial-Shortcodes-with-content

  • Rashid Iqbal

    Hello
    Please visit below mention site.
    https://www.eocean.

  • Mrunal

    Helpful Post.!

  • http://www.psdtowordpressexpert.com/ PSDtoWordPressExpert

    Nice post !!

  • http://nordicappeal.com/ Martin Bay

    Thanks – just what I needed.