The current default profile page is like example.com/user/johndoe
. But can I access the page via example.com/user/?user_id=21
where 21 is johndoe's user_id
.
2 Answers
You can use the get_the_author_meta()
function for this purpose. Use the get_the_author_meta('ID')
for only get the ID.
Ensure you check the page first with is_author()
function before getting the ID.
https://developer.wordpress.org/reference/functions/get_the_author_meta/ https://developer.wordpress.org/reference/functions/is_author/
-
Actually I was hoping to know how I could construct a URL such as example.com/user/?user_id=21 Commented May 2, 2023 at 9:27
I don't quite understand your intentions but if you need to create a url of type /user/user_id and this url will also run to that user's profile page then you can try these steps:
- Create a custom teamplate file to handle the display of user information.
- Create a request handler function, if the user accesses the url with the path you requested, it will run the other custom template file, for example here is when the user enters the path with the url /user
- You can make use of this template by the code below. Note that your custom template file needs to write handling functions to get user information according to that user_id.
add_action('template_include', function($template) {
$link = trim(parse_url(add_query_arg(array()), PHP_URL_PATH), '/');
$link_array = explode('/',$link);
$url = $link_array[0];
if ( $url === 'user' ) {
// load the file if exists
header('HTTP/1.1 200 OK');
// return get_stylesheet_directory();
return get_stylesheet_directory() . '/custom_template.php';
exit;
}else{
return $template;
}
});