[wp-trac] [WordPress Trac] #47616: Enhancement: doing_shortcode() function similar to doing_filter()
WordPress Trac
noreply at wordpress.org
Tue Jan 19 22:13:04 UTC 2021
#47616: Enhancement: doing_shortcode() function similar to doing_filter()
-------------------------------------------------+-----------------------
Reporter: keraweb | Owner: audrasjb
Type: enhancement | Status: accepted
Priority: normal | Milestone: 5.7
Component: Shortcodes | Version:
Severity: normal | Resolution:
Keywords: has-patch needs-docs needs-dev-note | Focuses:
-------------------------------------------------+-----------------------
Changes (by audrasjb):
* keywords: has-patch needs-refresh needs-testing => has-patch needs-docs
needs-dev-note
Comment:
The above patch refreshes the previous one against trunk and fixes few
docblock issues.
**Testing procedure for testing both `doing_shortcode` and
`current_shortcode` new functions**
In your theme, declare the following shortcode / callback function:
{{{#!php
<?php
function worg_get_testing_foo() {
if ( doing_shortcode( 'testing-bar' ) ) {
$return = '[' . current_shortcode() . '] is currently
used';
} elseif ( doing_shortcode( 'testing-foo' ) ) {
$return = '[' . current_shortcode() . '] is currently
used';
} else {
$return = 'Not called from a shortcode';
}
return $return;
}
function worg_testing_foo_shortcode( $atts ) {
return 'Shortcode? ' . worg_get_testing_foo();
}
add_shortcode( 'testing-foo', 'worg_testing_foo_shortcode' );
}}}
----
In the content of a post, I added the following shortcode: `[testing-
foo]`.
Which returns: `Shortcode? [testing-foo] is currently used`
----
In my theme single.php file, I added the following PHP snippet:
`echo 'Shortcode? ' . worg_get_testing_foo();`
Which returns: `Shortcode? Not called from a shortcode`
----
**Conclusion**
It really works fine! I can imagine plenty of implementation use cases for
this ♥️
Adding `needs-dev-note` workflow keyword.
--
Ticket URL: <https://core.trac.wordpress.org/ticket/47616#comment:6>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list