简体   繁体   中英

How to create a dynamic url with wordpress userid or username?

I am trying to achieve something that would create a URL of the following structure

https://example.com/profile/<username>/notification

I want to insert this URL in another plugin that would create a navigation button with this dynamic link.

I used this code:

<?php 


include($_SERVER['DOCUMENT_ROOT'].'/wp-blog-header.php');

global $wpdb;

global $current_user;
get_currentuserinfo();

$c = $current_user->ID;

 ?>

       <option value="<?php echo ‘../profile/'.$c.'/notifications' ?>" <?php selected(myplugin::getSetting('NavigationTabBarProfile'), ‘../profile/'.$c.'/notifications' ) ?>><?php echo "My Notif" ?></option> 

But the url always displays the userid of the admin that is logged into wp-admin and not the current user who is clicking the link. Even if I visit the link in private/incognito mode, it still fetches the userid of the last logged in admin.

I have been working on this for hours now, any help would be greatly appreicated.

Did you try get_site_url() wordpress function?

value="<?php echo get_site_url() . '/profile/' . $c . '/notifications'; ?>"

(Sorry I can't comment because my reputation is under 50...)

EDIT: Here is my result when debugging wp_get_current_user()

在此处输入图像描述

To do a debug like that I write this line in first line of my template page:

wp_die( var_dump( wp_get_current_user() ) );

You can try to put this line on your first page (the one called on plugins' wordpress hook) to see what happend. If the user is different, it means that the current user has been changed on script.

add_action( 'plugins_loaded', 'test_user' );

function test_user() {
    wp_die( var_dump( wp_get_current_user() ) );
}

Wordpress uses "me" permalink... So the answer without any unnecessary code would be https://example.com/profile/me/notification

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM