php – get wordpress username to customize url

In addition to the December 31 ’19 answer, I think this answer combines many of the answers given on this site. But since this website likes reproductions rather than references, I’ll give a possible solution – including today’s question (1 hour ago according to my current screen).

function prefixed_redirect() {
    global $wp;
    if ( home_url( $wp->request ) == 'your_url' )  { // or maybe something more specific.
        require_once TEMPLATEPATH . '/my-specific-template.php';
    }
}
add_action( 'template_redirect', 'prefixed_redirect' );

I haven’t tested this code, but it is supported by these references: https://stackoverflow.com/questions/51028362/what-is-the-alternative-of-serverhttp-host-and-serverrequest-uri-for and How to load a new template page according to a particular URL?.

Now in terms of menu addition, I have this example:

function prefixed_nav_menu_item( $items ) {
    $user = wp_get_current_user();
    $link = '<li><a href="' . get_permalink( $user>get( 'slug' ) ) . '">' . $user->get( 'login' ) . '</a></li>';
    $items = $items . $link;

    return $items;
}
add_filter( 'wp_nav_menu_items', 'prefixed_nav_menu_item' );

This is also not tested, but according to https://developer.wordpress.org/reference/functions/wp_get_current_user/ and https://developer.wordpress.org/reference/classes/wp_user/ it should lead you in the right direction Will go

It may be that your specific theme has a predefined function in formatting menu items that you should look into (of which theme template files are a good starting point).

Leave a Comment