The proper hook to handle custom 404 content is 404_template. This happens when WordPress is trying to include the default template, but before any HTTP headers have been sent.
So you can change the template, send or overwrite HTTP headers, and all of that without any checks, because WordPress has done that for you already.
In most cases you should not redirect those requests. A redirect is a 3xx status, not a 4xx.
You might find some inspiration in my old 404 plugin.
Related Posts:
- Filter to change the content of 404 page
- Set a specific page as 404 – not found via my own plugin
- Custom rewrite rule serves content, but returns 404 error code
- Show custom data instead of 404
- If $var is empty, return 404.
- Form submission to another page returning 404 error [duplicate]
- Check current URL is 404 in pre_option_stylesheet filter hook
- Scripts/styles not loading on cloned WP Site when logged in
- What process do you use for WordPress development? [closed]
- How do I create a custom role capability?
- What is the advantage of using wp_mail?
- Where to put third party PHP library?
- Add a new tab to WordPress Plugin install Listing
- Export data as CSV in back end with proper HTTP headers
- How to integrate a PHP webmail script into the backend of WordPress?
- Calling the widget id of a mult-instance widget from inside the widget?
- Do I need to call do_action in my plugin?
- WooCommerce get Shipping Class of product from either the product id or the order after order is completed
- Dequeue script, but still use wp_localize_script to pass vars
- I can’t find where a hook is being defined in a plugin – Easy Digital Downloads
- Can I add pages to my custom menu via script?
- Replacing WordPress menu functionality with a plugin
- Programmatically Selecting Theme Based on URL
- Save user-specific options in WordPress admin
- Why is print_r returning $classObj->userObj in several places on site
- Is it possible to load plugin from console with core ?
- use __($str) to translate strings (symfony/twig)
- Plugin development: is adding empty index.php files necessary?
- Plugin base URL
- Database for development
- Customize multisite site creation with user data
- Adding plugin settings link upon activation
- How to create multiple Gutenberg blocks in one plugin
- How to disable reCaptcha v3 except on Contact Form 7 pages?
- Fetching the value of forms in WordPress AJAX
- Proper way to run wp_query from inside a plugin
- Buddypress function and global $bp question
- Custom plugin: Loop through taxonomy types and update columns for all types?
- How to add setting section in custom post type [closed]
- Get Current Menu Location inside Nav_Walker
- Maximum lifetime for nonce
- How to Display my HTML form in my Custom Plugin?
- Where to add hooks in a class
- Insert terms for custom taxonomy on plugin activation, or each page load (init hook)
- Custom GET Parameters In Plugin’s Admin Page
- Adding option to Gallery shortcode
- WordPress Search filter to remove possible script injections
- Adding rewrite rule dynamically
- Incorporate small angular feature in my wordpress site
- Bootstrap doesn’t work on admin menu page-How to override wp-admin style?
- Use just a shortcode from another page
- how to invoke wordpress API from other existing PHP system
- How to cancel update messages from a changed plugin
- How to get the post excerpt using post object?
- jquery & ajax sending data to php
- How to import the css in the plugin admin area?
- how to get the post id in plugin
- What’s the impact on WordPress’ performance regarding the amount of classes/files in a plugin?
- Using add_sub_menu to put into Appearance Section
- How to call code when adding WooCommerce menu items via woocommerce_account_menu_items
- jquery in wordpress plugin with depdendency
- Replace first occurence of a word with link [closed]
- Plugin creation, restricting access to specific roles
- wordpress automatic update does not run
- Ajax +wordpress onClick link redirect to new page and create html content
- Adding filter to the title without affecting the menu title
- How to move an image in a plugin to the upload directory and make it as a media of wordpress?
- Is it possible for two WordPress plugins to share the same code base?
- Script to replace default blogroll with links to my social media URLs
- How to show multiple instances of the WP125 Widget?
- Add multiple checkboxes as single field to Custom Shipping Method Settings Page
- Posts form with AJAX request – Plugin development
- Multiple TinyMCE on button click is initialized and appended but why only last one is writeable?
- register_meta not showing custom post type metabox data in rest api
- Want to know how to reveal a WordPress theme, considering the theme name is hidden?
- Where to add functions and code snippets in wordpress
- How to re-render inspector controls?
- Save meta box data from selected dropdown list in bbpress reply form
- Sanitize WordPress Array Input?
- Events Manager: Create custom Placeholder for use in Email notification
- WC REST API Error Handling
- woocommerce add a new bulk action in the bulk actions dropdown in the orders list
- Creating a virtual page without exit
- Plugin Development – Call to undefined function comment_exists()
- WordPress doesn’t create table on plugin activation
- Building WordPress Themes With Bootstrap with Adi Purdila → TutsPlus
- Allow user to change homepage
- Need to change buddypress profile url by add_rewrite_rule
- wp_mail links are dead
- Generate activation url
- Use different archive template for plugin
- Get custom wp_query search results to appear on search.php
- Is it safe to use PUT and DELETE requests
- Multi-part form and wp_redirect()
- Sharing changes to a post (preview changes) with another user
- Developing a wordpress.com shortcode [closed]
- Modify a Themes Appearance >> Header admin screen from a plugin
- correct way to include an external HTML page in WP-admin
- Add custom CSS for plugin admin Page
- Fetch portion of Admin URL