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
Example where an attribute is included directly:
// This will display the value of "first_name" for the current user. echo wpmem_do_shortcode( 'wpmem_field', array( 'first_name' ) );
Example of an attribute that needs an array key:
$atts = array( 'login', 'redirect_to' => 'https://mysite.com/my-page/' ); echo wpmem_do_shortcode( 'wpmem_form', $atts );
Example of a shortcode with content:
$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.