I want to add a new endpoint to the author page, so that the URL looks like this:

http://example.com/writer/username/articles

So far, I have:

  • Changed the author base
  • Added a rewrite rule
  • Setup the query_vars
  • Added the endpoint
  • Used template_redirect to use my template

Result: 404 Page Not Found (even after flushing the rewrite rules). I can’t figure out where the problem is. Also I’d like to add pagination to the endpoint. Below is my code:

Changed the author base:

function foo_change_author_base() {
    global $wp_rewrite;
    $wp_rewrite->author_base="writer";
}
add_filter( 'init', 'foo_change_author_base', 0 );

Added a rewrite rule:

function foo_rewrite_rule()
{
    add_rewrite_rule( 'writer/([^/]+)/articles(/(.*))?/?$', 'index.php?author_name=$matches[1]&articles=$matches[3]', 'top' );
}
add_action( 'init', 'foo_rewrite_rule' );

Setup query_vars:

function foo_add_vars($vars) {
    $vars[] = 'articles';   
    return $vars;
}
add_filter('query_vars', 'foo_add_vars');

Added the endpoint:

function foo_add_endpoint() {
    global $wp_rewrite;
    add_rewrite_endpoint( 'articles', EP_AUTHORS | EP_PAGES );
}
add_action( 'init', 'foo_add_endpoint');

And added my template:

function foo_rewrite_template()
{
    global $wp_query;

    if ( array_key_exists( 'articles', $wp_query->query_vars ) ) {
        include (get_template_directory_uri() . '/articles.php');
        exit;
    }
}
add_action( 'template_redirect', 'foo_rewrite_template' );

2 Answers
2

Your code should works. The line:

include (get_template_directory_uri() . '/articles.php');

Needs to have allow_url_include=1 in the server configration because you are trying to include a file via http. Can you check this?

You must know also that template_redirect should be use for a real redirect, a include() may have undesired effects here. I think what you really want is template_include:

add_filter('template_include', 'my_template_include');

function my_template_include( $template ) {
    global $wp->query;
    if ( array_key_exists( 'articles', $wp_query->query_vars ) ) {
        return get_template_directory() . '/articles.php';
    }
   return $template;
}

So here is my code:

function foo_change_author_base() {
    global $wp_rewrite;
    $wp_rewrite->author_base="writer";
}
add_filter( 'init', 'foo_change_author_base', 0 );

function foo_rewrite_rule()
{
    add_rewrite_rule( 'writer/([^/]+)/articles/page/([0-9]{1,})/?$', 'index.php?author_name=$matches[1]&articles=1&paged=$matches[2]', 'top' );
    //If you don't want the last trailing slash change the las /?$ to just $
    add_rewrite_rule( 'writer/([^/]+)/articles/?$', 'index.php?author_name=$matches[1]&articles=1', 'top' );
}
add_action( 'init', 'foo_rewrite_rule' );

function foo_add_vars($vars) {
    $vars[] = 'articles';   
    return $vars;
}
add_filter('query_vars', 'foo_add_vars');

function foo_add_endpoint() {
    add_rewrite_endpoint( 'articles', EP_AUTHORS );
}
//add_action( 'init', 'foo_add_endpoint'); // Commented out
// Edit: add_rewrite_endpoint is not needed when using add_rewrite_rule


// auto-add the articles endpoint to author links
function foo_author_link_filter( $link, $author_id, $author_nicename ) {
    return user_trailingslashit($link) . 'articles/';
}
add_filter( 'author_link', 'foo_author_link_filter', 20, 3);

add_filter('template_include', 'foo_template_include');
function foo_template_include( $template ) {
    global $wp_query;
    //You may need some other checks
    if ( array_key_exists( 'articles', $wp_query->query_vars ) ) {
        return get_template_directory() . '/articles.php';
    }
    return $template;
}

Now go to yoursite.com/writer/author_name/articles/

Leave a Reply

Your email address will not be published. Required fields are marked *