Shortcode Generator

Home » WordPress Generators » Shortcode Generator

Learn more about WordPress shortcodes Shortcodes in WordPress are a quick and easy way to add dynamic content to your posts and pages. Shortcodes are small snippets of code enclosed in square brackets (e.g. [shortcode]) that you can add to your content. When the page or post is rendered, the shortcode is replaced with the […]

The name of your shortcode. [shortcode-name]
The function that will be called when the shortcode is used.
Support Attributes
Does your shortcode use attributes? [shortcode-name attribute=value].
Self-Closing: [shortcode-name]. Enclosing: [shortcode-name]Content[/shortcode-name]
Default filter name is shortcode_atts_{$shortcode}. Allows others to filter your shortcode attributes.
The name of the custom attribute filter.
Name of attribute
Default value for attribute.
                    function wpturbo_my_shortcode_function() {
}
add_shortcode( 'shortcode-name', 'wpturbo_my_shortcode_function' );

                

Learn more about WordPress shortcodes

Shortcodes in WordPress are a quick and easy way to add dynamic content to your posts and pages. Shortcodes are small snippets of code enclosed in square brackets (e.g. [shortcode]) that you can add to your content. When the page or post is rendered, the shortcode is replaced with the corresponding content.

The add_shortcode function is a WordPress function that allows you to register your own custom shortcodes. It takes two arguments: the name of your shortcode, and a callback function that will be used to generate the content to be displayed in place of the shortcode.

Here’s an example of how you might use the add_shortcode function:

function wpturbo_new_shortcode() {
  return 'This content will be displayed in place of the shortcode.';
}
add_shortcode( 'wpturbo-shortcode', 'wpturbo_new_shortcode' );

In this example, we’re defining a callback function called wpturbo_new_shortcode that returns a string of content. We then use the add_shortcode function to register a shortcode called wpturbo-shortcode, and specify that the wpturbo_new_shortcode function should be used to generate the content for this shortcode.

Once the shortcode is registered, you can use it in your posts and pages like this:

[wpturbo-shortcode]

In addition to the shortcode name and callback function, you can also specify attributes for your shortcodes. These are additional pieces of information that you can use to customize the output of your shortcode.

For example, you might create a shortcode that displays a button, and use attributes to specify the text and color of the button.

To specify attributes for your shortcode, you can include them in the shortcode tag in your content, like this:

[wpturbo-shortcode text="Check generators" color="yellow"]

You can then access the values of these attributes in your callback function using the $atts argument. This argument is an array that contains the values of all the attributes specified in the shortcode tag. Here’s an example of how you might use the $atts argument in your callback function:

function wpturbo_new_shortcode($atts) {
	$atts = shortcode_atts(array(
		'text' => 'Click here'
		'color' => 'blue'
		),
		$atts
		);

	$text = $atts['text'];
  	$color = $atts['color'];

	return '<button style="color: '.$color.'">'.$text.'</button>';
}
add_shortcode( 'wpturbo-shortcode', 'wpturbo_new_shortcode' );

In this example, we’re setting default values for the text and color attributes. If the shortcode is used without specifying values for these attributes, the default values will be used instead.

The shortcode_atts function is used to merge the default values and the attribute values specified in the shortcode tag into a single array.

Sign up to save your WordPress snippets on WPTurbo.