WordPress.org

Ready to get started?Download WordPress

Forums

'template_include' hook still has 404 status (3 posts)

  1. dwieeb
    Member
    Posted 2 years ago #

    I'm writing a plugin that uses the 'template_include' hook to include a specific template file. The plugin is for internal use.

    add_filter( 'template_include', function( $template ) {
       global $wp_query;
    
       if( 'page-name' == $wp_query->query_vars['pagename'] ) {
          header("Status: 200");
    
          add_filter( 'wp_title', function() {
             return 'Thank you! ‹ ';
          } );
    
          return plugin_dir_path( __FILE__ ) . 'templates/some-template.php';
       }
    
       return $template;
    } );

    The problem is WordPress is returning this page with the http header status of 404. Even when I try to mark the status as 200 okay, it still doesn't work. How can I fix this?

  2. bcworkz
    Member
    Posted 2 years ago #

    Your filter function is probably not returning the path you think it should. No amount of faking the header status will get the server to find a file that's not there.

    Try replacing the return statements with die() functions, passing the path as the parameter to die(). You will see how the path is flawed and be able to adjust accordingly.

  3. dwieeb
    Member
    Posted 2 years ago #

    No, it is returning the correct path, because it's including the right template and displaying the right template when I go to the page.

    WordPress just thinks it's a 404 before the template is included, and it never corrects that HTTP status, even when I hard code 200 as the status.

Topic Closed

This topic has been closed to new replies.

About this Topic