skip to Main Content

I want to hide a certain paragraph from the SINGLE POST in WordPress via PHP content filter.

I developed this filtering script, which works like a sunshine:

function hideDaStuff( $content ) {

$paragraphs = explode( '</p>', $content );

foreach ($paragraphs as $index => $paragraph) {
    if(6 == $index){
        $paragraphs[$index] = '<p>Hidden for you ma friend.</p>';
    }else{
        $paragraphs[$index] .= '</p>';
    }

}

return implode( '', $paragraphs );
}

add_filter( 'the_content', 'hideDaStuffForDaStranger' );
function hideDaStuffForDaStranger( $content ) {

if ( is_single() && ! is_user_logged_in() ) {
    $content = hideDaStuff( $content );
}

return $content;
}

The problem is that WP cache caches the page whatever happens. So if a logged in user visits the page, then the content will show for everybody, and viceversa.

How can I make this specific part cache-independent, while keeping an efficient cache?

Using latest WP version and WP cache.

Thank you.

2

Answers


  1. Actually no need of these filters. Use this format

    if(is_user_logged_in()){ ?>
        <p>oggedin data</p>
    <?php }
    
    Login or Signup to reply.
  2. Put only single condition

    function hideDaStuffForDaStranger( $content ) {
        if (! is_user_logged_in() ) {
            $content = hideDaStuff( $content );
        }
    
        return $content;
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search