How to create our own shortcode in wordpress

How to Create Our Own Shortcode in WordPress?

A shortcode is a small piece of code that works like a calling function in WordPress. We can create our own shortcode for any type of small section or group of sections. While creating a shortcode we just need to make sure that its shortcode name will be unique in our site otherwise it will through an error. WordPress provides add_shortcode hook to register the shortcode.


function shortcode_function_name (){
// Codes that you wants to display at the place of shortcode
add_shortcode('my_custom_shortcode_name', ‘shortcode_function_name');

If we developed a site using theme and there is a section that we can not design by the theme builder, in that case we will do that section in custom and convert that section into shortcode and simply place shortcode in our theme builder editor/shortcode elements.


This is a step form and to do so we had not found such plugin and the site is developed on the theme. So, I just make a step form in custom and create a shortcode for it. Just add bellow function in your function.php and create a template file in your theme folder and then replace the name ‘form-shortcode.php’.

function stepform_function() {
$output = '';
require_once( get_template_directory().'/form-shortcode.php');
$output = ob_get_contents();
return $output;
add_shortcode(‘step_form’, ‘stepform_function’); 
// Here step form is the name of shortcode and we will have to put it in our theme editor/shortcode elements having surrounded by middle bracket like [step_form]

If we want to display the form in our custom template then print the shortcode like:

<?php echo do_shortcode(‘[step_form]’);?>

One thought on “How to Create Our Own Shortcode in WordPress?

Comments are closed.