How to redirect author URL query to buddypress user profile page in Wordpress - wordpress

I am using Buddypress and a plugin called Teambooking. The Teambooking plugin doesn't know of the existence of Buddypress and it uses the default Wordpress author query string (/?author="id", where id is the user id number) to create and display a link to an author's page.
But Buddypress provides profile pages for users.
So I want to be able to catch when someone clicks on the standard author page link (/?author="id") and re-direct the page to Buddypress user profile page. I thought I could use the hook 'template-redirect' to catch this event, but that doesn't work, because the hook only get's triggered when the destination page is being loaded, and what is happening is that Wordpress is automatically redirecting the URL that has the /?author="id" query string to the index.php page, because there is no author.php page.
I cannot use $_SERVER["QUERY_STRING"] either to parse the URL , because as I said, Wordpress automatically redirects to index.php, dropping the query string.
I also was thinking that I could create an author.php page, so that WordPress stops redirecting to index.php, and then use 'template_redirect' hook to redirect the author.php to the buddypress profile page, but that didn't work either. I created author.php on my theme's directory but Wordpress continues to redirect to index.php.
Any ideas on how to this redirection from the /?author="id" query to the proper buddypress profile page of the user?
This must happen all the time with plugins that are not aware of Buddypress being installed .
thanks
-Malena

