0

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

  • this answer will work for you. – Milo Feb 28 '17 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 '17 at 2:16
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.

| improve this answer | |
  • 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 '17 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 '17 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' );
| improve this answer | |
  • @Milo What you think about this? in the correct way to get value with pretty url – metalbox Feb 28 '17 at 2:26
  • @kromosome What you think about this? in the correct way to get value with pretty url – metalbox Feb 28 '17 at 2:26
  • Note: Flush Permalink – metalbox Feb 28 '17 at 2:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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