0

I've created a regular wordpress page named "user" which is using a custom template.
This page is supposed to be called like so:
www.site.com/user/<ID>

Is it possible to get the ID segment of the URL programmatically?
The goal is to show the details of the user that belongs to the given ID.

When testing, that part of the URL is always removed.
If I request the URL www.site.com/user/123
the page is redirected to:
www.site.com/user

Thanks in advance.

2
  • Did you ever solve this problem? I have exactly the same thing happening. Posting an answer if you have it would be great for future searchers. TIA
    – mpdaly
    Commented Jun 27, 2023 at 10:37
  • @mpdaly the author has accepted an answer below already, see the answer with the big green checkmark
    – Tom J Nowell
    Commented Jun 28, 2023 at 17:50

2 Answers 2

2

There are two things that needed here, first add a new rewrite rule and second register the custom query variable.

So in your functions.php

/**
 * Add custom rewrite rule.
 *
 * NOTE: the `user_id` will be registered in the `query_vars` filter below. 
 */
add_action( 'init', function() {
   add_rewrite_rule(
      '^user/([0-9]+)/?',
      'index.php?pagename=user&user_id=$matches[1]',
      'top'
   );
} );


/**
 * Register custom query variables.
 */
add_filter( 'query_vars', function( $vars ) {
   $vars[] = 'user_id';

   return $vars;
} );

And inside your template you can then access the user ID by calling get_query_var( 'user_id' )

4
  • Thanks for your answer. Unfortunately I can't get it to work, and am still experiencing the unwanted redirect. If you had any tips for debugging this then they'd be much appreciated.
    – mpdaly
    Commented Jun 27, 2023 at 12:36
  • Did you try re-saving the Permalinks at least twice?
    – Den Isahac
    Commented Jun 27, 2023 at 14:10
  • Hi there! I implemented this solution and it worked. I completely forgot to provide feedback and express my gratitude here, so thank you. Commented Jun 28, 2023 at 12:27
  • Yes, it's all working now (I should say back working, because it is a 10-year-old site built be someone else with severe rot, and seemed to pre-date this technique: no idea how it used to work in an ancient version of WP!).
    – mpdaly
    Commented Jun 28, 2023 at 13:34
0

yes, you should be able to call the query var by:

get_query_var('user');

if this does not work, then "user" is not the registered query var. You can debug by dumping $wp_query and pin pointing the var / key.

global $wp_query;
1
  • Hi @Jhonatan Villena! Thks for the reply. Not sure if we are on the same page. My problem is when I try to access this URL: www.site.com/user/123 the browser redirects to: www.site.com/user. Is there a way of preventing this from happening? Commented Mar 30, 2022 at 23:16

Your Answer

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

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