Description
This function runs a shortcode’s callback function directly without having to parse the full regex that do_shortcode() has to run. It can be used for any shortcode – not just WP-Members.
Parameters
$tag
(string) (required) The tag for the shortcode being run.
$atts
(array) (optional) Any attributes as an array.
$content
(string) (optional) Any nested content for the shortcode.
Usage
wpmem_do_shortcode( $tag, $atts, $content );
Examples
Here is an example where an attribute is included directly. The function arguments are the tag of the shortcode (in this case “wpmem_form”) and an array of the attributes. This example is the same as using [wpmem_form first_name] to display the field value in “first_name”:
echo wpmem_do_shortcode( 'wpmem_field', array( 'first_name' ) );
This is an example where there is an array key for the value. The example is the same as:
[wpmem_form login redirect_to="https://mysite.com/my-page/"]
Note that the order of arguments in the array is important. The “tag” needs to be the first attribute because the shortcode parser reads that in array key [0], so it must be first. If you set up your array values following the same order as you would for the shortcode you’re calling, it will come out right.
$atts = array( 'login', 'redirect_to' => 'https://mysite.com/my-page/' ); echo wpmem_do_shortcode( 'wpmem_form', $atts );
Here is an example of a shortcode with nested content. The example would be the same result as the following shortcode:
[wpmem_logged_in]This content only displays to a logged in user[/wpmem_logged_in]
If there were additional attributes for wpmem_logged_in then those would go in the array position. In this example, however, there are no attributes, so pass either null (as in the example) or an empty array.
$content = "This content only displays to a logged in user"; echo wpmem_do_shortcode( 'wpmem_logged_in', '', $content );
Notes
- This utility can be used for any shortcode – not just WP-Members shortcodes. The only requirement is that WP-Members is installed and active.
- This utility comes from J.D. Grimes. You can implement his solution directly (if needed in an instance that WP-Members is not being used).
Changelog
- Introduced in version 3.2.5
Source
wpmem_do_shortcode() is located in /includes/api/api-utilities.php.