Site icon Puneet Sharma – Freelance Web Developer

How to create a custom shortcode in WordPress?

Here is an example of how I created and implemented a simple shortcode, and added it to a page using divi-visual-builder (inside the text module).

Here I am assuming that you have a good experience of creating child themes and PHP language.


Included a PHP file in function.php of a child theme


Edit PHP file and paste the following code:

<?php

function custom_shortcode( $atts , $content = null ) {
  $output = '<h1>This is a shortcode text</h1>';
  return $output;
}

add_shortcode( 'custom_shortcode', 'custom_shortcode' );
?>

Call shortcode

Now call the short code from anywhere inside any page or post with following method.

PHP method

<?php echo do_shortcode('[custom_shortcode]'); ?>

or

Call in a text editor in a post, or page, using visual page builder

[custom_shortcode][/custom_shortcode]
Exit mobile version