add_shortcode() WordPress Function
The add_shortcode() function allows you to add your own custom shortcodes to WordPress. A shortcode is a simple set of characters that you can use in your posts and pages to insert content that would otherwise be difficult or impossible to insert. For example, let's say you want to add a button to your posts that says "Click here to buy now". Normally, you would have to add HTML and CSS to your posts to create this button, but with a shortcode, you can simply add the following to your post: [button]Click here to buy now[/button] When this post is displayed, the button shortcode will be replaced with the actual button. Shortcodes can be used to insert images, videos, audio, and other media, as well as complex content like columns, tabs, and sliders. There are two parts to a shortcode: the opening tag (which consists of square brackets "[" and the shortcode name), and the closing tag (which consists of square brackets and a slash "/"). The content that you want to insert goes between the opening and closing tags. To create your own custom shortcodes, you can use the add_shortcode() function. This function takes two parameters: the shortcode name (which is what you'll use in your posts and pages), and a callback function (which is the function that will actually insert the content). Here's an example of how to use the add_shortcode() function to create a custom shortcode: function my_shortcode_callback() { // content to insert goes here } add_shortcode( 'myshortcode', 'my_shortcode_callback' ); In this example, we've created a shortcode called "myshortcode". When this shortcode is used in a post or page, the content from the my_shortcode_callback() function will be inserted. If you want to insert complex content or HTML, you'll need to use the return statement in your callback function. For example: function my_shortcode_callback() { return '
add_shortcode( string $tag, callable $callback ) #
Adds a new shortcode.
Description
Care should be taken through prefixing or other means to ensure that the shortcode tag being added is unique and will not conflict with other, already-added shortcode tags. In the event of a duplicated tag, the tag loaded last will take precedence.
Parameters
- $tag
(string)(Required)Shortcode tag to be searched in post content.
- $callback
(callable)(Required)The callback function to run when the shortcode is found. Every shortcode callback is passed three parameters by default, including an array of attributes (
$atts
), the shortcode content or null if not set ($content
), and finally the shortcode tag itself ($shortcode_tag
), in that order.
More Information
The shortcode callback will be passed three arguments: the shortcode attributes, the shortcode content (if any), and the name of the shortcode.
There can only be one hook for each shortcode. This means that if another plugin has a similar shortcode, it will override yours, or yours will override theirs depending on which order the plugins are included and/or ran.
Shortcode attribute names are always converted to lowercase before they are passed into the handler function. Values are untouched.
Note that the function called by the shortcode should never produce an output of any kind. Shortcode functions should return the text that is to be used to replace the shortcode. Producing the output directly will lead to unexpected results. This is similar to the way filter functions should behave, in that they should not produce unexpected side effects from the call since you cannot control when and where they are called from.
Source
File: wp-includes/shortcodes.php
function add_shortcode( $tag, $callback ) { global $shortcode_tags; if ( '' === trim( $tag ) ) { _doing_it_wrong( __FUNCTION__, __( 'Invalid shortcode name: Empty name given.' ), '4.4.0' ); return; } if ( 0 !== preg_match( '@[<>&/\[\]\x00-\x20=]@', $tag ) ) { _doing_it_wrong( __FUNCTION__, sprintf( /* translators: 1: Shortcode name, 2: Space-separated list of reserved characters. */ __( 'Invalid shortcode name: %1$s. Do not use spaces or reserved characters: %2$s' ), $tag, '& / < > [ ] =' ), '4.4.0' ); return; } $shortcode_tags[ $tag ] = $callback; }
Expand full source codeCollapse full source codeView on TracView on GitHub
Changelog
Version | Description |
---|---|
2.5.0 | Introduced. |