plugins – Help understanding dynamic endpoints based on where a shortcode is used

Question

I am new to PHP and WordPress development. I have a lot of coding experience with javascript and python so I have somewhat of an understanding of some things since PHP is similar in a lot of ways.

There is a simple plugin I want to create, mostly for learning purposes, but I have not been able to find out how to do it in my search (probably because I am not sure what to call the functions I am trying to execute)

function roleplay_user_profile() { 
  
    // Shortcode Result
    $message="This is a user profile for ".get_user_meta( $GET_USER_ID, 'nickname', true ); 
    return $message;

    }
// register shortcode
add_shortcode('roleplay_user_profile', 'roleplay_user_profile');

The idea is to put the shortcode [roleplay_user_profile] in a page called ‘user’, where instead of $message I can just build the template of how I want to profile to look.

so the page URL would be ‘samplesite.com/user‘. But I want to get the $GET_USER_ID variable from the url that would look like this ‘samplesite.com/user/1‘, which for example, would return $GET_USER_ID = 1.

I would want this to be dynamic, and work even if I were to change the permalink of the ‘user’ page to ‘members’.

I tried using add_rewrite_endpoint() and add_rewrite_rule(), and dug a bit into other existing plugins to see how they use it, but am having a hard time figuring it out. What would be the right way to go about this?

any help or even link to a guide that I haven’t been able to find would be appreciated!

0
Kyle Ariel Kushmar 4 months 2022-07-28T15:30:29-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse