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
[snippet slug=wpmem_do_shortcode lang=php]
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”:
[snippet slug=wpmem_field-shortcode-using-wpmem_do_shortcode lang=php]
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.
[snippet slug=wpmem_form-with-redirect_to-attribute-using-wpmem_do_shortcode lang=php]
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.
[snippet slug=wpmem_logged_in-shortcode-using-wpmem_do_shortcode lang=php]
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.