It turns out that the free version of Yoast SEO plugin was redirecting a query for the author page (?author=id) to index.php automatically, and I wasn't aware of it. So I had to deactivate Yoast SEO plugin, and then used is_author() to detect when the author archive page was being requested (?author=id query string in URL ) inside a template-redirect hook in order to redirect the call to the appropriate BuddyPress user profile. Here is the code I used in functions.php, which does the redirection:
/* Redirect author page to BuddyPress page */
function my_page_template_redirect()
{
/** Detect if author archive is being requested and redirect to bp user profile page */
if( is_author() )
{
global $wp_query;
if(isset($wp_query->query_vars['author'])) {
$userID = urldecode($wp_query->query_vars['author']);
}
$url = bp_core_get_user_domain($userID);
wp_redirect( $url );
exit();
}
add_action( 'template_redirect', 'my_page_template_redirect' );

Buddypress doesn't work with the default permalink structure, so it probably isn't the author id that you're looking for. If your permalink structure were set to Post name for instance, then this would work.
/* Redirect author page to buddypress page */
function my_page_template_redirect()
{
if( is_author() )
{
global $wp_query;
$user = get_user_by( 'slug', $wp_query->query['author_name'] );
$url = bp_core_get_user_domain($user->ID);
wp_redirect( $url );
exit();
}
}
add_action( 'template_redirect', 'my_page_template_redirect' );

Related

How to redirect user to another page if the user is logged in - WordPress Elementor Page Builder

I have created a registration form using Elementor Page Builder. Now, I want to redirect the user to a different page if he/she is trying to access that registration page after logging in.
Is there any Elementor hook available for that? I know the WordPress function called is_user_logged_in().
function my_logged_in_redirect() {
if ( is_user_logged_in() && is_page( 12 ) )
{
wp_redirect( get_permalink( 32 ) );
die;
}
}
add_action( 'template_redirect', 'my_logged_in_redirect' );
You should get the ids of the page where the form is and the id of the page you want to redirect the user to.
Code goes in your child theme functions.php file
Reference: here
The 'Content Area Not Found' error might appear on Elementor designed sites when you use that snippet and try to edit page of ID 12 (in your example) in certain cases.
To avoid this, add the following code before the if-statement of your snippet:
if ( \Elementor\Plugin::$instance->preview->is_preview_mode() ) {
return;
}

Unlogged user cannot see store and must go to login and successfully logged in users must be redirected to store

I am working on a Wordpress site with Woocommerce. I am using this function inside functions.php to redirect everyone who visits my store to login since it is a private area. Once the person logs in, they should be redirected to the store again. I have the following code implemented, which works for me to get all those trying to navigate my store to log in, I need to modify this code so that once logged in, it redirects me to the store page in woocommerce, in my case the page of the store is called "tienda".
To clarify: Users who are not registered or logged in, should not be able to see the store or cart or anything from woocommerce, I already implemented this with a wp-members plugin, if they try to navigate these woocommerce urls, they must go to login. When you have already logged in, you should go to the store, in my case you should not redirect to the previous link, but must inevitably go to the page called "tienda".
On the other hand, I would like to know if my function is missing something or is well built. Thank you!
function loggedoutuser_redirect() {
if (
! is_user_logged_in()
&& (is_woocommerce() || is_cart() || is_checkout())
) {
header('Location: ' . wp_login_url());
exit;
}
}
add_action('template_redirect', 'loggedoutuser_redirect');
function woocommerce_login_redirect_custom( $redirect, $user ) {
$redirect = wc_get_page_permalink( 'shop' );
return $redirect;
}
add_filter( 'woocommerce_login_redirect', 'woocommerce_login_redirect_custom', 10, 2 );
Paste this above snippet at the end of your active child theme functions.php file or else if you are good in plugin development then create a custom plugin.
Snippet Explanation:
Create a custom function to redirect after logged in
Fetch the shop URL using WooCommerce helper function wc_get_page_permalink( 'shop' ) and assign it to the variable $redirect
Then return the URL
Hook the custom function to the filter hook woocommerce_login_redirect
Note: You can check the user role inside our custom function then based on the role you can redirect it to the dashboard if he is administrator else to the shop page.

How to disable Woocommerce login redirect if on Checkout Page

I have some code in my WordPress functions.php file to do a redirect after a successful woocommerce login. It works great, but I'm wondering how I can disable that redirect from running on the checkout page?
If a user logs into their account while on the checkout page they are getting redirected off the checkout page just before they have the chance to fill in their credit card data which isn't a great experience.
According to https://docs.woocommerce.com/document/conditional-tags/ conditional query tags like is_checkout() won't work in the functions.php file because You can only use conditional query tags after the posts_selection action hook in WordPress. And, unfortunately, apparently the functions.php file gets run before that.
What's my best option to solve this?
Here's the code I currently have running (the one I want to disable on checkout page):
function woo_login_redirect( $redirect_to ) {
$redirect_to = get_permalink(70241);
return $redirect_to;
}
add_filter('woocommerce_login_redirect', 'woo_login_redirect');
This is me trying to avoid the redirect if on checkout page (it doesn't work. It just redirects to post id 70241):
function woo_login_redirect( $redirect_to ) {
if (is_checkout()){
$redirect_to = '#';
} else {
$redirect_to = get_permalink(70241);
}
return $redirect_to;
}
add_filter('woocommerce_login_redirect', 'woo_login_redirect');

Wordpress remove permalink from custom posts but retain archive

I have a Wordpress site where I keep track of publications with an archive of custom post types called "publications". Each publication post shouldn't have its own page, it just needs to appear in the archive page. Right now when I create a publication called "test publication" there is a page created at www.mysite.com/publications/test-publication. I've seen the suggestion of changing the post option public to false, but then I can't access the archive itself, it just redirects to the home page. If I add publicly_queryable => true in addition to public => false then I can get to the archive page, but the dedicated page for each publication shows up again. I need it to give me a 404 if I try to visit www.mysite.com/publications/test-publication but still allow me to access the archive. Help, am I missing something obvious?
You can have template redirect added so that singular link if accessed redirects to Archive page:
add_action( 'template_redirect', 'disable_singular_publications' );
function disable_singular_publications()
{
if ( ! is_singular( 'publications' ) )
return;
wp_redirect( get_post_type_archive_link( 'publications' ), 301 );
exit;
}
You may add above function in functions.php , code is not tested so you may need to check on any typos or syntax errors.

Redirect outside WP after Login

I have http://mysite.com/admin.php
There I check wether the user is admin or not.
In second case I send the user to the wp-login page like this:
blog.mysite.com/wp-login.php?redirect_to=http%3A%2F%2Fmysite.com/admin.php
I expect redirect back for admin.php but wordpress always send me to wp-admin control panel.
I have researched.
When the dest. host is not in filter
allowed_redirect_hosts
WP just redirect the user to wp-admin.
How can I add more hosts to the filter?
If I put this example from the WP Codex on functions.php it stops working.
(http://codex.wordpress.org/Plugin_API/Filter_Reference/allowed_redirect_hosts)
add_filter( 'allowed_redirect_hosts' , 'my_allowed_redirect_hosts' , 10 );
function my_allowed_redirect_hosts($content){
$content[] = 'blog.example.com';
$content[] = 'codex.example.com';
// wrong: $content[] = 'http://codex.example.com';
return $content;
}
Add the following in your functions.php:
function my_allowed_redirect_hosts($allowed_host) {
$allowed_host[] = 'anothersite.com';
$allowed_host[] = 'www.someotherwebsite.com';
return $allowed_host;
}
add_filter('allowed_redirect_hosts','my_allowed_redirect_hosts');
Replace anothersite.com and add new values accordingly.
If you're trying to redirect users in a normal page, you can make use of Wordpress's wp_redirect() function:
<?php
wp_redirect( $location, $status );
exit;
?>
Documentation: wp_redirect()
Hope this helps!
Finally it works!
What I was doing wrong is putting the code in the WP functions.php file, and not in my custom theme functions.php file.
Thanks all!

Resources