WordPress.org

Support

Support » Plugins and Hacks » Hacks » 'template_include' hook still has 404 status

'template_include' hook still has 404 status

  • 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?

Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator bcworkz

    @bcworkz

    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.

    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.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘'template_include' hook still has 404 status’ is closed to new replies.
Skip to toolbar