Create Shortcode in Plugin

1. Under folder plugins create a new folder with your plugin name, example “my-sample-plugin”

2. Create a php file in the new folder with the same name “my-sample-plugin.php”

3. Setup your class in the newly created file:
class wp_my_sample_plugin {
public function __construct() {

$wpSample = new wp_my_sample_plugin ();


4. At the end of your constructor add the code for the shortcode:


add_shortcode( 'mysample', array( $this, 'my_sample_shortcode_fn' ) );

//first parameter represents the shortcode name you want to assign

//second parameter represents the function name to call


5. Right after the constructor, create the function name “my_sample_shortcode_fn”


public function my_sample_shortcode_fn($atts) {
$msg = 'Welcome to my sample plugin shortcode';
$msg .= ','.$atts['name'];
return $msg;

//The above function will display a welcome message whenever the shortcode is called.

//$atts represents any attributes passed along in the shortcode

//To check if an attribute is set, all we need to do is call $attr[‘name_here’]

//Attributes help making a plugin customizable through shortcodes


6. And finally, in your html all you need to do is add the following shortcode where ever you would like to display the content:

echo do_shortcode('[mysample name="Rochelle"]');



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s