The correct hook to use is template_redirect
which allows you to have the necessary info available to do checks while also early enough to actually redirect. As per the example on the codex page:
function my_page_template_redirect()
{
if( is_page( 'goodies' ) && ! is_user_logged_in() )
{
wp_redirect( home_url( '/signup/' ) );
exit();
}
}
add_action( 'template_redirect', 'my_page_template_redirect' );
Codex page here – template_redirect
Related Posts:
- How do I force wp_enqueue_scripts to load at the END of ?
- Add code into on a per page/post basis
- How to remove/hide action links cluttering under specific plugins’ names
- Change My Basket text in the header of WooCommerce Artificer theme
- How to send “Location” header on plugin form submit event?
- Meteor Slides as header only for homepage and Use featured image of each page as header for respective pages
- Strip HTML comments from plugins in header/footer
- send_headers don’t work on wordpress multisite
- adding meta data using plugin to top of head
- WordPress Redirect Not Working – AJAX Callback Response Not Picked Up
- Headers already sent when I try to add headers via a shortcode
- WordPress repository forbids me call wp-load.php in non-WordPress file
- Show an image in my header.php
- Headers Content-Security-Policy CSP Major Issue
- How to debug error message: Cannot modify header information
- Object method calling for global $wpdb in header.php
- What does making sure “that your theme is WordPress header and footer tag enabled” mean?
- Know which plugin is calling JS in wp_head()?
- My single.php page does not show the related data to the post which is clicked
- Enqueue stylesheet in plugin for wp-login.php
- Get ‘Headers already sent’ error for the plugin I am creating when I try to login
- Custom Login Page — wp_signon Headers Already Sent?
- Adding Plugin Assets to Header
- Content-Security-Policy Headers are there and showing the correct settings, but still getting a refused connection
- echo get_option in header template
- Cannot access WP admin because plugin returns 204
- force jQuery to load in the head
- Edit page header on a custom plugin
- How do i ‘deactivate’ a plugin only on a certain page template?
- how would I include some html in the header section of a wordpress plugin? I get an error
- How do I fill the Home Description?
- Hook for plugin to insert into entry-meta
- sliding/fading header plugin or approach suggestion
- How to make a slider hearder?
- Prevent WordPress from sending set-cookie http header
- How to change the width of certain paragraph or header blocks?
- Modifying server’s response to API endpoint
- How to fix shifting header after installing speed / cache plugins?
- Cannot modify header information – headers already sent during plugin activation
- Debug in WP backend
- How to overlay a webcomic plugin on top of a header image?
- Set Content-Type header in do_parse_request filter
- Primary Menu doesn’t show because of w3 cache
- Insert Plugin on a custom page
- Problems with installing and deleting plugins
- Change text “My Basket” in woocommerce artificer theme
- WordPress repository forbids me call wp-load.php in non-WordPress file
- How to laod wp_enqueue_style to another header i created my self
- Cannot modify header information – headers already sent by
- Footer option is missing in Customiser
- Disable plugin / plugin action via theme
- How to eliminate weird 404 errors in wp-admin?
- How would you require and automatically download dependent plugins?
- Custom Widget Creating PHP Exception When Placed In Its Own PHP File
- How to set value/term of a Woocommerce attribute text field in admin panel?
- How to add nofollow on all external links without plugin?
- How to limit WordPress pages during updates?
- ajax front-end increment views on click
- WordPress converting my code
- Plugin to list active plugins across networked sites
- How to make a custom field as an editor in wordpress?
- Gutenberg: How to refresh ServerSideRender with useSelect and AsyncModeProvider true?
- Adding custom code to a WordPress site
- get_post_type() and WP_QUERY issue
- Python with wordpress plugin
- How to find wordpress db schema changes when upgrading from 4.9 to 5.2
- WordPress Post Visibility Options for Frontend
- How do I go back to wordpress 3.5
- What are the Best Practises When Using AJAX in Plugin Development?
- Plugins in WP_Options but not Active
- Check if user is on backend page
- do_action in conditional
- Show WordPress Plugin Menu(Admin) To Editor
- How many MySQL queries per visit I should normally have?
- Want to modify a Plugin – Tweetily – Can I make it tweet a Custom Field instead of Post Title?
- Problem enabling the user to edit Markdown and displaying the HTML output (WMD Editor plugin for WordPress)
- How can I disable an attribute from a Gutenberg block included in a template via InnerBlocks?
- How to add gallery to post and make seperate album page with gallery link to post?
- Shortcode do not return the right data in post
- Display pagination in reservation Plugin and and Print table as pdf
- Undeletable column in Posts
- Content disappears when searching with Search & Filter plugin
- PHP message: WordPress database error Deadlock found when trying to get lock
- Field salespeople task management
- How to populate a Mailchimp newsletter with latest events from WordPress plugin EventOn?
- How to get author developer link on plugin page
- wp_schedule_event need to deactivate plugin changing recurrance
- Show all posts of all categories but excluding a category on custom blog page with pagination of my theme
- ajax request not returning the result
- adding image in the header of my dev widget
- passing ‘&’ in return function of add_filter
- Session alternative for plugins (due to caching)
- Is there a plugin that will override the “Error establishing a database connection” message? [closed]
- Plugin with custom domain
- woocommerce retriving category name as div class?
- Duplicating wordpress install issue
- Edit a page/post DB data?
- how to update WordPress plugins from external website with nodejs?
- this code is fiven by chat-gtp but nothing match even I key in the value of a cell. help!
- How do i fix this error?