-1

Well I should qualify that title. The shortcode is fine in itself, and no doubt nothing to do with the guys at CF7 causing this issue. I am sure it's me, but it just isn't working after I've added another shortcode directly after it. Here's the story:

I have a page which should appear with some text to non logged-in users, and then a submission form appears (on the same page) when the user is logged in. I used some code for this in my functions.php which looks like this:

add_shortcode( 'visitor', 'visitor_check_shortcode' );

function visitor_check_shortcode( $atts, $content = null ) {
     if ( ( !is_user_logged_in() && !is_null( $content ) ) || is_feed() )
        return $content;
    return '';
}

add_shortcode( 'member', 'member_check_shortcode' );

function member_check_shortcode( $atts, $content = null ) {
     if ( is_user_logged_in() && !is_null( $content ) && !is_feed() )
        return $content;
    return '';
}`

and then I have wrapped the appropriate text on the page with the shortcodes as follows:

[visitor]
the text that appears to non logged-in users
[/visitor]

[member]
the text that appears to logged-in users and this form:
[contact-form-7 id="26" title="Submit your work"]
[/member]

The problem is that with the [/member] shortcode in place, it disables the contact form 7 shortcode. So that it appears on the page exactly as it does above (i.e. a string of shortcode), and doesn't display the form. If I remove the [/member] shortcode, the contact form works again. But I need the [/member] shortcode! Any ideas what I've done wrong?

  • Don't forget to close your question if answered. – jdm2112 Jun 4 '15 at 14:46
1

From the codex:

The shortcode parser uses a single pass on the post content. This means that if the $content parameter of a shortcode handler contains another shortcode, it won't be parsed. http://codex.wordpress.org/Shortcode_API

The codex also provides the solution to your issue, which is to use the funciton do_shortcode()

If the enclosing shortcode is intended to permit other shortcodes in its output, the handler function can call do_shortcode() recursively:

function caption_shortcode( $atts, $content = null ) {
   return '<span class="caption">' . do_shortcode($content) . '</span>';
}

So, in your case you would need to edit the members function to include the form shortcode in it. Probably something along these lines:

function member_check_shortcode( $atts, $content = null ) {
     if ( is_user_logged_in() && !is_null( $content ) && !is_feed() )
        return $content . do_shortcode([contact-form-7 id="26" title="Submit your work"]);
    return '';
}`

If you need to change the form ID or title on the fly, you could pass those parameters via the members shortcode.

0

The comment from gdaniel is right on the money. In order to handle nested shortcodes properly, you can wrap your $content with do_shortcode().

Your MEMBER handler would become:

function member_check_shortcode( $atts, $content = null ) {
 if ( is_user_logged_in() && !is_null( $content ) && !is_feed() )
    return do_shortcode($content);
return '';

}`

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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