WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

share|improve this question
What is loadWordPressTemplate() ? – Rarst Nov 30 '13 at 15:47

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 ) {  
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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