1

I've created an rewrite endpoint with the add_rewrite_endpoint function … here is the whole contruct:

// Register to query vars

add_filter( 'query_vars', 'add_query_vars');


function add_query_vars( $vars ) {

    $vars[] = 'account';

    return $vars;

}


// Add rewrite endpoint


add_action( 'init', 'account_page_endpoint' );

function account_page_endpoint() {

    add_rewrite_endpoint( 'account', EP_ROOT );

}



// Account template

add_action( 'template_include', 'account_page_template' );

function account_page_template( $template ) {

    if( get_query_var( 'account', false ) !== false ) {

        return locate_template( array( 'account.php' ) );

    }

    return $template;

}

This works great so far when i enter a url like example.com/account/username ... but the links in the site are still like example.com?account=username.

How do i redirect from the parameter version to rewritten version? Is it necessary to add a additional rewrite rule or is there any function that these links have to run through?

The account links on the site itself are created by this function:

function account_url( $user_id ) {

    $user = get_userdata( $user_id );

    return add_query_arg( 'account', strtolower( $user->user_login ), get_home_url() );

}
  • What code is generating those links? – Milo Mar 29 '18 at 22:19
  • @Milo: Ive edited the question and added the function that are used to create these account links/urls – GDY Mar 30 '18 at 7:28
2

Rewrite rules only handle incoming requests, they aren't involved in link generation.

The primary WordPress rewrite system is parsed internally with PHP, you won't see any changes to an .htaccess file when you add an endpoint. The basic .htaccess rules essentially say "If this isn't a request for a physical file or directory on the server, then hand the request over to WordPress".

Your account_url function has to handle both "ugly" and "pretty" cases when it outputs the account URLs. We can look at the built in get_permalink function to see how WordPress handles this, here's an abbreviated version:

$permalink = get_option('permalink_structure');
if ( '' != $permalink ) {
    // output a pretty permalink
} else {
    // output an ugly permalink
}

The permalink_structure option holds the chosen pattern if pretty permalinks are enabled, we know pretty permalinks are disabled if it's empty.

  • On point! Thank you, that was exactly what i wanted to know. – GDY Apr 1 '18 at 15:39
4

EDIT 2

To use pretty permalinks, such as example.com/account/john you need to activate in your admin area, in the permalink settings, and activate it on you server. You told us in comments that you used nginx, I know better apache so here is a tutorial which could help you.

https://www.cyberciti.biz/faq/how-to-configure-nginx-for-wordpress-permalinks/

EDIT

I went too fast, forget about my suggestion of add_rewrite_rule, I think that with using add_rewrite_endpoint you have to use the "template_redirect" hook instead of the "template_include" one.

END EDIT

According to the code you show, I would add this to your account_page_endpoint function

add_rewrite_rule('^account/([a-z0-9]+)/?', 'index.php?account=$matches[1]', 'top');

It does not modify the .htaccess file, however you can refresh the rules.

You may need to refresh the rules after this modification.

Two possibilities:

-either you add, right after the line of code above, the call to the flush rewrite function like this:

flush_rewrite_rules();

-or you can go in you admin area, in the permalink settings and re save your current settings.

  • Ive added your function to init hook and flushed the rewrite rules by saving the permalinks ... but altough it seems like exactly what i need this has no effect ... – GDY Mar 30 '18 at 7:37
  • With this function there should be some changes visible in the rewrite rules of the .htaccess file? Right now it doesn't seem like anything of that rules have changed … – GDY Mar 30 '18 at 7:43
  • It does not deal with the server, I updated my answer. – Friss Mar 30 '18 at 7:47
  • Doesn't change anything even with a flush_rewrite_rules directly after the function ... this rewrite rule redirect ./?account=foobar to ./account/foobar? – GDY Mar 30 '18 at 7:53
  • Well it seems like your function does exactly the same like add_rewrite_endpoint( 'account', EP_ROOT ); ... so without that function and only your solution ./account/foobar/ still works. But the problem is that the links in the site are still like ./?account=foobar so it is necessary to change either the links to the rewritten format or redirect ./?account=foobar to ./account/foobar/ – GDY Mar 30 '18 at 8:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.