You can filter 404_template and return the path of your custom file from the plugin directory like this:
add_filter(
'404_template',
fn() => plugin_dir_path( __FILE__ ) . '404.php'
);
This hook exists only when a 404 happens, so you don’t need any custom detection.
Related Posts:
- Get page content using slug
- Change the_title() of a page dynamically
- How to edit the properties of a page programmatically?
- Filter to change the content of 404 page
- Pages 404 in wordpress
- How to create a dynamic page based on form data with a plugin?
- Use an empty page to build custom plugin output
- Is it possible for a plugin to register a page template file?
- Dropdown list of pages to get page id to store in plugin options
- Calling a function from functions.php in custom page/ blog post
- How do I approach removing menu items on the fly based on settings in my plugin?
- Custom rewrite rule serves content, but returns 404 error code
- Create a Page via plugin
- Secure Pages Best Practice
- Show custom data instead of 404
- Plugin generated virtual pages
- Add Password Generator on password protected page
- ECHO Executing 4 Times In Head
- WordPress pages are not published due to External database connection with WPDB class
- Create page (not the post type) dynamically
- Multiple array for post_content on plugin activation
- How to list all images used on a specific page?
- Create fixed static pages
- If $var is empty, return 404.
- Form submission to another page returning 404 error [duplicate]
- Set page to draft on plugin deactivation
- How to create archive page to add in menu
- Load specific page when a custom URL is hit
- Check current URL is 404 in pre_option_stylesheet filter hook
- Plugin frontend page design irrespective of the theme used
- Custom non-template page on front end fails to render
- Scripts/styles not loading on cloned WP Site when logged in
- How do I control the list of Pages an author can see?
- How to add conent (text) to Add New pages form of admin in WordPress
- auto create only 1 wp page in activate custom plugin
- Create pages for authors
- Plugin – Proper way to handle 404 pages?
- How do I use (or mimic) document.getElementById() on a page loaded from WordPress database?
- Using a Page Template Not in Theme
- Generating Multiple Divi Pages from Database
- Problem with Owl Carousel, infinite loop [closed]
- Is there a way to decide from init whether we are on a certain backend page?
- Can a WordPress plugin add read-only pages?
- Plugin – create a page without it appearing in the side menu
- Using wp_filesystem in Plugins
- How to write testable classes in WordPress
- Edit or Remove Plugin .htaccess Using The Admin Panel
- WordPress scheduled task is called but not executed
- wp_mail not recognizing cc and bcc headers
- How can I call “preview post” from wp_remote_get with authentication?
- When is it too late to call the action wp_enqueue_scripts?
- Use js script from one plugin in another plugin
- Plugin Loading Scripts and Styles on Every Page – Even when not being used
- Build dynamic page from cURL (HTML page) response with plugin
- template_redirect for single posts w/ custom fields
- can I prevent WP users (even admin) from deleting custom categories?
- How to Add Page Template from Plugin without Losing $this
- Backslashes being stripped from CSS
- Uploading images from a custom page using blueimp uploader?
- Extending WP_List_Table seems getting wrong with $this->get_columns() – what’s wrong?
- Why doesn’t flush_rewrite_rules run on plugin activation?
- Finding posts containing matching array elements in a meta field usign WP_Query
- Namespaced action and filter tags
- Adding Dropbox dropin js using wp_enqueue_script
- Proper way to pass credentials in a custom login form to avoid “headers already sent”
- How to add custom fields to the all users page
- Can Page Templates be Applied to Archive and Post Templates?
- WooCommerce Admin Reports : Get custom product report based on date range
- Auto gallery from folder contents
- Is there a plugin that will allow a tag cloud to be created from any mysql field?
- Allow users to add / remove settings in plugin
- Update problem with update_option() in combination with register_setting()
- Why does my widget break other widgets?
- How do I add a text field under the activity texarea? [closed]
- WP Cron being triggered but not working as expected
- Plugin Options Array Set to Undefined
- is pre_get_comments not working?
- Settings API – format container of setting and setting’s description
- Delete option of a Plugin
- Automating the process of shortcode generation in a plugin – how is it done?
- Proper way to include stylesheet for panels
- Get thumbnails with array sizes parameter
- Why would someone use function_exists(‘add_action’) in a plugin?
- WordPress Gutenberg InnerBlocks renderAppender not showing with template
- How to share user data across multiple WordPress websites?
- How to properly create table in plugin
- Set the payment processor callbacks to a plugin
- Error code when migrate
- Add two button to page and post admin to redirect to : add new & list of (posts – pages) just after saving post or page
- WordPress: I wanted to use Add rewrite rule multiple times for multiple CPT in my plugin
- Loading ACF Field content on a 404.php page
- WordPress Ajax not returning Response
- Group By in a Metaquery
- get_option() is undefined in TinymceWindowManager
- Getting duplicate page when activating my plugin
- Modify request query string before plugins
- how to insert content into wp_head after loop_end
- Remove Gutenberg Buttons Block
- How to detect when the user clicks the publish/update button in Gutenberg Editor
- Is It Possible to Use the Block Editor’s Notification Feature to Send Another Notification After the Post Is Saved?