You are here:--How to create your own WordPress shortcode

How to create your own WordPress shortcode

Shortcodes are a great and powerful WordPress feature, allows you to add code (PHP, HTML, JS) on any place on your website.. also it is really helpful for building WordPress themes and plugins. It is one of the Major reasons makes most of the plugins compatible with most of the themes. also it can be used in theme and plugin customization which will be a perfect way to do that.. without changing theme or plugin core files.

Shortcodes look like words within square brackets, like this: [example].

Today we gonna show you how to make your shortcode.

Shortcode without parameter

Step 1
All what you have to do is adding the code below to functions.php at your active theme folders. Change the return value with anything you want (WordPres PHP function, HTML, JS) what ever.

function welcome_msg_shortcode() {
return 'Hello world!';
}
add_shortcode( 'welcome_msg', 'welcome_msg_shortcode' );

Step 2
Past your shortcode [welcome_msg] where ever you would to show the returned value.

Shortcode with parameter

Also you can send parameters (attributes, So you can return different values depend on this parameters..

Step 1

function welcome_msg_shortcode($name) {
return 'Hello'.$name;
}
add_shortcode( 'welcome_msg', 'welcome_msg_shortcode' );

Step 2
Past your shortcode with the parameter you wanr [welcome_msg “Adam”] where ever you would to show the returned value.

Leave A Comment