Menü schliessen
Created: February 26th 2021
Last updated: February 18th 2022
Categories: Php,  Wordpress
Author: Tim Fürer

WordPress: Add your own Shortcode

Tags:  functions.php,  guide,  PHP,  wordpress

In this short guide, I'll show you how to make a shortcode for your WordPress.

Inside your functions.php, create a function like this:

function my_shortcode() {
	ob_start();
	// shortcode content
	return ob_get_clean();
}

And hook it like this:

add_shortcode( 'myshortcode', 'my_shortcode' );

How do I call my WordPress shortcode?

You can call your shortcode, inside the WordPress Text-Editor, like this:

[myshortcode]

Attributes

Add a variable to your shortcode function's parameters to receive the attributes with.

function my_shortcode($atts) {
	ob_start();
	// shortcode content
	return ob_get_clean();
}

Receive the attributes, using the shortcode_atts() function:

function my_shortcode($atts) {
	$atts = shortcode_atts([
		'test' => ''
	], $atts);
	ob_start();
	// shortcode content
	return ob_get_clean();
}

Each key in the array is an attribute you want to receive. The value they are set to is their fallback.