0 votes
in Programming by (1.9k points)

I created a shortcode by add_shortcode. where the shortcode placed in a wordpress page to display dynamic content. The dynamic content is based on the URL value.

Example

If URL is wordpress/sample-page/?user=john in my shortcode content I get the url value by $_GET['user']

Now I would like to rewrite the url like wordpress/sample-page/john. And want to get the URL values in shortcode. Is this possible in wordpress?

if so Please guide me.

1 Answer

0 votes
by (1.9k points)

The simplest and quickest solution would be to use the add_rewrite_endpoint function.

<?php add_rewrite_endpoint( $name, $places ); ?>

So in your case it would be something like this:

add_rewrite_endpoint( 'user', EP_PERMALINK | EP_PAGES );

An user URL would look like this:

http://www.example.com/my-page/user/john

To access the variable:

$user = get_query_var( 'user', false );

HOPE so you find your answer!!

...