V
V
vadim0232015-07-02 09:38:47
PHP
vadim023, 2015-07-02 09:38:47

CNC WordPress conflicts with custom, how to fix?

The problem is, there is a page of Biographies with its own template, it searches for a person from the database using the get ID parameter and everything works well, but the site.ru/biography?id=42 link looks not very good, I added a rule to the .htaccess file for mod_rewrite

...
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^biography/([^/\.]+)/?$ biography/?id=$1 [L]
...

It works fine if you enter the numbers site.ru/biography/4 , But when you enter characters, say site.ru/biography/a , then the Wordpress redirect already works and it looks for an entry that starts with the character a .
How can I disable this search (redirect) for this example?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
ShamblerR, 2015-07-02
@ShamblerR

Firstly, you have a redirect with a get parameter, but it is not transmitted in this way at all, for a redirect with a get, a two-level construction is used. Where you can exclude in addition everything that you need.

B
berlevdv, 2015-11-19
@berlevdv

Perhaps crooked, but I was able to decide, the article helped. You
need to reassign Rewrite:

add_filter( 'rewrite_rules_array','my_insert_rewrite_rules' );
add_filter( 'query_vars','my_insert_query_vars' );
add_action( 'wp_loaded','my_flush_rules' );

// Сбиваем правила flush_rules(), если наших еще нет в списке (операция ресурсоемкая, поэтому не стоит ее делать каждый раз)
function my_flush_rules(){
    $rules = get_option( 'rewrite_rules' );

    // достаточно проверить, есть ли в списке хотя бы одно из наших правил
    if ( ! isset( $rules['biography/([^/]*)/?$'] ) ) {
            global $wp_rewrite;
            $wp_rewrite->flush_rules();
    }
}

// Добавляем наши правила
function my_insert_rewrite_rules( $rules )
{
    $newrules = array();
    $newrules['biography/([^/]*)/?$'] = 'index.php?pagename=biography';
    return $newrules + $rules; // так оно добавляет наши правила к уже существующим, а еще мы можем тупо убрать все и вернуть только свой список, сделав return $newrules;
}

// Добавляем переменную id, чтобы WP воспринимал ее
function my_insert_query_vars( $vars )
{
    array_push($vars, 'id');
    return $vars;
}

Now pages like site.ru/biography/a/ will open.
Next, it is necessary to process the received CNC in the code (I did this, because I didn’t understand how else it is possible to transfer the parameters), the article helped me with this:
PS: I’m not a programmer at all and I’m not completely understood, in this way the code may be superfluous.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question