How can I remove search result page - wordpress

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.

Related

How to change search URL in wordpress?

I want to change the search url to something like this
http://simply.mmag.in/blog/?s=coaching
Where as my current search result shows which is recommended by most user and article
http://simply.mmag.in/blog/coaching
Code which i am using for the above search result
function wpb_change_search_url() {
if ( is_search() && ! empty( $_GET['s'] ) ) {
wp_redirect( home_url( "/blog/" ) . urlencode( get_query_var( 's' ) ) );
exit();
}
}
add_action( 'template_redirect', 'wpb_change_search_url' );
and the base url code
function re_rewrite_rules() {
global $wp_rewrite;
$wp_rewrite->search_base = 'blog';
$wp_rewrite->flush_rules();
}
add_action('init', 're_rewrite_rules');
How can i get search result like this
http://simply.mmag.in/blog/?s=coaching
What happends if you just remove or comment out your wpb_change_search_url() function?
Have you tried to just remove this custom code? Wordpress by default uses ?s= in urls, and code used by you just rewrite it.

How to Stop Accessing WordPress Page When User Login

I am trying to stop users from accessing a page when they already login. Anyone please help me how to do this. I checked it on google and all over the stake overflow but did not find any solution. Hope to get answer soon.
Thanks
try this code bellow , put it within your theme function.
function my_page_template_redirect()
{
if( is_page( 'page-slug' ) && is_user_logged_in() )
{
wp_redirect( home_url() );
exit();
}
}
add_action( 'template_redirect', 'my_page_template_redirect' );
Change page slug based on your case, you also can use page_id check these references bellow. Don't forget to save permalink after put that code
Reference :
https://codex.wordpress.org/Plugin_API/Action_Reference/template_redirect
https://developer.wordpress.org/reference/functions/is_page/

redirect specific page category if not logged in

I'm sure this was working but now it's not. I added categories to pages with:
function add_categories_to_pages() {
register_taxonomy_for_object_type( 'category', 'page' );
}
add_action( 'init', 'add_categories_to_pages' );
then tagged a range of pages with a 'client' category and added to functions:
add_action( 'template_redirect', 'client_redirect_to_login' );
function client_redirect_to_login() {
$category_slug = 'client';
global $pages;
if ( ! is_user_logged_in() && in_category( $category_slug, $pages ) ) {
wp_redirect( site_url( '/login' ) );
exit();
}
}
the intention, simply enough, is that not logged in users trying to directly access any of the 'client' pages is redirected to my custom /login page. being returned directly to the page they were trying to access would be a bonus but this, THIS, used to work. there's a dozen pages and growing, so restricting by category is easier than by page ID array - but i can't see what i'm doing wrong.
all advice greatly appreciated!
From global variables pages in codex $pages will return:
The content of the pages of the current post. Each page elements contains part of the content separated by the <!--nextpage--> tag.
And you need either post/page ID or object when using in_category() function as a second argument, as noted here.
You can try to get the category of the visited page by setting:
$current_cat = get_the_category();
And then check
in_category( $category_slug, $current_cat );
inside the conditional.

Wordpress redirect if URL parameter is empty for each post

So there are people who access my website directly (not thru the tracking link I got with Voluum), thus they are not able to click the links and I can't see them as a part of my stats.
How can I redirect users who don't have a /?voluumdata=BASE64... URL parameter to a tracked URL, and to have a different redirect for each blog post?
I was testing and looking for a plugin / .htaccess trick for hours but nothing seemed to help.
Edit: I found a solution that I was certain is going to work but for some reason it didn't:
[insert_php]
if(empty($_GET['voluumdata']))
{
header('Location: REDIRECT_URL');
exit;
}
[/insert_php]
Also tried:
[insert_php]
if(!isset($_GET['voluumdata']))
{
header('Location: REDIRECT_URL');
exit;
}
[/insert_php]
Both just break the page loading proccess.
Unfortunately, I cannot understand what is the purpose of the code you have entered in your question. I mean that is not clear the reason you use the tags [insert_php].
A solution to your problem it can be the following.
function redirect_direct_access( ) {
// You may use the code:
//
// global $wp_query
//
// in order to determine in which pages you should run your
// redirection code. If you only check for the token existence
// then you will be faced with redirection loop. I don't explain in
// depth how to use the $wp_query as it is not part of your question
// but you always have the opportunity to check what is the contents
// of this variable using the code:
//
// echo "<pre>";
// print_r( $wp_query );
// echo "</pre>";
//
// This way you will be able to build your specific if statement
// for the page you like to test.
if (
! isset( $_GET[ 'voluumdata' ] ) ||
empty( $_GET[ 'voluumdata' ] )
) {
wp_redirect( home_url( '/page/to/redirect/' ) );
exit();
}
}
add_action( 'template_redirect', 'redirect_direct_access' );
You can find more information in the WordPress documentation related to the template_redirect hook.
Just in case anyone would face the same issue, #Merianos Nikos has given a half-answer and I mastered it into this:
function redirect_direct_access( ) {
$post_id = get_the_ID();
if (
$post_id == POST_ID &&
!isset( $_GET[ 'voluumdata' ] )
) {
wp_redirect( 'REDIRECT_URL' );
exit();
}
if (
$post_id == POST_ID &&
!isset( $_GET[ 'voluumdata' ] )
) {
wp_redirect( 'REDIRECT_URL' );
exit();
}
}
add_action( 'template_redirect', 'redirect_direct_access' );

Require WordPress Login only for posts, not pages?

I'm trying to create a WordPress site with a login system, but I only want the login to be required to view POSTS. They should be able to view pages without logging in. I did some research, but no one had exactly what I was looking for, so I tried to puzzle something together, and this is what I came up with, but it doesn't work.
function my_force_login() {
global $post;
if ( ( is_single() && !is_user_logged_in() ) ){
auth_redirect();
}
}
Any ideas?
add it to your single.php
<?php auth_redirect(); ?>
This function will check if the user is logged in and if he is not it will redirect him to the login page http://codex.wordpress.org/Function_Reference/auth_redirect
Your code works, but it has run on the correct point:
add_action( 'template_redirect', function()
{
global $post; // Use this to detect the custom post types if needed
if ( ( is_single() && !is_user_logged_in() ) )
{
auth_redirect();
}
});
Details of the post is being displayed in the single.php. Please put the code of displaying post's content into the condition. Then you will get the solution. Try the following code into the single.php in the twentyforteen theme.
if ( is_user_logged_in() ) {
get_template_part( 'content', 'single' );
}
Now get logged out and refresh the browser, you can see none of content are shown.

Resources