I have made a shortcode, because we can't redirect page within shortcode using wp_redirect so I used 'init' hook to check the $_POST/GET parameter and make system redirect. But I'm stuck in a condition checking where I need to check current post id.

The problem is that until 'init' hook global post doesn't have any value. So I can't get current post/page id. Now how to handle these type of problem. In shortcode I can't redirect but can get global post. On 'init' I can make redirect but can't get current post id or global post variable value.

For now, I can use $_SERVER variable to get current URL and passing current url to url_to_postid to get current post id. But I don't think this is the proper and good way of doing this. It's like a patch and not full proof solution. I wonder what should be the standard and correct way for doing redirect with getting current post/page id?

  • 4
    Use the template_redirect hook – Pieter Goosen Jun 28 '15 at 13:35
  • Thanks Pieter, this is what I was looking for. Perfect hook. It worked. – Yogendra Jun 28 '15 at 13:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.