Tom Isaacson
Tom Isaacson

Reputation: 1

Wordpress display 404 template with any url

I want, if url is http://mysite.domain/url-for-404/ (example), system will display 404 template.

This is my code:

function _display_404_template() {
      $current_url = get_current_url(); // get current url
      if ( $current_url === 'http://mysite.domain/url-for-404/' ) {
            global $wp_query;
            $wp_query->set_404();
            status_header(404);
            nocache_headers();

            $tpl_404 = get_404_template();
            if ( file_exists( $tpl_404 ) ) {
                  $tpl_404 = apply_filters( 'template_include', $tpl_404 );
                  require( $tpl_404 );
            } else {
                  wp_die( '404 - File not found!', '', array( 'response' => 404 ) );
            }

            exit;
      }
}

add_action( 'init', '_display_404_template', 20 );

It working, but page title is not "Page not found", title is "June - 2015 | Mysite".

I want page title is "Page not found", somebody can help me?

Upvotes: 0

Views: 545

Answers (2)

Samuel
Samuel

Reputation: 3801

Because this page actually does not exists. When you enter this URL to browser, WordPress will try to take you there. My suggestion, to create custom page url-for-404.php that will contain your code.

Upvotes: 0

Mario Sanchez Maselli
Mario Sanchez Maselli

Reputation: 1101

I think that with a 404.php you will have what you are looking for. And inside this file you can add your template.

Upvotes: 1

Related Questions