I’m using a few plugins that have shortcodes … however, instead of creating a public page for the content, I’ve created some new pages within the admin using add_menu_page and I need to know how to utilize do_shortcode() within this context.
As it stands, all the function does it spit out the string. I’m assuming it’s because the shortcode API isn’t available within an admin page.
How do I get around this? There is no documentation that I can find that explains how to utilize shortcodes within the WP Admin… or if it’s even possible.
Specifically I’m trying to utilize WooCommerce shortcodes within the WP Admin. I hate the fact that plugins don’t utilize the WP Backend for account/user management.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
Instead of calling do_shortcode() just call the function associated with the shortcode.
Example
There is a shortcode named [example] and a function registered as shortcode handler:
function example_shortcode( $atts = array(), $content = '' )
{
extract(
shortcode_atts(
array (
'before' => '',
'after' => '',
),
$atts
)
);
return $before . $content . $after;
}
add_shortcode( 'example', 'example_shortcode' );
In your admin page you just call the function:
echo example_shortcode(
array ( 'before' => 'This ', 'after' => '!' ),
'works'
);
Output: This works!.
Faster and more reliable than do_shortcode().
Method 2
It seems the shortcode API is available in the admin, but its output will depend on the shortcode tag in question.
The built-in works as expected, whereas doesn’t (this is due to how the embed API “lazy-loads” it’s shortcode, and depends on the_content filter to run, so technically not the shortcode API’s fault).
Conclusion: It’s entirely dependent on how & when the tag is registered, and what it does/assumes when executed.
@dcolumbus Which tag are we talking about in your case?
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0