Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

share|improve this question
    
Whomever came along and decided that "I did not do any research" and gave me a negative score without commenting is extremely unhelpful. –  dcolumbus Aug 11 '12 at 21:39
1  
I guess you could say shortcodes are for front end content ... Are you trying to use another plugin to generate content for your own plugin admin page? –  Damien Aug 11 '12 at 21:56
    
@dcolumbus I concur. I've seen a few spurious downvotes recently, certainly more than I remember seeing in the past. IMO this is an honest (and pretty awesome) question, so I'm currently taking a stab at an answer ;) –  TheDeadMedic Aug 11 '12 at 22:39
    
If you've created an admin page then you can specify the output - why do you need/want to use do_shortcode(). Can you provide an example? –  Stephen Harris Aug 11 '12 at 22:53
    
Specially I'm trying to utilize WooCommerce shortcodes. User management and account management pages. –  dcolumbus Aug 12 '12 at 19:35

2 Answers 2

up vote 5 down vote accepted

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().

share|improve this answer
1  
I hadn't even thought of this! Then again, I'd never used shortcodes in the admin (for whatever reason) & had an excuse to investigate ;) –  TheDeadMedic Aug 11 '12 at 23:15
    
+1 simple and well done explanation. –  kaiser Aug 12 '12 at 1:17
    
I have no idea why I haven't thought of this either... I'm going to give it a try and report back! –  dcolumbus Aug 12 '12 at 19:35
    
One downside to this is that it will break if the plugin author refactors the shortcode's callback function in the future. Changes to the shortcode itself will probably be backwards-compatible, but it's less likely that the plugin's internal function names will be. –  Ian Dunn Sep 15 '12 at 9:49

It seems the shortcode API is available in the admin, but its output will depend on the shortcode tag in question.

The built-in [caption] works as expected, whereas [embed] 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?

share|improve this answer
1  
Also, some tags will be expecting get_the_ID() to return a post ID. –  Stephen Harris Aug 11 '12 at 22:52

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.