D
D
dev_b2018-12-14 13:21:37
WordPress
dev_b, 2018-12-14 13:21:37

Wordpress CNC from GET parameters?

Greetings, comrades.
There is a link of the form - /catalog/platforms/android/oreo/?p_from=100&p_to=1000&country=russia&style=all&type=all
Tell me how to bring it to this form - /catalog/platforms/android/oreo/100/1000/russia/all/ all
I tried to do it through add_rewrite_rule, but the URL does not change, it just follows the link with GET parameters.

add_action('init', 'do_rewrite');
function do_rewrite(){
  add_rewrite_rule( '([\w]+)=([\w+,.-]+)', 'index.php?pagename=catalog&p_from=$matches[1]&p_to=$matches[2]&country=$matches[3]&style=$matches[4]&type=$matches[5]', 'top' );

  add_filter( 'query_vars', function( $vars ){
    $vars[] = 'p_from';
    $vars[] = 'p_to';
    $vars[] = 'country';
    $vars[] = 'style';
    $vars[] = 'type';
    return $vars;
  } );
}

I would be grateful for any advice where to "dig". I've been wrestling with this issue all day.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vitaliy Orlov, 2018-12-15
@orlov0562

The format is like this
on a regular basis you should have
it will look like this
accordingly you will get such pockets

$matches[0] = /catalog/platforms/android/oreo/100/1000/russia/all/all
$matches[1] = android ; регулярка ([^/]+)
$matches[2] = oreo ; регулярка ([^/]+)
$matches[3] = 100 ; регулярка (\d+)
$matches[4] = 1000 ; регулярка (\d+)
$matches[5] = russia ; регулярка ([^/]+)
$matches[6] = all ; регулярка ([^/]+)
$matches[7] = all ; регулярка ([^/]+)

now use these pockets in replacement
as a result, the url should look like this after the redirect
* note that android and oreo are not included in the url, you need to add them if necessary according to your parameters
Final expression for the redirect
add_rewrite_rule(
 'catalog/platforms/([^/]+)/([^/]+)/(\d+)/(\d+)/([^/]+)/([^/]+)/([^/]+)', 
 'index.php?pagename=catalog&p_from=$matches[3]&p_to=$matches[4]&country=$matches[5]&style=$matches[6]&type=$matches[7]', 
 'top' 
);

After saving, you need to go to the admin panel, go to the "Settings - Permalinks" section and click the [Save changes] button.
Then you need to open .htaccess and make sure that the correct rule has been created.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question