This should work for you, using plugins_url()
:
...
// Assumes that your templates are in a subdirectory called 'templates' in your plugin.
// Adjust accordingly.
$templates_dir = plugins_url( 'templates', __FILE__ );
$page_404 = $templates_dir . '/404-' . $post_type . '.php' );
...
Update: template_include
If you want to have WordPress loaded up, you might be better off to use the template_include
filter.
add_filter( 'template_include', array( 'MyClass', '404_template' );
class MyClass {
function 404_template( $template ) {
if ( is_404() ) {
global $post_type;
$my_template = plugins_url( 'templates/404-' . $post_type . '.php' , __FILE__ );
if ( file_exists( $my_template ) ) {
$template = $my_template;
}
}
return $template;
}
}
Related Posts:
- Template redirect template loaded, but the header 404
- How do I redirect a permalink for a Draft post to a custom 404 page?
- Add custom content to nonexistent page
- WordPress Migration: Getting 404 Errors: Only Home Page works
- 404 when fetching image from wp-content/uploads/
- REST API: No route was found matching the URL and request method
- WordPress REST API 404
- Changing Site Address (URL) causes 404
- In wordpress I am manipulation 404 response. I want to give the response before 404 error is given to google
- 404 error on homepage when using Nginx proxying to Apache
- template_redirect to accompany with a shortcode
- page not found in Chrome, but found in Firefox
- How to identifty post from its URL in 404 page?
- Moved to new server, all pages work EXCEPT home page (page not found)
- High CPU usage on 404 errors
- How to fix “failed to set referrer policy: The value ‘ ‘ is not one of…”?
- 404 to 301 – Fixing old links
- $error = 404 after caught add_rewrite_rule
- How do I go about fixing this apparently messed up upgrade?
- WordPress Stats keeps showing non-existent post as a top post?
- Form in WordPress : 404
- Non-existent page returns code 200
- WP is redirecting instead of going to 404
- post parameter ‘name’ 404 error
- Intermittently missing pages and posts in WordPress
- WordPress Post – On Post – 404 Page Not Found
- How to find out which function is causing a 404
- Prevent WordPress from interpreting a subfolder?
- Getting 404s on New Pages
- All pages after level 1 showing 404 after WordPress migration plugin – how to fix?
- How to pass a message using template_redirect
- How can I prevent non-existing wordpress pages from getting redirected to home page
- Incorrect url for sharing blog on linkedin [closed]
- Catch 404 errors in nested installations
- Getting 404 error on my wp rewrite error
- redirect deleted / drafted pages
- WordPress refuses to read the .htaccess file and gives a 404 for sub-pages
- How to override a query and display specific page by ID?
- How to recover WordPress Adress(URL)
- My wordpress website does not show 404 page and results in http 500 error
- Why does the $_GET parameter ?search forward the front-page to the archive/blog page
- URL Change – Check if Post Exists Before 404
- How to use wp_footer() on a 404 page?
- Sub Domain’s Pages return 404
- Is the 404 page the same as “well this is embarrasing” page?
- Google 404 Errors
- Page not found if mod_cache_disk is active
- Why aren’t links clickable on my 404 error page? [closed]
- Redirect to first child in menu
- How to redirect home page to another page after login for all user?
- Blank TinyMCE and 404 errors on assets
- No 404 page available
- New install of wordpress, my url goes to index of/
- Redirect returning users to a certain page?
- Can’t view post, goes to 404 page. Please help
- Custom Post Types cause 404 for pages? [closed]
- send popup after wp_redirect()
- Trying to re-direct users to specific page based on an ACF variable
- How can stop redirects 404 in wordpress
- why I get a lot of 404 not found links via Google search console and how to fix them?
- Custom Login page, redirection and restrictions
- Redirect Users who aren’t logged in, aren’t post authors and aren’t admin. Frontend
- How do I redirect the browser to 404 page, if no posts are found in home page
- Page not found after migration to localhost. I already updated the sql links and the permalinks. Wp-admin page works perfectly
- 404 error on new pages but only for non logged in users?
- Unable to change 404
- How to redirect if 404 error in wordpress then redirect 404.php url
- Redirect search.php to 404.php
- Problem with a wordpress admin system. and website
- WordPress as backend for SPA – problem of 404 page for custom routing
- 404s after changing site url in settings
- Page 404 Error on Localhost
- wp-admin throws 404 when accessed from some IPs
- Internal redirect from page to category
- Why my post showing not found in admin side when i click on count of category?
- “Headers already sent” error triggered in “parse_query” action
- Manage 404 page over wordpress backend?
- I’m getting 404 when editing a post with hyperlink, what’s wrong?
- All files being pulled from wp-content returning 404 error
- What could be the reason that i can’t access to the other pages of my localhost wordpress website except the homepage?
- Page 2 404 Error – From main homepage, not custom post type
- Copying live site that is using older version of WP to localhost
- Intermittent Rewrite Problems
- Getting 404 pages when upgrading from 3.5.2 to 4.1.1
- My website not showing 404 error!
- Why are 404 errors redirecting to my homepage and not my custom 404.php?
- WordPress not showing custom 404 page for not found images, zip files
- Override private status – Enable users to read private posts
- Posting to most pages throws 404
- 404 Error for Custom Post type
- $_POST[‘message’] gives a 404 [duplicate]
- %20 in URL in archive page shows error 404 on page [closed]
- 404 Not Found nginx on wp-admin
- Redirect to a subdirectory frontpage using without using a WP plugin- what files to edit, and how?
- Author page not found
- Getting 404 when static files have “?ver=x.y” suffix
- Redirect drafted post of custom post type to URL?
- How to include a php file in 404.php file?
- Cannot find redirect – WordPress behind HTTPS proxy (Cloudfront)
- How to navigate to a non-wordpress page in a wordpress install without getting a 404 error?