template wordpress override
<?php
//some file override
add_filter( 'page_template', 'wpa3396_page_template' );
function wpa3396_page_template( $page_template )
{
if ( is_page( 'my-custom-page-slug' ) ) {
$page_template = dirname( __FILE__ ) . '/custom-page-template.php';
}
return $page_template;
}
//for single post ovverirde
https://codex.wordpress.org/Plugin_API/Filter_Reference/single_template
function get_custom_post_type_template($single_template) {
global $post;
if ($post->post_type == 'my_post_type') {
$single_template = dirname( __FILE__ ) . '/post-type-template.php';
}
return $single_template;
}
add_filter( 'single_template', 'get_custom_post_type_template' );
//All page override
https://github.com/wpexplorer/page-templater
//Template redirect
function my_page_template_redirect()
{
if( is_page( 'goodies' ) && ! is_user_logged_in() )
{
wp_redirect( home_url( '/signup/' ) );
die;
}
}
add_action( 'template_redirect', 'my_page_template_redirect' );
//template override 2
add_filter( 'template_include', 'portfolio_page_template', 99 );
function portfolio_page_template( $template ) {
if ( is_page( 'portfolio' ) ) {
$new_template = locate_template( array( 'portfolio-page-template.php' ) );
if ( !empty( $new_template ) ) {
return $new_template;
}
}
return $template;
}