Wordpress: if (url example.com/sitemap) does this ...

I used to create a page with a site example.com?action=sitemap

This allowed me to easily test my index page for a Sitemap request with ...

$_REQUEST['action']

However, I would like to create a link to the site map using example.com/sitemap

And I would like to know how I can parse the request for the appearance of " /sitemap"

+3
source share
5 answers

You can create a new rewrite rule in Wordpress like this:

function sitemap_rewrite($wp_rewrite) {
    $rules = array('sitemap' => 'index.php?action=sitemap');
    $wp_rewrite->rules = $rules + $wp_rewrite->rules;
    return $wp_rewrite;
}
add_action('generate_rewrite_rules', 'sitemap_rewrite');

function flush_rewrite_rules() {
    global $wp_rewrite;
    $wp_rewrite->flush_rules();
}
add_filter('init', 'flush_rewrite_rules');

You only need to run these functions once (when your theme is installed, for example), because rewriting rules are stored in the database.

, , , action, $_REQUEST['action']. , Wordpress 'query_vars, :

function add_action_query_var($vars) {
    array_push($vars, 'action');
    return $vars;
}
add_filter('query_vars', 'add_action_query_var');

get_query_var('action').

+1
if (strpos($_SERVER['PHP_SELF'],'sitemap')===false) {
//sitemap not found in server string
} else { 
//sitemap found in server string
}

+1

Wordpress, /, mod_rewrite, ( , , ). .htaccess -:

RewriteEngine On
RewriteRule ^/sitemap$ /index.php?action=sitemap [QSA,L]

.htaccess RewriteEngine On, RewriteEngine.

What he does basically tells Apache to process the entire request /sitemapas requests for /index.php?action=sitemap.

+1
source

You can try checking the contents of a PHP variable $_SERVER["QUERY_STRING"]. It should have your part /sitemapwith the possibility of some additional parameters.

0
source
0
source

Source: https://habr.com/ru/post/1730065/


All Articles