I'm trying to rewrite my urls but I don't know what I'm doing wrong.
I want the page site.ru/product-category/executiv-kresla/?wpf_filter_proizvoditel=dauphin&wpf_fbv=1
looked like site.ru/product-category/executiv-kresla/dauphin.
I made it
add_action('init', 'do_rewrite');
function do_rewrite() {
add_filter( 'query_vars', function( $vars ) {
$vars[] = 'wpf_filter_proizvoditel';
$vars[] = 'wpf_fbv';
return $vars;
} );
add_rewrite_rule('^(product-category/executiv-kresla/)([^/]*)/([^/]*)/?$', 'index.php?pagename=$matches[1]&wpf_filter_proizvoditel=$matches[2]&wpf_fbv=$matches[3]', 'top');
}
but it doesn't work.
Related
Wordpress codex says I can access URL parameters using get_query_var() when I register the variable to WP_Query using this code
function add_query_vars_filter( $vars ) {
$vars[] = "i_id";
return $vars;
}
add_filter( 'query_vars', 'add_query_vars_filter' );
https://codex.wordpress.org/Function_Reference/get_query_var
But that doesn't work the URL
https://example.com/contact-form/contact-de/?i_id=12345
doesn't give me any id parameter.
<?php
function add_query_vars_filter( $vars ) {
$vars[] = "i_id";
return $vars;
}
add_filter( 'query_vars', 'add_query_vars_filter' );
$i_id = get_query_var('i_id');
var_dump($i_id);
Always prints string(0) ""
What am I doing wrong here?
Try moving this part to your child theme's functions.php:
function add_query_vars_filter( $vars ) {
$vars[] = "i_id";
return $vars;
}
add_filter( 'query_vars', 'add_query_vars_filter' );
And keep this part in your template:
$i_id = get_query_var('i_id');
var_dump($i_id);
The URL is:
mywebsite.com/custom/?var1=random_text
and I want
mywebsite.com/custom/random_text
here's the code in my functions.php file:
add_filter( 'query_vars', 'wpse12965_query_vars' );
function wpse12965_query_vars( $query_vars )
{
$query_vars[] = 'var1';
return $query_vars;
}
add_action( 'init', 'wpse12065_init' );
function wpse12065_init()
{
add_rewrite_rule(
'custom(/([^/]+))?/?',
'index.php?pagename=custom&var1=$matches[1]',
'top'
);
}
But it still returns 404 error. What am I doing wrong?
Your code is looking perfect try to add flush_rewrite_rules function.
add_filter( 'query_vars', 'wpse12965_query_vars' );
function wpse12965_query_vars( $query_vars )
{
$query_vars[] = 'var1';
return $query_vars;
}
add_action( 'init', 'wpse12065_init' );
function wpse12065_init(){
add_rewrite_rule(
'custom(/([^/]+))?/?',
'index.php?pagename=custom&var1=$matches[1]',
'top'
);
flush_rewrite_rules();
}
I have custom search page, that have permalink http://mywebsite.com/custom-search/
What should I do to pass the search keyword as a parameter, like this: http://mywebsite.com/custom-search/keyword
I get error 404 page. Or may be there a way to change standard permalink /search/ to /custom-search/ ?
You should use rewrite endpoints
A sample code :
/*!
* URL rewrite
*/
function my_custom_rewrite_rules() {
$page_id = 123;
$page_path = get_page_uri( $page_id );
add_rewrite_endpoint( 'keyword', EP_PAGES );
add_rewrite_rule('^'. $page_path .'/(.*)/?', 'index.php?page_id=' . $page_id . '&keyword=$matches[1]', 'top');
}
add_action('init', 'my_custom_rewrite_rules');
and then add it as a query_var
function my_custom_query_vars($vars) {
if( isset( $_GET['keyword'] ) && !empty( $_GET['keyword'] ) ) {
$vars[] = 'keyword';
}
return $vars;
}
add_filter( 'query_vars', 'my_custom_query_vars', 10, 1 );
you will be able to retrieve the value of the passed keyword via get_query_var("keyword")
hope it helps
Note : You must update your permalinks structure or use flush_rewrite_rules(); after adding these codes
just changed search base with function
function vital_custom_search_base() {
$GLOBALS['wp_rewrite']->search_base = 'custom-search';
}
add_action( 'init', 'vital_custom_search_base' );
function only works after resave in settings > permalinks
I have post with categories based one year, month and date
Categories
2013
May 14
APRIL 10
2012
JUNE 6
I am creating the rewrite urls to particular date categories
by creating the url like category/slug-name/issues/year/month/date
My rewrite url is below
add_action('generate_rewrite_rules', 'past_issue_rewrite_rules');
function past_issue_rewrite_rules( $wp_rewrite ) {
$wp_rewrite->rules = array_merge( array('category/past-issues/issues/(.+)/(.+)/
(.+)/' => 'category/past-issues/?year='.$wp_rewrite->preg_index(1).'&month='.
$wp_rewrite->preg_index(2).'&day='.$wp_rewrite->preg_index(3)),
$wp_rewrite->rules );
}
add_filter( 'query_vars', 'setup_filter_query_vars' );
function setup_filter_query_vars( $query_vars )
{
$query_vars[] = 'year';
$query_vars[] = 'month';
$query_vars[] = 'day';
return $query_vars;
}
when I tried to access the page it is showing page not found.
what is the error?
Is it possible to send the parameters to category.php page
I am not sure. Please let me know if this is wrong.
I found the s0lution by myself and here is the code
function past_issue_rewrite_rules(){
add_rewrite_rule(
'category/past-issues/(\d+)/(\d+)/(\d+)/?$',
'index.php?category_name=past-issues&pyear=$matches[1]&pmonth=$matches[2]&pday=$matches[3]',
'top'
);
}
add_action( 'init', 'past_issue_rewrite_rules' );
add_filter( 'query_vars', 'setup_filter_query_vars' );
function setup_filter_query_vars( $query_vars )
{
$query_vars[] = 'pyear';
$query_vars[] = 'pmonth';
$query_vars[] = 'pday';
return $query_vars;
}
I have a page template that gets some variables added to the end of the url so I can display data based on what was passed.
ie: mysite.com/search-listins/listing/?address=123+The+Street&mls-number=00000
I need to convert this into a pretty permalink. Somthing like this:
mysite.com/search-listings/listing/123-The-Street OR mysite.com/search-listings/listing/00000-123-The-Street
I tried using this function. But nothing seems to be working. Any thoughts? These are not coming from a custom post type. As you can see, these are not a custom post type. These are MLS items that live outside the wp_ tables.
Function:
function setup_filter_rewrites(){
add_rewrite_rule('search-listings/listing/([^/]*)/?', 'index.php?pagename=search- listings/listing/?address=$matches[1]&mls-number=$matches[2]', 'top');
}
add_action( 'init', 'setup_filter_rewrites' );
function setup_filter_query_vars( $query_vars ){
$query_vars[] = 'listing';
return $query_vars;
}
add_filter( 'query_vars', 'setup_filter_query_vars' );`
I'm always using this code instead of add_rewrite_rule() function:
add_filter( 'query_vars', 'my_query_vars' );
function dlouhavidea_query_vars( $vars ) {
$vars[] = 'address';
$vars[] = 'miles';
return $vars;
}
add_action( 'generate_rewrite_rules', 'my_rewrite_rules' );
function ,y_rewrite_rules( $wp_rewrite )
{
$wp_rewrite->rules = array(
'search-listings/listing/address/?([^/]*)/?$' => $wp_rewrite->index . '?pagename=search-listings&address=' . $wp_rewrite->preg_index( 1 ),
'search-listings/listing/miles/?([0-9]{1,})/?$' => $wp_rewrite->index . '?pagename=search-listings&miles=' . $wp_rewrite->preg_index( 1 )
) + $wp_rewrite->rules;
}
You'll than find your vars in get_query_var('address') and get_query_var('miles');