I'm having a problem with my Wordpress site when using search. This is probably due to qTranslate plugin.
So I have permalink turned on and when I use search field, I get URL with ?s= query string like this:
www.mydomain.com/en/?s=test
The above URL works just fine but if I have many search results that show pagination buttons, links on those buttons cause 404 errors. Link for second page has the following URL:
www.mydomain.com/en/page/2/?s=test
This throws 404 error but if I manually modify the URL like this:
www.mydomain.com/en/search/test/page/2/
then the pagination works.
So I would like to force Wordpress to use /search/ permalink instead of /?s= query string but not sure how to do that.
you can do it by the following function, paste this into your theme functions.php
function change_search_url_rewrite() {
if ( is_search() && ! empty( $_GET['s'] ) ) {
wp_redirect( home_url( "/search/" ) . urlencode( get_query_var( 's' ) ) );
exit();
}
}
add_action( 'template_redirect', 'change_search_url_rewrite' );
then the search url will be like search/test instead of ?s=test,this function is quit simple
Related
I need to change my blog archive pagination url from:
/blog/2
to blog/page/2
This is our current permalink setup under Settings
Custom Structure - /blog/%postname%/
I don't have access to the server, htaccess, or the template files so trying to accomplish this inside functions.php
I am trying to use paginate_links filter to update the 'format' of my url string but does the $link argument have all the details of the paginate_links function, base, format..ect. I tried to use var_dump to see the $link array/object but nothing prints out to the screen.
// define the paginate_links callback
function filter_paginate_links( $link ) {
// Blog page
if ( !is_front_page() && is_home() ) {
var_dump($link);
return $link;
}
return $link;
};
// add the filter
add_filter( 'paginate_links', 'filter_paginate_links', 10, 1 );
I made a new website, and I have a little problem. The site doesn't need any kind of search engine etc..
So I don't need the search result page.
Is there maybe a way to make searching and search page redirect to home page?
So basically I want to redirect any user trying to access this page https://example.com/?s= to home;
Thank you
You could add the code below into your themes functions.php
add_action( 'template_redirect', 'redirect_s_to_homepage' );
function redirect_s_to_homepage(){
if ( is_search() && ! empty ( $_GET['s'] ) ){
wp_safe_redirect( home_url(), 301 );
exit;
}
}
It makes sure your loading search page and that there is a search variable set, and redirects you.
EDIT
The code above will work when there is an actual search query. example: ?s=something.
Since you are also wanting to redirect if there is no query, try the snippet below.
add_action( 'template_redirect', 'redirect_s_to_homepage' );
function redirect_s_to_homepage(){
if ( is_search() && isset( $_GET['s'] ) ){
wp_safe_redirect( home_url(), 301 );
exit;
}
}
The first code was making sure ?s= wasn't empty. The edited code just makes sure ?s= is set.
We have an action hook called 'template_redirect' , for search query we can do following to achieve:
function wm_search_redirect(){
global $wp_query;
if( $wp_query->is_search) {
wp_redirect( home_url() );
die;
}
}
add_action( 'template_redirect', 'wm_search_redirect' );
Please test it on non live site, as it is untested code just for idea.
SO I have this problem when I go to my main website, for example, www.mysite.com I want to redirect it to www.mysite.com/home I'm using this plugin 'link. it works but not always, I don't understand why so then I wrote a little script in the header file
$GetURL = (isset($_SERVER['HTTPS']) ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"
if ($GetURL === 'www.mywebsite.com') {
header('Location: www.mywebsite.com/home');
}
But still, it doesn't redirect. what could be a problem?
You can set your page as Front Page is Setting -> Reading or
add_action('template_redirect', 'function(){
if(is_front_page()){
wp_redirect(get_permalink('your_page_id'), 301)
}
});
Maxim's answer helped me redirecting the homepage to a custom post type archive, but it contains two typos. First, there must not be an apostrophe before "function"; second, the wp_redirect command needs to be closed with a semicolon.
This is a version of the code which, put into functions.php, points the front page to the archive of the post type 'project':
add_action(
'template_redirect',
function( $post_type ){
$post_type = 'project';
if ( is_front_page() ){
wp_redirect( get_post_type_archive_link( $post_type ) );
}
}
);
Obviously, you can use get_permalink() to redirect to a static page as well.
I do a custom wordpress rewrite with the following code:
function add_rewrite_rules() {
add_rewrite_rule(
'^mypath/([A-z0-9\-\_\,]+)/?$',
'index.php?page_id=2&tags=$matches[0]',
'top'
);
}
add_action('init', 'add_rewrite_rules');
This works fine:
When I open /mypath/tag1,tag2,tag3/ the page with the page_id==2 is shown, and GET parameter tags contains tag1,tag2,tag3.
The path stays /mypath/tag1,tag2,tag3/
But there is one exception:
When the page with the page_id==2 is marked as the wordpress startpage, then he forgets everything and redirects to /
Wordpress seems to redirect to WP_HOME, if the page is the startpage, but I want it to stay on /mypath/tag1,tag2,tag3/, because I want to load my startpage and use the tags parameter in an angular script.
Does somebody have an idea how I can prevent this redirect?
Found the solution by myself in this thread:
https://wordpress.stackexchange.com/questions/184163/how-to-prevent-the-default-home-rewrite-to-a-static-page
Just disable canonical redirect for front page:
function disable_canonical_redirect_for_front_page( $redirect ) {
if ( is_page() && $front_page = get_option( 'page_on_front' ) ) {
if ( is_page( $front_page ) )
$redirect = false;
}
return $redirect;
}
add_filter( 'redirect_canonical', 'disable_canonical_redirect_for_front_page' );
I am not sure if this is a known issue or suppose to be like this performance,
any wordpress website, if you put a number at the end of the URL, for example,
http://perishablepress.com/wordpress-multisite-mamp/
we put 1 or any numbers at the end
http://perishablepress.com/wordpress-multisite-mamp/123
We will not get the NOT FOUND page, we'll still stay at the same page, which is
http://perishablepress.com/wordpress-multisite-mamp/
Is this an error or normal ? how do we redirect to NOT FOUND PAGE instead?
It's not an error, the 123 is interpreted as a pagination parameter:
Request: wordpress-multisite-mamp/123
Query String: page=%2F123&name=wordpress-multisite-mamp
Matched Rewrite Rule: ([^/]+)(/[0-9]+)?/?$
Matched Rewrite Query: name=wordpress-multisite-mamp&page=%2F123
Posts can be paginated with <!--nextpage-->. WordPress displays the content of the last page if no further content is found (or the full post if the post is not paginated).
To redirect to the 404 page when the pagination parameter exeeds the number of pages, drop the following in your functions.php file:
add_action( 'template_redirect', 'so16179138_template_redirect', 0 );
function so16179138_template_redirect()
{
if( is_singular() )
{
global $post, $page;
$num_pages = substr_count( $post->post_content, '<!--nextpage-->' ) + 1;
if( $page > $num_pages ){
include( get_template_directory() . '/404.php' );
exit;
}
}
}