1

I create shortcode like this

function test_func( $atts ) {   

    return $_GET['myvar'];
}
add_shortcode( 'test', 'test_func' );

and one page with this name myparameters

so this is the final url

http://website.com/myparameters

if I try this works perfect

http://website.com/myparameters/?myvar=theparameter

But i like have pretty url or friendly url like this

http://website.com/myparameters/theparameter/

But show page not found.

I try some tutorial like this LINK but nothing happen

2
  • this answer will work for you.
    – Milo
    Feb 28, 2017 at 2:06
  • @Milo well, i investigate a litle more, if use global $wp; $view = $wp->query_vars['myvar']; to get the parameter and now work fine with add_rewrite_rule and return $view in shortcode function, I'll leave the complete example when ready.
    – metalbox
    Feb 28, 2017 at 2:16

3 Answers 3

0

You're forming a GET request, i.e. ?var=1&var2=2 but you've sent it as a POST request, you need to send the request as a GET request so that the variables are displayed in the url bar as you expect them to be displayed.

2
  • the @milo Link example is the answers only we need use global $wp; to get the value like this $wp->query_vars['myvar'];
    – metalbox
    Feb 28, 2017 at 2:19
  • I misunderstood your question that you wanted to know why you were getting one format vs. another format in the url bar. Good that you found a solution to your issue.
    – Matt G
    Feb 28, 2017 at 2:24
0

This is the full example, it works very well with one parameter, just change the id to that of your page.

function bartag_func( $atts ) { 
    global $wp;
    $view = $wp->query_vars['jp_stn'];
    return $view;
}
add_shortcode( 'bartag', 'bartag_func' );


function cdl_rewrite_rule(){
    add_rewrite_rule(
        'pronosticador/([-a-z]+)/?$',
        'index.php?page_id=52&jp_stn=$matches[1]',
        'top'
    );
}
add_action( 'init', 'cdl_rewrite_rule' );

function cdl_query_vars( $query_vars ){
    $query_vars[] = 'jp_stn';
    return $query_vars;
}
add_filter( 'query_vars', 'cdl_query_vars' );
3
  • @Milo What you think about this? in the correct way to get value with pretty url
    – metalbox
    Feb 28, 2017 at 2:26
  • @kromosome What you think about this? in the correct way to get value with pretty url
    – metalbox
    Feb 28, 2017 at 2:26
  • Note: Flush Permalink
    – metalbox
    Feb 28, 2017 at 2:39
0

To achieve pretty URLs with parameters in WP, you can utilize the rewrite rules provided by WordPress along with a custom endpoint or a rewrite rule.

you need to register a custom rewrite rule to handle the pretty URL. Add the following code to your themes functions.php file

function custom_rewrite_rule() {
    add_rewrite_rule('^myparameters/([^/]+)/?', 'index.php?pagename=myparameters&myvar=$matches[1]', 'top');
}
add_action('init', 'custom_rewrite_rule', 10);

function custom_query_vars($query_vars) {
    $query_vars[] = 'myvar';
    return $query_vars;
}
add_filter('query_vars', 'custom_query_vars', 10, 1);

After adding the rewrite rule, you need to flush the rewrite rules for Wp to recognize the new rule. You can do this by visiting the "Settings" > "Permalinks" page in your WP admin area

Update your shortcode function to retrieve the value of myvar from the query variables instead of $_GET

function test_func($atts) {
    $myvar = get_query_var('myvar');
    return $myvar;
}
add_shortcode('test', 'test_func');

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.