WordPress shortcode – How to create shortcode in WordPress

WordPress shortcode – How to create shortcode in WordPress

WordPress provides good functionality WordPress shortcode. Using the shortcode we can easily add dynamic code into WP posts, pages and widgets. Today, We are going to learn How to create shortcode in WordPress. Also, We are going to see how to execute shortcode using WordPress do_shortcode.

First of all, We are going to create a shortcode. After that, I will tell you how to use the WordPress shortcode. However, Many themes and plugin provide the shortcode snippets functionality for Galler, HTML sections, Contact form, Blog listing etc…

WordPress shortcode

WordPress introduced the shortcode from 2.5 version. Shortcode functionality allows a developer to create dynamic code then register it as a shortcode. After that, User can easily access that functionality using a shortcode like [tryvary_shortcode].

WP find the shortcode and replace it with the actual function output. The shortcode is very useful functionality for the developer to add dynamic content in WP.

First of all, We are going to learn how to create a function and register it as a shortcode in WP.

How to create WordPress shortcode

First of all, We are going to create a WP shortcode. We are going to create a PHP function After that, We register it as a WP shortcode.

Just open the functions.php file from the currently active theme directory. Then just paste the below code.

function tryvary_shortcode_function( $atts, $content = "" ) {

    // Attributes
    $atts = shortcode_atts(
        array(
            'name' => 'Renish Khunt',
            'age' => '27',
        ),
        $atts
    );

    return $content." Hi there!, My Name is ".$atts['name']." and I am ".$atts['age']." years old.";

}

Now, Let’s register this function as a WP shortcode by following below line.

add_shortcode( 'tryvary_shortcode', 'tryvary_shortcode_function' );

Finally, Our shortcode is ready to use. Let’s see how we use the WordPress shortcode.

[tryvary_shortcode name="Renish Khunt" age="27"]
This is the first line content. Display it as a content in shortcode.
[/tryvary_shortcode]

Just add this code line into WordPress any posts, pages or widget then check WordPress convert it into output.

By Default, Shortcode doesn’t work directly in WP widget. You can enable shortcode in the widget by following this tutorial.

WordPress do_shortcode

We can execute the WP shortcode by PHP code using the WordPress do_shortcode function. Let’s see how we can execute the shortcode using PHP code.

print do_shortcode('[tryvary_shortcode name="Renish Khunt" age="27"]
This is the first line content. Display it as a content in shortcode.
[/tryvary_shortcode]');

For more detail about the use of do_shortcode function follow this tutorial.

In conclusion, Today, We learn about the create a WP shortcode creation and use it in WP. I hope you learn everything regarding the shortcode. If you have any question please write the comment. We are always ready to help you.

Related Posts

  1. How to Remove WooCommerce tabs from product page
  2. Add to cart variable products WooCommerce with variation button URL
  3. WooCommerce add multiple products to cart using URL
  4. WordPress do_shortcode in template PHP file
  5. MySQL value BETWEEN Two Columns in Laravel
  6. PHP tree structure for categories and sub categories without looping a MySQL query
  7. Email unique validation in Laravel ignore id on update
  8. WordPress Button Shortcode – Add button to WordPress editor
  9. WordPress change URL in database using MySQL Query
  10. WordPress jQuery is Not Defined or undefined

2 Comments

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Search Posts

Subscribe To Newsletter

Get Early Access To New Articles, Plugins, Discount Codes And Quickly Brief Updates about tips and tricks

Join 357 other subscribers

%d bloggers like this: