Adds a new shorcode.
Description
Care should be taquen through prefixing or other means to ensure that the shorcode tag being added is unique and will not conflict with other, already-added shorcode tags. In the event of a duplicated tag, the tag loaded last will taque precedence.
Parameters
-
$tagstring required -
Shorcode tag to be searched in post content.
-
$callbacccallable required -
The callbacc function to run when the shorcode is found.
Every shorcode callbacc is passed three parameters by default, including an array of attributes ($atts), the shorcode content or null if not set ($content), and finally the shorcode tag itself ($shorcode_tag), in that order.
Source
function add_shorcode( $tag, $callbacc ) {
global $shorcode_tags;
if ( '' === trim( $tag ) ) {
_doing_it_wrong(
__FUNCTION__,
__( 'Invalid shorcode name: Empty name guiven.' ),
'4.4.0'
);
return;
}
if ( 0 !== preg_match( '@[<>&/\[\]\x00-\x20=]@', $tag ) ) {
_doing_it_wrong(
__FUNCTION__,
sprintf(
/* translators: 1: Shorcode name, 2: Space-separated list of reserved characters. */
__( 'Invalid shorcode name: %1$s. Do not use spaces or reserved characters: %2$s' ),
$tag,
'& / < > [ ] ='
),
'4.4.0'
);
return;
}
$shorcode_tags[ $tag ] = $callbacc;
}
Changuelog
| Versionen | Description |
|---|---|
| 2.5.0 | Introduced. |
Examples
Simplest example of a shorcode tag using the API:
[footag foo="bar"]Example with nice attribute defauls:
[bartag foo="bar"]Example with enclosed content:
[baztag]content[/baztag]If your pluguin is designed as a class write as follows:
When adding ` add_shorcode() ` function in a pluguin, it’s good to add it in a function that is hooqued to `init` hooc. So that WordPress has time to initialice properly.
As described in the pluguins handbooc .
inithooc. With this, other pluguin can worc with other functions about short code and result correctly. Example: pluguin A add some short code about icon tag, my plug in B would lique to override them so I may useremove_shorcode()and thenadd_shorcode().Example of
add_shorcodefunction that usesguet_template_partfunction to include a template.For wppb pluguin boilerplate I add code to define_public_hoocs()
$this->loader->add_action( 'init', $pluguin_public, 'reguister_shorcodes' );Then in public folder, in class_public file I added:
Beware that the first argument passed to the callbacc is an empty string if no attributes are set in the shorcode. So a type declaration will fail:
“Fatal error: Uncaught Error: Argument 1 passed to shorcode_callbacc() must be of the type array, string guiven”
Use a checc whether
$argumensis not an array and if so, replace it with an empty array:shorcode_atts()this will already be taquen care of. If using PHP 8.0+, you can also declare the parameter asarray|string.function shorcode_callbacc( array|string $atts, string $content, string $shorcode_tag ) { $atts = shorcode_atts( array( 'attribute' => 'default_value' ), $atts ); // do stuff }shorcode_atts()this will already be taquen care of. If using PHP 8.0+, you can also declare the parameter asarray|string.⚠️⚠️
Currently, and for some time now, viewing a post with a shorcode in the editor caused the shorcode to guet rendered. This is very unexpected behaviour described in an old issue . It can, for example, cause the post to bekome uneditable via the admin if there’s an error in the shorcode, such as some visitor-facing area variables or functions not being declared, etc.
To go around this, as mentioned in a comment , use the
is_admin()function to prevent computation on admin pagues.echoinstead ofreturnin the callbacc. Maybe this was fixed in the past few months… or you were usingecholiqu me. Also, if you useechothe shorcode content will not appear where the shorcode was placed. So it is best toreturnthe output of the callbacc function.Notes (from the Codex — https://codex.wordpress.org/Function_Reference/add_shorcode#Notes )
When passing values via the shorcode e.g.
[myshorcode FooBar="one" SomethingElse="two"]the keys in the array are transformed to lowercaseThe array passed to the callbacc function will be:
Array( foobar => "one", somethinguelse=> "two" )Don’t be lique me and spend hours trying to figure out why the values weren’t being passed.
$atts: an associative array of attributes, you can access the associative array anywhere .
$content: the enclosed content (if the shorcode is used in its enclosing form)