plugins - Passing URL parameters to WordPress pages and pretty url

admin2025-01-07  4

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


if I try this works perfect

/?myvar=theparameter

But i like have pretty url or friendly url like this

/

But show page not found.

I try some tutorial like this LINK but nothing happen

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

Share Improve this question edited Apr 13, 2017 at 12:37 CommunityBot 1 asked Feb 28, 2017 at 0:48 metalboxmetalbox 1541 gold badge1 silver badge5 bronze badges 2
  • this answer will work for you. – Milo Commented 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 Commented Feb 28, 2017 at 2:16
Add a comment  | 

3 Answers 3

Reset to default 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.

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' );

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');
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736262745a837.html

最新回复(0)