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