This plugin looks for page_contact.php from active theme’s folder and uses plugin’s templates/page_contact.php as a fallback.
<?php
/**
* Plugin Name: Test Plugin
*/
add_filter( 'template_include', 'contact_page_template', 99 );
function contact_page_template( $template ) {
$file_name="page_contact.php";
if ( is_page( 'contact' ) ) {
if ( locate_template( $file_name ) ) {
$template = locate_template( $file_name );
} else {
// Template not found in theme's folder, use plugin's template as a fallback
$template = dirname( __FILE__ ) . '/templates/' . $file_name;
}
}
return $template;
}
Create page “Contact” and then templates/page_contact.php in your plugins folder. Now you should see your template content instead of normal template.
Related Posts:
- What is singular.php?
- What are the differences between custom post type and custom page templates?
- Page Template as Custom Post Type Archive
- Loading custom page template via plugin
- Ordering Posts List By Taxonomy Terms?
- WordPress keeps fetching the archive page instead of the template page
- How to use a dedicated template for the Custom Post Type from a plugin?
- How to sort list of custom posts to get view like a tree of posts under categories and their children’s categories?
- Assign Page Template Within A Custom Post Type
- Custom post type archive and single.php files not working
- Page Templates – this code only works for one Custom Post Type
- Custom Post Type – Archive page title
- WordPress custom post type Single.php?
- Custom Post Types, Page Templates and Pagination. Why do I get a 404 Error?
- Custom Post Type slug same as page name causing a conflict
- Control Custom Post Type template from a plugin
- Detect if 1st, 2nd or 3rd level custom page?
- Create second custom 404 page for selected post type
- is_page_template not working as expected
- Hyphens instead of Underscores in Post-type Archive Template Filenames when Post-type handle contains underscore
- Help with CPT template pagination
- After updating a page, all pages on the site used the index.php template
- Using custom page templates with plugin [duplicate]
- Can I make WordPress use a custom template for a child page
- Create Template for Custom Post Type same like for Page
- How to load a template without it being assigned to a page/post?
- Custom template with CPT doesn’t display on frontend
- create a template page for a post
- Custom post type archive page template
- Custom Post Type with Templates using Meta Boxes?
- Posts are duplicating on wp_post_update
- Custom-Posttype & Custom Taxonomy WP_Query
- Page template with different page and post content
- Custom page sidebar using Template dropdown box
- Adding sidebar to template creates horizontal gap the size of the side bar
- How to render a custom post type template with custom fields using shortcode
- Adding the_content() in custom template email
- Custom Post Type Template Alternative
- General advice on addressing content-centric pages
- next_/previous_post_link() `in_same_category` appears to fail when true
- Multiple templates for single custom post type
- How do you create a custom template to display a category with an image and related posts below?
- WordPress Custom Search by post_type
- Pages Become “Archive” Instead of Using Page Template
- Custom post type pages return 404 page?
- Multiple single templates?
- Custom URL redirect in WP
- Associate page w/ specific single post template?
- Include php on a specific page template
- page template for custom post type
- Page Template Dropdown For Custom Post Types
- How to integrate single and archive templates for custom post type in any WordPress theme
- providing access to post_id or post inside functions.php
- wp_get_canonical_url showing first url of the post for custom page
- Custom page for editing custom post type on frontend based on url
- How do I display child post types in the parent post type template
- How can I modify a custom post type and custom page template for a child theme if all content seams to be handled by theme’s ‘native’ plugin?
- Custom permalink structure for remote content pages
- Enabling permalinks disables custom page template
- Mixing Custom Post Types and Page Templates in WordPress 3.7.1
- Custom Post Type Template Insert not working properply
- Loop and add Specific Categories and Products Images
- Custom Post Type page template doesn’t show up
- How can I allow users to edit text that will be displayed on a custom post type archive page?
- Problem with custom loop navigation inside the blog page
- WordPress giving me the index
- How to use pre_get_posts on archive page custom post type
- Can I automatically assign a page template to a custom post type (i.e page-slug.php file)
- Virtual page and loading template
- Custom Post type dont use custom page template (slug is not right)
- How to change Elementor Template for Single Page Post on Load
- A certain theme is forcing it’s singular post layout
- How to create additional rendering for custom post types?
- Set a template on a custom post in the plugin
- How can I use a custom template to load custom post type posts based on a post_meta value
- Showing Custom Post Type with his templates on a custom page template
- Question on templates
- Custom post type option page template
- Custom post Query and WordPress Post Query Clash
- How can I load Template file from wordpress plugin
- How can I find out what template is my custom post type using?
- Search custom post type result in same template page
- WordPress custom post type and page
- Ordering Posts List By Taxonomy Terms?
- Loading a template for a URL pattern
- Custom Homepage As Single Page or Custom Post Type?
- Templates list in “Page Attributes” metabox is inaccurate
- Nested custom post types templating
- Custom Post Type & Page Template
- Multiple search template with separate stylesheets
- Using custom post types within a section of a template
- How do I paginate a custom post type listing on a custom template page?
- Custom Post Type order Title ASC
- get tags from custom post type
- Dynamically change Custom Post Type Template OR Change Permalink?
- Custom Post Type Template Based on Page Slug?
- using a template for more then 1 page
- display related posts based on the PAGE TITLE
- I want to show a list of posts under specified categories
- I want to show a list of post with title that have link goes to that post and icon under the specific category to which it belongs