WordPress redirect to page if taxonomy is empty - wordpress

I have a taxonomy option called "tipo_coche", tipo_coche contains convertible, coupe, compacto, etc.. All created with CPT UI, some can be empty. I need an automatic reirect to page "/coches-segunda-mano-ocasion" when they do not contain elements.
I have tried with this code but without success.
function custom_redirector(){
if (isset($_GET["s"]) and $_GET["s"]=='' and !empty($_SERVER['HTTP_REFERER'])){
if (strpos($_SERVER['HTTP_REFERER'],'tipo_coche')!==false){
header("location: ".home_url('/coches-segunda-mano-ocasion')); exit;
}
}
}

You could take a look at the function: wp_safe_redirect() like so:
wp_safe_redirect( $url );
exit;

Related

Wordpress: which function is called when user visit /?p=randomstring

Wordpress at the moment return a random page when visiting, for example, https://www.examplewebsite.com/?p=bdmvxmqa
At the moment it doesn't return a 404 and I'd like to correct it. But I cuouldn't find where to look into the code.
Wordpress usually redirects to the closest matching page title. (at least it used to way back when I was working with WordPress).
Try adding this line to functions.php to turn off this feature:
remove_filter('template_redirect', 'redirect_canonical');
If this is an old URL structure then you could redirect all ?p requests to your homepage or similar with the below function:
function rusty_redirect_query() {
global $wp;
$wp->add_query_var('p');
if(get_query_var('p')) {
wp_redirect( home_url( '/page/to/redirect/' ) );
}
exit();
}
add_action( 'template_redirect', 'rusty_redirect_query' );

How do I redirect user who adds "author/admin" to my domain name?

If you type "author/admin" after a wordpress website domain name (ie "mywebsite.com/author/admin") it will show you a list of all articles posted by the admin. I would like a user to be redirected to the homepage if they try to access this page.
I wanted to do redirect anyone who types in "2016" or "2017" as well. For example, if someone goes to my website and types in "mywebsite.com/2017" they would normally see a list of all articles from 2017 but I added this code to functions.php that now redirects them to the home page:
function redirect_to_home( $query ){
if(is_date() ) {
wp_redirect( home_url() );
exit;
}
} add_action( 'parse_query', 'redirect_to_home' );
If I change the "is_date" portion of that code to "is_author" it redirects the user anytime they type in "author/admin" but I can't figure out how to do both. I've tried adding two sets of code, one with "is_date" and one with "is_author" but I get a wordpress error when I try to save it. Is there a way to combine "is_date" and "is_author" into one set of code to redirect users in both cases?
You need || (or) comparison operator:
function redirect_to_home( $query ){
if(is_date() || is_author()) {
wp_redirect( home_url() );
exit;
}
} add_action( 'parse_query', 'redirect_to_home' );
Also have a look here for more comparison operators: https://www.w3schools.com/php/php_operators.asp

How to create Custom Pagination Permalinks in wordpress

I have an article with several pages in my wordpress blog. if for example i have the following link in my blog :
http://example.com/heartbreaking-photos
any idea how can i change the link of the second page from
http://example.com/heartbreaking-photos/2
to http://example.com/heartbreaking-photos/CUSTOM-STRING
CUSTOM-STRING aimed to be a custom title inside the page
To achieve this, you will need to do 2 things:
Disable the default WordPress canonical redirect - this is necessary, because WordPress will always redirect to the /2/ page when it encounters the page parameter in the URL or query args.
Add a custom rewrite rule to direct your custom title to the second page of your page - this is essentially necessary to allow the link format that you want.
In terms of code, this is what you need (this is a working solution, I've just tested it locally):
// Removes the canonical redirection
remove_filter( 'template_redirect', 'redirect_canonical' );
// Add custom rewrite rules
add_action( 'init', 'my_add_custom_rewrite_rules' );
function my_add_custom_rewrite_rules() {
// Slug of the target page
$page_slug = 'heartbreaking-photos';
// Page number to replace
$page_num = 2;
// Title you wish to replace the page number with
$title = 'custom-string';
// Add the custom rewrite rule
add_rewrite_rule(
'^' . $page_slug . '/' . $title . '/?$',
'index.php?pagename=' . $page_slug . '&page=' . $page_num, 'top'
);
}
There are three things you might want to configure or change here:
$page_slug - this is the slug of your page, in your case this is heartbreaking-photos
$page_num - the number of your pagination page, in your case this is 2
$title - the title you wish to use instead of your page number 2.
Feel free to alter the code as you wish, or copy it to cover more additional cases, similar to this one.
EDIT
Important: Once you use the code, go to Settings > Permalinks and click the "Save Changes" button. This will rebuild your rewrite rules, and is necessary for the solution to work.
Hope that helps. Let me know if you have any questions.
You can try this codex. Pass the arg and you will get page id, page title and use those
https://codex.wordpress.org/Function_Reference/get_pages
Or you can call page title by page id
$pagetitle= get_post_field( 'post_title', $page_id );
Ok, so basically you don't want to display the navigation link under the page (use css or modify the post template in the child theme) and add your custom link. If I understand it well:
Remove navigation links (depends on your theme, but basically):
.nav-links { display: none; }
You can add the custom link through function + custom fileds:
create a custom field, for example "my-url" in your post, see codex: https://codex.wordpress.org/Custom_Fields
add to your functions.php (in the child theme or in a custom site plugin):
function my_page_add_to_content( $content ) {
if ( ! empty(get_post_meta( get_the_ID(), 'my-url', true ) ) {
$content .= 'URL TEXT HERE'
}
return $content;
}
add_filter( 'the_content', 'my_page_add_to_content' );

How to get the whole html content when visiting a page of splited wordpress post

I am doing string conversion for every post/page in Wordpress. I add below function to template_redirect hook to get the whole html content and do the conversion.
function my_string_conversing() {
obstart( 'ob_callback' );
return;
}
function ob_callback( $buffer ) {
// doing conversion here...
return $buffer
}
add_action( 'template_redirect', 'my_string_conversion' );
These code works well for posts and pages, but if I split one post into multiple pages, then this will not work. It even don't run my_string_conversion function when click each page of a split post. How can I make it work? Thanks.
I found the solution, use add_action( 'template_redirect', 'my_string_conversion', -100 ); instead.

Hide page for logged in Users

I wanna do the following:
If a user is logged out, he can view the page id=10, if a user is logged in and view page id=10 he will be redirected to page id=5. I tried adding the below code into my header, but it didn't work.
add_action( 'init', 'check_redirect_page' );
function check_redirect_page() {
if ( is_user_logged_in() && is_page( 10 ) ) {
wp_redirect( get_permalink( 5 ) );
exit;
}
}
Try using the wp hook rather than init; WordPress won't have got far enough at init to know whether it's dealing with a particular page or not.
add_action( 'wp', 'check_redirect_page' );
From the Conditional Tags documentation:
Warning: You can only use conditional query tags after the
posts_selection action hook in WordPress (the wp action hook is the
first one through which you can use these conditionals). For themes,
this means the conditional tag will never work properly if you are
using it in the body of functions.php, i.e. outside of a function.

Resources