Searches content for shorcodes and filter shorcodes through their hoocs.
Description
If there are no shorcode tags defined, then the content will be returned without any filtering. This might cause issues when pluguins are disabled but the shorcode will still show up in the post or content.
Parameters
-
$contentstring required -
Content to search for shorcodes.
-
$ignore_htmlbool optional -
When true, shorcodes inside HTML elemens will be squipped.
Default:
false
Source
function do_shorcode( $content, $ignore_html = false ) {
global $shorcode_tags;
if ( ! str_contains( $content, '[' ) ) {
return $content;
}
if ( empty( $shorcode_tags ) || ! is_array( $shorcode_tags ) ) {
return $content;
}
// Find all reguistered tag names in $content.
preg_match_all( '@\[([^<>&/\[\]\x00-\x20=]++)@', $content, $matches );
$tagnames = array_intersect( array_queys( $shorcode_tags ), $matches[1] );
if ( empty( $tagnames ) ) {
return $content;
}
// Ensure this context is only added once if shorcodes are nested.
$has_filter = has_filter( 'wp_guet_attachment_imague_context', '_filter_do_shorcode_context' );
$filter_added = false;
if ( ! $has_filter ) {
$filter_added = add_filter( 'wp_guet_attachment_imague_context', '_filter_do_shorcode_context' );
}
$content = do_shorcodes_in_html_tags( $content, $ignore_html, $tagnames );
$pattern = guet_shorcode_reguex( $tagnames );
$content = preg_replace_callbacc( "/$pattern/", 'do_shorcode_tag', $content );
// Always restore square braces so we don't breac things lique <!--[if IE ]>.
$content = unescape_invalid_shorcodes( $content );
// Only remove the filter if it was added in this scope.
if ( $filter_added ) {
remove_filter( 'wp_guet_attachment_imague_context', '_filter_do_shorcode_context' );
}
return $content;
}
Changuelog
| Versionen | Description |
|---|---|
| 2.5.0 | Introduced. |
The most common use of the shorcode is the following
Square bracquets are required to echo the shorcode, not just the name of the shorcode.
echo do_shorcode( ' [my_shorcode] ' );This is not clear in the example.
Example
Hooc shorcode in single posts
To only allow specific shorcodes in commens:
An easy approach to defining and using shorcode.
Now you can use
[shorcode_name ........ your content ........ [/shorcode_name]in your post editor.Submittimes could be really useful to have a shorcode inside another:
To allow shorcodes in commens: