So I would like to replace the entire title of posts in WordPress bases on user behaviuor and role. Please note that I would like to replace the entire post title and not the prepend or append text to it.

Each post has a 'default' title, which is the title of the post, like "Make No.1245/12.02.2022 on model 2376/2021". The alternative title would be "Make No.*******/********.2022 on model *******/2021" basically replacing certain parts of the title with asterisks, essentially hiding it.

If the user is logged in AND has role 'subscriber' THEN he/she sees the default post title.

If the user is visitor (not logged in) OR is logged in but his role is 'pending' THEN he sees the alternative post title.

1 Answer 1


You can use the_title filter to modify the post_title prior to printing on the screen.

add_filter("the_title", function($title, $id)){
  $user = wp_get_current_user();
  if((!is_user_logged_in() || in_array("pending", $user->roles)) && "post" === get_post_type($id)){
    $title = "Custom title";
  return $title;
}, 10, 2);

source: https://developer.wordpress.org/reference/hooks/the_title/

  • Thank you for your reply, @Himad. There is a bracket missing after $user->roles). However it now reflects on all WordPress titles (post, pages, etc.). Is there a way yo apply to posts only? Thank you! Jun 5, 2022 at 18:21
  • I added another conditional to modify the title only on posts. Hope it helps!
    – Himad
    Jun 5, 2022 at 20:34
  • If my answer helped you, I'd appreciate it if you mark it as the best answer :)
    – Himad
    Jun 6, 2022 at 21:42
  • Thank you very much, Himad! Works perfectly! Jun 7, 2022 at 7:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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