1

Is it possible to use php variable in menu URL, that I defined in function.php?

This is actually not working:

enter image description here

For example, in variable $path I store username of every user so they can visit their own profile.

2
  • Short answer: No, it's not possible. That url is just for text. It doesn't evaluate PHP code there. Commented Sep 11, 2017 at 19:09
  • What is the value of $path? Why don't you just put the value there? Commented Sep 11, 2017 at 19:13

2 Answers 2

1

You can use a hook and add the PHP programmatically.

add_filter( 'wp_get_nav_menu_items','nav_items', 11, 3 );
function nav_items( $items, $menu, $args ) {

     if( is_admin() )
        return $items;

    foreach( $items as $item ) {
        if( 'profil' == $item->post_title ) {
            $current_user = wp_get_current_user();
            $username = $current_user->user_login;
            $item->url .= '/' . $username;
        }
    }

    return $items;
}

I found this code from another answer on this community.

Sign up to request clarification or add additional context in comments.

1 Comment

yea i got username already stored i just need to attach it to profile link
0

In Wordpress You Can Do Direct Like this for get the current user details:

<?php
    $current_user = wp_get_current_user();

    echo 'Username: ' . $current_user->user_login . '<br />';
    echo 'User email: ' . $current_user->user_email . '<br />';
    echo 'User first name: ' . $current_user->user_firstname . '<br />';
    echo 'User last name: ' . $current_user->user_lastname . '<br />';
    echo 'User display name: ' . $current_user->display_name . '<br />';
    echo 'User ID: ' . $current_user->ID . '<br />';
?>

Reference: https://codex.wordpress.org/Function_Reference/wp_get_current_user

3 Comments

yea i got username already stored i just need to attach it to profile link
url/users/<?= echo '$current_user->user_login'; ?>
actually this is what i wrote above that is not working

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.