0

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

1
  • 1
    What is loadWordPressTemplate() ?
    – Rarst
    Nov 30, 2013 at 15:47

2 Answers 2

1

Your portfolio_newpage_template function does not result in a 404 when I try it, but it is not going to work either. locate_template will always load from theme directories, so it will never find your plugin file. You can see that in the source. __FILE__ is going to refer to the file with the function in it, which you say is a plugin, so what you have done will not work.

Just include your file and die.

add_filter( 'template_include', 'portfolio_newpage_template', 99 );
function portfolio_newpage_template( $template ) {  
  get_header();
  include('/some/file/path/file.php');
  get_footer();
  die;
}
0

See my question - and my own answer, which proved to be the solution: Create Page that uses specific template .

My issue was to have a specific page use a template that is in my plugin's folder. I used the following code (along with some supporting code to create the plugin):

// include our template
add_filter( 'template_include', 'use_our_page_template', 99 );

function use_our_page_template( $template ) {

    if ( is_page( 'My Custom Page' )  ) {
        $new_template =  plugin_dir_path( __FILE__ ) . 'templates/mycustomtemplate.php';
        return $new_template;
    }
return;

This will add your template to the template list (array) for the 'My Custom Page'. You can use that code in your plugin's file. Change the page name as needed. If you want to have that template available for all pages, then remove the 'if' statement.

If you put the above code in your plugin's 'init', then (if the plugin is enabled), the template from your plugin folder ('plugin-name'/templates) will show up for any page that you create.

See my answer for the above question for more explanation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.