Wordpress custom url rewrite - wordpress

Is it possible in wordpress to rewrite the following url.
http://www.example.com/aboutus/?city=newyork to http://www.example.com/aboutus/newyork
and then read out the get_query_var('city') , how do i accomplish this within wordpress?

This is the sollution:
add_filter( 'query_vars', 'add_query_vars_filter' );
function add_query_vars_filter( $vars ){
$vars[] = "city";
return $vars;
}
//https://thereforei.am/2011/10/28/advanced-taxonomy-queries-with-pretty-urls/
function eg_add_rewrite_rules() {
global $wp_rewrite;
$new_rules = array(
'aboutus/(.+?)/?$' => 'index.php?page_id=274&city=' . $wp_rewrite->preg_index(1)
);
$wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
}
add_action( 'generate_rewrite_rules', 'eg_add_rewrite_rules' );
echo get_query_var('city');

Related

Wordpress rewrite url not working on custom template

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();
}

Wordpress rewrite slug = post_meta

At this time I have 2 custom post-type item, projects, made custom field for attach project post in item post (custom field save post_id)
I want use attached project post-name in link:
Current link structure:
domain / {item-post-type-slug} / {item-name}
I want change to:
domain / {project->post_name} / {item-name}
With current code should flush rewrite rules on every save/delete action on projects post-type?
How to redirect old link structure to the new ?
add_action( 'init', 'add_rewrite_rules' );
function add_rewrite_rules() {
$arg = array(
'post_type' => 'projects',
'no_conflict' => '1',
'posts_per_page' => '-1'
);
$projects= new WP_Query($arg);
while($projects->have_posts() ) : $projects->the_post();
global $post;
add_rewrite_rule( $post->post_name.'/([^/]+)/?$', 'index.php?item=$matches[1]', 'top');
endwhile;
}
add_filter( 'post_type_link', 'custom_permalinks', 10, 2 );
function custom_permalinks( $permalink, $post ) {
if ( $post->post_type !== 'item' )
return $permalink;
$project_id = get_field('project', $post->ID);
$project = get_post($project_id);
$new_permalink = str_replace("item", $project->post_name, $permalink);
return $new_permalink;
}
For flush rewrite rules using
function flush_project_links( $post_id) {
if ( get_post_type( $post_id ) != 'projects' )
return;
flush_rewrite_rules();
}
add_action('delete_post', 'flush_project_links', 99, 2);
add_action('save_post', 'flush_project_links', 99, 2);
but it's work only on second time when update post
UPDATED:
flush rewrite fixed:
function flush_project_links( $post_id) {
if ( get_post_type( $post_id ) != 'projects' )
return;
add_rewrite_rules()
flush_rewrite_rules();
}
add_action('delete_post', 'flush_project_links', 99, 2);
add_action('save_post', 'flush_project_links', 99, 2);
and removed add_action( 'init', 'add_rewrite_rules' );

Auto add query string for admin URL Wordpress

I want auto add query string "_adminToken" for admin URL, example:
http://mydomain.site/wp-admin/plugins.php => http://mydomain.site/wp-admin/plugins.php?_adminToken=123
http://mydomain.site/wp-admin/edit.php?post_type=page => http://mydomain.site/wp-admin/edit.php?post_type=page&_adminToken=123
This is my code:
add_action( 'after_setup_theme', 'my_theme_setup', 999 );
function my_theme_setup() {
add_filter( 'admin_url', 'filter_admin_url' );
}
function filter_admin_url( $url ) {
$query_string = parse_url( $url, PHP_URL_QUERY );
return !$query_string ? $url . '?_adminToken=123' : $url . '&_adminToken=123';
}
But it not working, somebody can help me?

Wordpress rewrite url with query parameters

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;
}

Custom rewrite permalinks WordPress

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');

Resources