• Hello! Is it possible to change the hierarchy of WordPress templates?
    In this case, I need to leave the “page” above the “author”.

    I have a page whose slug is the same as the author’s “user_nicename”.

    Example:

    Test Page (/test)
    Test User (/test)

    So, when accessing the “/test” link, WordPress shows the author’s page, but I need you to display the created page.

    Thank you.

    • This topic was modified 4 years, 4 months ago by Jan Dembowski. Reason: Moved to Fixing WordPress, this is not an Developing with WordPress topic
Viewing 5 replies - 1 through 5 (of 5 total)
  • The “page” template is checked before the “author” template.
    See https://core.trac.wordpress.org/browser/tags/5.4/src/wp-includes/template-loader.php#L57

    At the top of the function, there is do_action( 'template_redirect' ); if you want to hook there, or after all the checks, there is a filter you could use instead: $template = apply_filters( 'template_include', $template );

    Thread Starter wpforever18

    (@wpforever18)

    Thank you, Joy.
    The template I managed to fix, but unfortunately WordPress still keeps the “author” in priority.

    What I need is to leave the “page” as a priority.

    I don’t know which WordPress file I can modify this in (though it’s not ideal).

    And I couldn’t find a hook. 🙁

    Moderator bcworkz

    (@bcworkz)

    Far from ideal. Do not ever modify core files, it will never end well for you.

    Joy showed you the hooks you can use. “template_redirect” to hijack the entire process or “template_include” to override whatever WP wanted to use. Her link lands you in an odd spot because the file had been updated, the former action is currently at line 13 and the latter at line 104 of the same page.

    Thread Starter wpforever18

    (@wpforever18)

    Hello friends. I think I missed the title. I apologize. The moderator can change if necessary.

    What I need is to “change the route of the links”. Something like.

    I got what I wanted using the code below. I don’t know if it’s ideal, but it worked. The page is given priority when using the link with the slug similar to the author’s nickname.

    I don’t want to use the author’s page, because I created the page for him. So the slug is the same. However, before typing / slug I opened the author’s page instead of the page.


    add_action( 'init', 'wpse16902_init' );
    function wpse16902_init() {
    $GLOBALS['wp_rewrite']->use_verbose_page_rules = true;
    }

    add_filter( 'page_rewrite_rules', 'wpse16902_collect_page_rewrite_rules' );
    function wpse16902_collect_page_rewrite_rules( $page_rewrite_rules )
    {
    $GLOBALS['wpse16902_page_rewrite_rules'] = $page_rewrite_rules;
    return array();
    }

    add_filter( 'rewrite_rules_array', 'wspe16902_prepend_page_rewrite_rules' );
    function wspe16902_prepend_page_rewrite_rules( $rewrite_rules )
    {
    return $GLOBALS['wpse16902_page_rewrite_rules'] + $rewrite_rules;
    }

    A possible solution would be to disable author.php completely, but without redirection. Disable completely. But I couldn’t do that. All the code I found disables author.php, but redirects it. So it still exists, although it is hidden. Anyway, the code above helped me. I just ask now if it is ideal.

    • This reply was modified 4 years, 4 months ago by wpforever18.
    Thread Starter wpforever18

    (@wpforever18)

    (I was wrong. I wanted to quote “user_nicename” instead of “nickname”)

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Change the hierarchy of WordPress templates’ is closed to new replies.