PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, May 15, 2022

[FIXED] Which hook do I use, to redirect user based on post/category?

 May 15, 2022     wordpress, wordpress-hook     No comments   

Issue

I'm new to WP development. I need to write a hook to check if the currently logged in user is viewing a post listed within a specific category, and then redirect user if they're lacking certain meta data.

I tried creating this function:

add_action('init','check_user_post_category');

however inside that function I was unable to get the post object (I have tried everything I found on the web!)

global $post; // This object is not valid at this time
global $wp; // $wp->request is empty
$_REQUEST; // This var is giving me an empty array! Is this normal??? :(

Could you kindly suggest, what hook is best to use in this case, and how to get the post object? Many thanks!


Solution

Use 'wp' hook instead of 'init'.

add_action('wp','check_user_post_category');

Maybe this would work for you.



Answered By - Ali Malik
Answer Checked By - Pedro (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

1,217,654

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © 2025 PHPFixing