Create shortcode in wordpress

Wordpress Tutorials, WrodPress Programmatically

Create shortcode in wordpress

Create shortcode in wordpress

Create shortcode in WordPress is easy and the most useful things in WordPress So that we need to know about how to create a shortcode in WordPress.

Shortcode is some type of code that will do work like some functionality needs everywhere then use a shortcode to reduce rewrite the code just like a function we can assume that shortcode is the one type of function is WordPress so we can call that everywhere in WordPress. And nowadays shortcode is used mostly in themes and plugin.

Let’s we start to create our custom shortcode in WordPress so before start don’t forget to take backup of you functions.php file first as you will put code in your functions.php file then sometimes if you made any syntax mistake or something else then WordPress will goes down in that time backup will restore WordPress if anything goes wrong.

We can create two types of shortcode in WordPress simple shortcode and parameterize shortcode same as like function.

  • Shortcode: For add shortcode in WordPress first we need to add shortcode function in a functions.php file then after we can use that in WordPress.
function first_shortcode_function( $atts ) {
    return "Hi this is my first shortcode from helpyouforever.";
add_shortcode( 'mysfirsthortcode', 'first_shortcode_function' );

Above code will add shortcode function in WordPress after that we need to call that shortcode in your pages and template where you want to use. For that, you need to place that shortcode name wrapped with square brackets like [mysfirsthortcode].

Below is the image it displays how to place shortcode in a page.

Create shortcode in wordpress 1

  • Parameterized Shortcode: For add parameterized shortcode in need to follow the same process above but the code will be changed something.
function first_shortcode_function( $atts ) {
    $a = shortcode_atts( array(
        'value1' => 'Default value 1',
        'value2' => 'Default value 2',
    ), $atts );

    return "Test = {$a['value1']}";
add_shortcode( 'mysfirsthortcode', 'first_shortcode_function' );

In the above code, you can see that we have defined two parameters in the shortcode and both the parameters have default value also and after then I have to return the value of the first parameter. That code will add that shortcode in WordPress after than you need to place shortcode in a page like this [mysfirsthortcode value1=”My first value” value2=” My second value”] To use parameterize shortcode as we already know if we will not pass that value in shortcode then it will display default value.

That is the best practice always to pass default value always so will never occur undefined error.

Below is the image it displays how to place shortcode in a page.


Create shortcode in wordpress 2

That is all about shortcode in WordPress and that is the simple example of shortcode and basics using shortcode we can make big functionality in our website to reuse code every at write only once. Shortcode is the best feature in WordPress and best alternative in of function.

17 thoughts on “Create shortcode in wordpress

  1. Can I simply just say what a relief to discover a person that genuinely knows what they’re discussing on the web.
    You definitely understand how to bring a problem to
    light and make it important. A lot more people should look at this and understand this side of your story.
    It’s surprising you’re not more popular since you most certainly have the gift.

  2. Hey! Do you know if they make any plugins to help with Search Engine Optimization? I’m trying to get my blog to rank for some targeted keywords but I’m not seeing very good gains. If you know of any please share. Many thanks!

  3. Hello there! This post could not be written any better! Reading this post reminds me of my good old room mate! He always kept talking about this. I will forward this page to him. Fairly certain he will have a good read. Thank you for sharing!

  4. Hi there Dear, What you ?have here absolutely have me excited up to the last sentence, and I gotta tell you I am not that typical man who read the entire post of blogs ’cause I most of the time got bored and tired of the trash content that is presented in the junkyard of the world wide web on a daily basis and I simply end up checking out the pics and maybe a headline, a paragraph and so on. But your headline and the first few rows were great and it right on the spot grabbed my attention. Commending you for a job well done in here Thanks, really.

  5. Hello there, just became alert to your blog through Google, and found that it’s really informative. I am gonna watch out for brussels. I’ll appreciate if you continue this in future. Many people will be benefited from your writing. Cheers!

Leave a Reply