• Resolved Jan B-Punkt

    (@janbpunkt)


    Hi there community,

    I want to create a little plugin to show specific posts only between 10pm and 6am as some kind of youth protection.

    I managed to edit the title of the posts with:

    function hidefsk16 ($title) {
    	$fsk = substr($title,0,7);
    	if ($fsk == "[FSK16]") {
    		$title = substr($title,8);
    		return "FSK16 - ab 22 Uhr freigeschaltet ".$title;
    	}
    	return $title;
    }
    add_filter('the_title','hidefsk16');

    but I also want to manipulate the content of the post to be something like “No text here. Come back later” instead of the original text.

    How can I do that with one filter? Is it possible to do that with only one filter?

    Thanks!

    PS: Maybe there already is a plugin which does exact this. Show me ??

Viewing 7 replies - 1 through 7 (of 7 total)
  • Moderator bcworkz

    (@bcworkz)

    You would need to use the ‘the_content’ filter for content. No way around that. Your filter callback only gets the actual content, not what post it belongs to. If this is intended to work in the main WP loop, you can get the current post from the global $post variable. From that you can get the title or whatever other post data you need.

    Thread Starter Jan B-Punkt

    (@janbpunkt)

    Thank you, bcworkz!

    Unfortunately I’m very new to coding with php and wordpress and I have no idea how I can manipulate the title and post and return it to the_content.

    Can you/someone else give me a little hint?

    Thanks!

    Here’s some pseudo code.

    function hidefsk16 ($content) {
            global $post
            $title = $post->title;
    	$fsk = substr($title,0,7);
    	if ($fsk == "[FSK16]") {
                    $title = "Not visible between 6am and 10pm";
    		return $title;
    	}
    	return $content;
    }
    add_filter('the_content','hidefsk16');

    Moderator bcworkz

    (@bcworkz)

    Pseudo code huh? ?? That’s actually quite close!

    On a quick read, I think you just need to alter how you address the title.
    $title = $post->post_title;

    If you still have problems after this change, let me know and I’ll take a closer look. I’m pretty sure you’ll be good with this one change though.

    Thread Starter Jan B-Punkt

    (@janbpunkt)

    Thanks again, bcworkz!

    When I use the following code, the title of the post stays, but the text of the post becomes: “Not visible between 6am and 10pm”

    May you have a closer look at it please ??
    What I want to do is to alter the title to something like “Youth protection” and the text of the post to something like “The original post is not visible between…. “.

    Thank you!

    function hidefsk16 ($content) {
            global $post;
            $title = $post->post_title;
    	$fsk = substr($title,0,7);
    	if ($fsk == "[FSK16]") {
                    $title = "Not visible between 6am and 10pm";
    		return $title;
    	}
    	return $content;
    }
    
    add_filter('the_content','hidefsk16');
    Moderator bcworkz

    (@bcworkz)

    OK, I think I see what’s wrong now. Your code is doing what it’s supposed to do. You still need your original code to manage the title. Title and content are separate distinct entities requiring separate distinct code to alter each.

    That said, I now see a way to handle both from ‘the_title’ filter. It’s a bit hacky and I haven’t verified that it’ll work. Personally I’d stick with the two hook solution. You should get the two hook solution working the way you want before exploring the one hook variant, the code is still applicable.

    Instead of returning the modified content in ‘the_content’, set $post->post_content to the modified value in ‘the_title’. Being a global, the value should persist until the content is output later inside the loop.

    Thread Starter Jan B-Punkt

    (@janbpunkt)

    Will try that later.
    Thanks a lot!

    Thread Starter Jan B-Punkt

    (@janbpunkt)

    Okay, I used the 2 filter/hook solution.
    Not the best way, but it works ??

    Thank you so much!

    //when should the warning show up?
    $FSKstart = 6;
    $FSKend = 22;
    
    //define message for hidden posts
    $FSKwarning = "<strong>Diese Beitrag ist aus Gr&uuml;nden des Jugendschutzes nur zwischen 22:00Uhr und 06:00Uhr vollst&auml;ndig zu sehen. <br>Komm bitte sp&auml;ter wieder.</strong>";
    
    //function to remove the [FSK] Tag from title
    function hidefsk16_title ($title) {
    	$fsk = substr($title,0,5);
    	if ($fsk == "[FSK]") {
    		$title = substr($title,6);
    		return $title;
    	}
    	return $title;
    }
    
    //function to replace article with warning message
    function hidefsk16_content ($content) {
    	global $post, $FSKstart, $FSKend, $FSKwarning;;
    	$timestamp = time();
    	$hour = date("H",$timestamp);
    	$title = $post->post_title;
    	$fsk = substr($title,0,5);
    	if ($fsk == "[FSK]" && ($hour >=$FSKstart && $hour<=($FSKend-1) )) {
    		//$post->post_title = substr($title,7);
    		return $FSKwarning;
    	}
    	return $content;
    }
    
    //register the filter hooks
    add_filter('the_title','hidefsk16_title');
    add_filter('the_content','hidefsk16_content');
Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Filter to manipulate the_title AND the_content at the same time’ is closed to new replies.