admin管理员组

文章数量:1426810

I have a simple rewrite to pass a variable:

// Add custom URL parameters
function add_custom_query_var( $vars ){
  $vars[] = "item_id";
  return $vars;
}

add_filter( 'query_vars', 'add_custom_query_var' );

// Add rewrite for custom query vars
function custom_rewrite_basic() 
{
    add_rewrite_rule('^add-item/([0-9]+)/?', 'add-item/?item_id=$1', 'top');
}

add_action( 'init', 'custom_rewrite_basic' );

On the add-item page, the variable is fetched as such:

$item_id = filter_input( INPUT_GET, "item_id", FILTER_SANITIZE_NUMBER_INT );

if(!empty($item_id)) {
    ...

When the page is called with add-item/1, var_dump($item_id) comes back with NULL. Any other number (including 0) work fine. Any ideas?

I have a simple rewrite to pass a variable:

// Add custom URL parameters
function add_custom_query_var( $vars ){
  $vars[] = "item_id";
  return $vars;
}

add_filter( 'query_vars', 'add_custom_query_var' );

// Add rewrite for custom query vars
function custom_rewrite_basic() 
{
    add_rewrite_rule('^add-item/([0-9]+)/?', 'add-item/?item_id=$1', 'top');
}

add_action( 'init', 'custom_rewrite_basic' );

On the add-item page, the variable is fetched as such:

$item_id = filter_input( INPUT_GET, "item_id", FILTER_SANITIZE_NUMBER_INT );

if(!empty($item_id)) {
    ...

When the page is called with add-item/1, var_dump($item_id) comes back with NULL. Any other number (including 0) work fine. Any ideas?

Share Improve this question asked Apr 29, 2019 at 0:53 KermitKermit 15512 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 2

Rewrite rules in WordPress should point to index.php, and to substitute a matched value (number from address) to URL parameter (item_id) you must use $matches[1] instead of $1.

So your rule should look like this:

add_rewrite_rule('^add-item/([0-9]+)/?', 'index.php?item_id=$matches[1]', 'top');

You usually need to complete the rule with information about what is displayed. For display:

  • single post / page - add to rewrite &p={post_id} or &postname={post_slug}
  • archive page - &post_type={post_type_slug}
  • custom category - &{taxonomy_slug}={term_slug}.

This rule setting only the item_id query var, so if you have not added your own function to template_include filter hook, WP will display the homepage.

Edit:

Rules with and without index.php
Rewrite rules begining with index.php are stored in DB. When parsing request, WordPress get them from the DB and tries to match the request URL to one of the rules.

Rules that do not start with an index.php, do not go to the database. They are not checked when the request is being parsed by WordPress. It is possible that they will be saved in .htaccess file, but only if you use Apache and file permissions allow for writing.

Why $item_id is NULL
example/add-item/1 address is recognized as a post (post type can be page or post) with several pages (post pagination). You should know that WordPress removes page number from URL if it equal to "1".
WordPress checks whether the requested url is canonical by comparing the url with the one it generates. Canonical URL in this case (first page of post) is example/add-item/ and it is not the same as the requested one example/add-item/1, so redirecting will be done.

I do not know what type of post is the add-item (custom post type or built in), but try this way:

add_action( 'init', 'se336565_custom_rewrite_basic' );
add_filter( 'query_vars', 'se336565_add_custom_query_var' );

function se336565_add_custom_query_var( $vars ){
  $vars[] = "item_id";
  return $vars;
}
function se336565_custom_rewrite_basic() 
{
    add_rewrite_rule('^add-item(?:/([0-9]+))/?$', 
        'index.php?post_type={custom_post_type_slug}&pagename=add-item&name=add-item&item_id=$matches[1]', 'top');
}

Replace {custom_post_type_slug} and flush the rewrite rules.

Finally, on the actual page use get_query_var( 'item_id' ).

本文标签: url rewritingInteger based rewrite isn39t recognized for value of 1