How to create shortcode in wordpress

It is very easy to create shortcode and use it in WordPress. All it needs is to create a php function. Let’s create a shortcode that will generate a list of categories.

1. Open the functions.php file in your theme, it is located in wp-content/themes/yourthemename/functions.php . If it doesn’t exist, create it yourself.

2. Put this function in the functions.php file. The WordPress function wp_list_categories() will generate a list of categories.

function getCategoryList()

3. With the above function ready to create a list of categories, we now need to turn it into a showcode so we can use this function in the WordPress Admin Panel. All it needs to turn the function into shortcode is to use the WordPress function add_shortcode(param1, param2) fucntion, param1 is the shortcode name and the second is the function name. Let’s add this line after the function we just created.

add_shortcode('mycatelist', 'getCategoryList');

4. Now that we have everything needed in the functions.php file, we can use it in the blog posts and pages. To use it, we just need to give the shortcode name in a square bracket in the post or page editor in WordPress Admin Panel, like this.


5. Done.

Search within Codexpedia

Custom Search

Search the entire web

Custom Search