Page 1 of 1

Nesting WordPress Shortcodes & Conditionals?

PostPosted: July 12th, 2011, 11:26 am
by fcandillo
I am creating a teaser / blocker for certain pages & posts.

So, I will show some teaser content... and if the viewer has 'Level 1' access, then they will be shown the rest of the content. ** those that don't have access to the [pay] Premium content are shown a "blocker" message.

I understand how to do this with Simple Conditionals. However, want to take it a step further.

We are converting from another membership plugin and we already have shortcode tags in our posts written like the following:

Teaser content...
[pay]
Premium content
[/pay]

So far, using the S2Member plugin... I have figured out to do the following

Teaser content...
[s2If current_user_can(access_s2member_level1)]
Premium content...
[/s2If]
[s2If !current_user_can(access_s2member_level1)]
[blockermessage]
[/s2If]

here is my sandbox page example using the Simple Conditionals: https://museuminsider.co.uk/sandbox/sit ... y-tenders/

I created a [blocker] shortcode by adding to my functions.php -- include shortcode.php.... and then my [blocker] message is pulled from the shortcode.php

So the Question is...
Is is possible to duplicate the same using Simple or Advanced PHP conditionals... So that basically the following occurs:

[pay] = [s2If current_user_can(access_s2member_level1)]
[/pay] = [/s2If] [s2If !current_user_can(access_s2member_level1)] [blockermessage] [/s2If]

Then I don't have to change anything in the thousands of posts we have. ;)

Re: Nesting WordPress Shortcodes & Conditionals?

PostPosted: July 12th, 2011, 4:17 pm
by fcandillo
For now... since the Simple Conditionals are working properly, i just used the "Search Regex" plugin to search and replace the [pay] shortcode to = [s2If current_user_can(access_s2member_level1)], etc.

but, i would still be interested in knowing how to shorten the shortcode / simple conditionals.
thanks.

Re: Nesting WordPress Shortcodes & Conditionals?

PostPosted: July 13th, 2011, 2:17 am
by Cristián Lávaque
If you know how to create new shortcodes, then you could try defining new ones using the PHP conditionals. WP Admin -> s2Member -> API / Scripting -> Using Advanced Conditionals

Re: Nesting WordPress Shortcodes & Conditionals?

PostPosted: July 13th, 2011, 7:23 am
by fcandillo
Yes, this is what I tried with the PHP conditionals. It looks correct. However, it isn't working...
Code: Select all
function pay1_tag() {
   return '<?php if (current_user_is("s2member_level1")){ ?>';
   }

   add_shortcode('pay1', 'pay1_tag');


function pay2_tag() {
   return '<?php } else if (current_user_is("s2member_level0")){ ?>
                   The blocker message...
               <?php } else { ?>
                  The blocker message...
               <?php } ?> ';
   }

   add_shortcode('pay2', 'pay2_tag');


I have the following written in the post:

Teaser content...
[pay1]
Pay Content.......
[pay2]

Re: Nesting WordPress Shortcodes & Conditionals?

PostPosted: July 13th, 2011, 4:13 pm
by Cristián Lávaque
I'm not sure you can have the if statement split into separate shortcodes, it doesn't look like that will work. Try searching Google for shortcode conditionals in WordPress and see if you find info on how to achieve it.

Re: Nesting WordPress Shortcodes & Conditionals?

PostPosted: July 15th, 2011, 7:11 pm
by fcandillo
i was able to combine the S2Member simple conditional shortcodes and my [blockermessage] shortcode using the "Embedder" plugin. worked very simple!

Re: Nesting WordPress Shortcodes & Conditionals?

PostPosted: July 16th, 2011, 12:27 am
by Cristián Lávaque
Great! Thanks for the update. :)