For anyone who is having a similar issue. You’ll be able to solve the issue by using wp
, wp_head
, wp_footer
or template_redirect
action hooks.
In this case, template_redirect
action hook solved my issue.
add_action( 'template_redirect', 'redirect_visitor' );
function redirect_visitor() {
// Do your stuff
}