I am trying to write a code for my custom plugin where need to include a template page. I have tried in few ways but it redirects me to 404. After this i also added code for template redirect but no results. Somebody please notice me where I am doing wrong ?
Few example of my code snippet:
add_filter( 'template_include', 'portfolio_newpage_template', 99 );
function portfolio_newpage_template( $template ) {
$new_template = locate_template( array( dirname( __FILE__ ). '/portfolio-page-template.php' ) );
if ( '' != $new_template ) {
return $new_template ;
}
return $template;
}
For redirect i am using:
function templateRedirect() {
//global $wp;
// if ($wp->request == 'http://localhost/plugin-testing/portfolio') {
// loadWordPressTemplate(dirname( __FILE__ ). '/portfolio-page-template.php');
if ( is_page_template('portfolio-page-template.php') ){
loadWordPressTemplate(plugin_dir_url( __FILE__ ) . 'portfolio-page-template.php');
}
}
add_action('template_redirect', 'templateRedirect');
I already google many codes but i didn't get the right way to solve my issue. Currently i am testing in my localhost so I need to load my template when the url is something like:
esc_url(home_url( )).'/portfolio'
And one more thing i don't want to use wp_insert_post function that requires update_post_meta function which make visible the post/page in admin area. But if there have any way to hide the inserted post/page from admin area, really it will be appreciated from me.
Thanks in advance
loadWordPressTemplate()
?