• Resolved jaeeun

    (@jaeeun)


    I’m trying this code in my page.php:

    <?php echo do_shortcode('[content_protector password="password"]');?>
    			<?php the_content();?>
    			<?php echo do_shortcode('[/content_protector]');?>

    but it doesn’t work. The page just displays [/content_protector]. If I look into the html, it outputs a div with the “content-protector-access-form” class, then the content, then the closing code as a string.

    https://wordpress.org/plugins/content-protector/

Viewing 9 replies - 1 through 9 (of 9 total)
  • Thread Starter jaeeun

    (@jaeeun)

    I did more research and now I’m using this code:

    <?php
           $password = get_post_custom_values('password');
    	$protected = the_content();
    	echo do_shortcode("[content_protector password=". "'".$password[0]."']". $protected. "[/content_protector]");
    ?>

    But this displays the content before the password is entered.

    Hi, jaeeun.

    Assuming you’re already in the Loop, this should work (note that in this snippet, the password is actually the string ‘password’ – you probably want to change that):

    <?php
    echo do_shortcode('[content_protector password="password"]
    		. get_the_content()
    		. '[/content_protector]');
    ?>

    Thread Starter jaeeun

    (@jaeeun)

    Thanks, tested and it works with content().

    Now I’m trying to apply to what I actually need to protect, which is an iframe that’s displayed through this code

    do_action( 'tribe_events_single_event_after_the_meta' )

    And this is my code

    <?php
    	$password = get_post_custom_values('password');
    
    	if(!empty($password)){
    	echo do_shortcode("[content_protector password=". "'".$password[0]."']". do_action( 'tribe_events_single_event_after_the_meta' ). "[/content_protector]");
    	} else {
    		do_action( 'tribe_events_single_event_after_the_meta' );
    	}
    ?>

    but again, it just shows the iframe anyway before entering the password…this might be beyond your plugin support, but I’d really appreciate if you could help…!

    Can you return the results of do_action( 'tribe_events_single_event_after_the_meta' ) so that it can be assigned into a variable?

    K. Tough is there any way to get Ajax working when doing the do_shortcode method? I am trying to run the plugin on a secondary content field (not the main content). When doing it with do_shortcode even within the loop it does not enqueue the ajax scripts. Because of server caching I need this to run via ajax.

    Hey jaeeun,

    Did you ever get this to work?

    I happened upon this post because I guess I’m trying to do the same thing as you with tribe.

    K Tough, I have a very similar issue… but I have a bunch of echo $print lines in between the shortcode and can’t get the ending shortcode working. How would your example work in this scenario?

    //      echo do_shortcode( '[content_protector password="xxxxxxx" cookie_expires="0" identifier="xxx"] Private Info  [/content_protector]' );
    echo do_shortcode( '[content_protector password="xxxxxxx" cookie_expires="0" identifier="xxx"]' );
    
          echo '<h2>' . $print->FirstName.' ' . $print->LastName.' </h2>';
          echo '<table class="profile center">';
          echo '<tr><td>Team </td><td>' . $print->Team.'</td></tr>';
          echo '<tr><td>First Name </td><td>' . $print->FirstName.'</td></tr>';
          echo '<tr><td>Nickname </td><td>' . $print->Nickname.'</td></tr>';
          echo '<tr><td>Middle Name </td><td>' . $print->MiddleName.'</td></tr>';
          echo '<tr><td>Last Name </td><td>' . $print->LastName.'</td></tr>';
    ...
          echo '[/content_protector]';
    //      echo do_shortcode( '[/content_protector]' );

    Easiest way would be to put your content into a variable, then run it through do_shortcode() (it requires the complete shortcode, including opening and closing tags). Something like this:

    $the_content = '<h2>' . $print->FirstName.' ' . $print->LastName.' </h2>';
    $the_content .= '<table class="profile center">';
    $the_content .= '<tr><td>Team </td><td>' . $print->Team.'</td></tr>';
    $the_content .= '<tr><td>First Name </td><td>' . $print->FirstName.'</td></tr>';
    $the_content .= '<tr><td>Nickname </td><td>' . $print->Nickname.'</td></tr>';
    $the_content .= '<tr><td>Middle Name </td><td>' . $print->MiddleName.'</td></tr>';
    $the_content .= '<tr><td>Last Name </td><td>' . $print->LastName.'</td></tr>';
    ...
    $the_content .= '/<table>';
    echo do_shortcode( '[content_protector password="xxxxxxx" cookie_expires="0" identifier="xxx"]' . $the_content . '[/content_protector]' );

    Awesome! Works like a charm. Was just looking at https://wordpress.org/support/topic/do_shortcode-in-theme-template?replies=9 trying to apply it to my code. Many thanks!! You rock!

Viewing 9 replies - 1 through 9 (of 9 total)

The topic ‘Shortcode in Template’ is closed to new replies.