Redirect outside WP after Login - wordpress

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!

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

Wordpress Rewriting Rules

I have a Wordpress Multisite installation and I add custom post type named course in blog 1 and then I select the other blogs where I want see it.
In other blogs add a page that I use like single course template.
The url of this page is
http://example.com/blogName/course/?course-name=lorem-ipsum&course-id=xx
I would like to have a url like this
http://example.com/blogName/course/lorem-ipsum
and i would like to get the course-id parameter in template page.
I tried to use add_rewrite_rule but i'm not able to do what i want.
add_filter('query_vars', 'my_query_vars', 10, 1);
function my_query_vars($vars) {
$vars[] = 'course-name';
return $vars;
}
add_action( 'init', 'init_custom_rewrite' );
function init_custom_rewrite() {
add_rewrite_rule(
'^course/([^/]*)/?','index.php?course-name=$matches[1]','top');
}
How can I do this?
I need to add something to .htaccess?
Once you've executed the above hooks, find a way to call the flush rewrite function which will update the rewrite cache so it should start working.
flush_rewrite_rules( true );
Documentation for this function can be found on the developer docs site.
You can indirectly call that function too by just saving the Permalinks settings in the dashboard by going to Settings > Permalinks.

How to redirect author URL query to buddypress user profile page in 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' );

How to redirect based on URL

I am using Wp Store Locator plugin.And I have Modified it according my need.But I am stuck in redirection.
Basically this plugin works with short code.So at the time of listing my URL is like that : localhost/wordpress/?page_id=391
Now there is one link which is redirects me to http://localhost/wordpress/?wpsl_id=3
Here is code for that :
add_action("template_redirect", 'my_theme_redirect');
function my_theme_redirect() {
$dpath = $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"] ;
$templatefilename = 'single-store.php';
if (strpos($dpath,'wpsl_id') !== false){
$return_template = get_template_directory() .'/' .$templatefilename ;
//$nurl = get_permalink($return_template);
//echo $return_template; exit;
wp_redirect($return_template);
exit;
}
}
This code is not redirecting me to any-where.It stays on the same page localhost/wordpress/?page_id=391 What might be the issue?
Anyone can suggest me how can I direct to URL when it detects wpsl_id in the URL ? I want to redirect them to plugin's template folder where I have added a file for that.
EDITED :
I have added above code in the plugin file just above the short code. Hope I has nothing to do with this issue :)
The template name looks fine. You just need to register the template before you call it.
Add this filter so this will execute before your action:
add_filter( 'template_include', 'single-store' );

rewrite rule wordpress not working

This quetsion has been ask al lot, but however I tried I get always a 404 page in WP
I have a templatepage (wp-content/themes/responsivewizzard.php cals: wizzard. ->
I Added a page in the admin sector. The name of the page = "tespage". The template I choose is "wizzard"
When in try example.com/wp/testpage -> I get my wizzard page. No worries.. but..
I like to add a subpage to my wizzardpage. For example:
example.com/wp/testpage/nice
When i try this I get a 404 page from WP
I added this code to my function.php file. This file is in wp-content/themes/responsive directory -> my default theme
function members_rewrite_rules()
{
add_rewrite_rule('testpage /(.+)/?$', 'index.php?pagename=testpage &subpage=$matches[1]', 'top');
}
add_action( 'init', 'members_rewrite_rules' );
function members_query_vars($query_vars)
{
$query_vars[] = 'subpage';
return $query_vars;
}
add_filter('query_vars', 'members_query_vars');
Please help me
I found out the soulution. You need to reset the permalinks in the setting menu..

Resources