1

I'm using a plugin that shows content only to specific user roles. This content is surrounded by a shortcode tag to make it invisible to other other user roles. Now I'd like to call other shortcodes inside that restricted content.

I understand that I need to set the plugins shortcodes up so that they use do_shortcode(), but there are so many files that I'm not sure exactly where to do this. All of the places I've tried have not made any change. Any tips on what to look for?

Here's the code block I found. I think this is what I need to edit. What do you think? Change this:

return stripslashes($message);

Change to:

return stripslashes(do_shortcode($message));

// Show the level based protected shortcode message
    function do_levelprotected_shortcode($atts, $content = null, $code = "") {

        global $M_options;

        // Set up the level shortcodes here
        $shortcodes = apply_filters('membership_level_shortcodes', array() );
        $notshortcodes = apply_filters('membership_not_level_shortcodes', array() );

        $code = strtolower( $code );

        if( substr( $code, 0, 4 ) !== "not-" ) {
            if(!empty($shortcodes)) {
                // search positive shortcodes first
                $id = array_search( $code, $shortcodes );
                if($id !== false) {
                    // we have found a level so we need to check if it has a custom protected message, otherwise we'll just output the default main on
                    $level = new M_Level( $id );
                    $message = $level->get_meta( 'level_protectedcontent' );
                    if(!empty($message)) {
                        return stripslashes($message);
                    }
                }
            }
        } else {
            if(!empty($notshortcodes)) {
                // search positive shortcodes first
                $id = array_search( $code, $notshortcodes );
                if($id !== false) {
                    // we have found a level so we need to check if it has a custom protected message, otherwise we'll just output the default main on
                    $level = new M_Level( $id );
                    $message = $level->get_meta( 'level_protectedcontent' );
                    if(!empty($message)) {
                        return stripslashes($message);
                    }
                }
            }
        }

EDITORS NOTE: I removed all the links to the (closed source) plugins as they're as relevant for the actual question as the plugins source code is accessible: zero.

4
  • Nope, it wasn't that easy unfortunately. Oct 24, 2013 at 0:09
  • This question appears to be off-topic because it is about closed source software.
    – kaiser
    Oct 24, 2013 at 3:47
  • Hm. Maybe the close vote was too early. Question should be edited and rephrased to "How to call shortcodes from inside a shortcodes callback" and all the specifics about the closed source plugin should get deleted. Then it's actually a quite interesting question.
    – kaiser
    Oct 24, 2013 at 3:54
  • @kaiser, thanks for making the question wording better. Oct 24, 2013 at 4:21

2 Answers 2

1

tl;dr

  1. Find where the shortcode is registered.
  2. Find the shortcode output function.
  3. Pray for a filter.
  4. Report as a bug.

Remember: All of the functions below are made up. The point of the first two steps is finding the right values to plug into the snippets.


Find where the shortcode is registered

Use a program like Notepad++ or Sublime Text 2 (my personal favorite) that can search multiple files at once. Then search for some variation of the following, where "member" is the shortcode you're using:

  • add_shortcode( 'member'
  • add_shortcode('member'
  • add_shortcode( "member"
  • add_shortcode("member"

Find the shortcode output function

One of those will likely lead you to a line of code that looks like this:

add_shortcode( 'member', 'member_shortcode' );

Then you'll need to find the "member_shortcode" function. Look near the add_shortcode function to see if you see it. If not, do a search for:

function member_shorcode(

Pray for a filter

And then this is the moment of truth. You're praying this whole time that the shortcode provides a filter for the shortcode output. That would let you do something like this:

function recursive_member_shortcode( $content ) {
    return do_shortcode( $content );
}
add_filter( 'member_shortcode_filter', 'recursive_member_shortcode', 9999 );

If there is no filter, your only option is to hack the plugin file itself which is bad news.

Report as a bug

To be honest, having said all of the above, this seems to be a bug to me, and I'd report it to WPMU. But given my experience with their support and code-quality, I wouldn't get my hopes up.

5
  • I took this to the WPMU support community first thing, and they basically said it was impossible unless I wanted to 'get my hands dirty' with the code. I have no problem with that, but why not just add the great functionality? Anyway, thanks for the tips. I'll go do this now and let you know! Oct 23, 2013 at 23:46
  • That's ridiculous. Sorry to hear that. It sounds like there's a valid use-case for nesting shortcodes, so for them to tell you to go get your hands dirty is sad. If you have to manually edit the plugin file, make sure to never update the plugin. If anything, you might consider forking their shortcode and making your own version so you can update the rest of the plugin at a later date.
    – mrwweb
    Oct 23, 2013 at 23:50
  • I totally agree! Maybe if I get it to work, I can just ask them to add it in the next plugin update so I don't have to worry with about making my own. I've updated the question with the code I found. It's more complex than I was expecting. Would you mind taking a look to let me know if it's worth my time? Oct 23, 2013 at 23:59
  • I would think the change you suggested would work. Make sure that's the right shortcode function. (And if this answer was correct, make sure to accept it.)
    – mrwweb
    Oct 24, 2013 at 3:42
  • still can't get it to work, so looking for other potential functions. The problem is there are about a million shortcode functions. This one seemed like it would be the one. Oct 24, 2013 at 5:15
1

Whatever you need to do with shortcodes: You can always filter them.

Core uses the following filter.

apply_filters( "shortcode_atts_{$shortcode}", $out, $pairs, $atts );

So, as long as the third argument is used when calling shortcode_atts(), it's easy as your filter callback can simply hook into

"shortcode_atts_{$third_Argument}"

If that's not the case (and as it's optional, most developers forget about it), you'll have to search for unique (as much as this is possible) identifiers in the callback arguments.

Then you can filter the output. Attaching other shortcodes and every other funk is no problem from there.

Note: I'm not sure how much this answer is on topic with the question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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