add_filter not working on Wordpress custom login page - wordpress

I'm creating a custom login page in wordpress. Everything is good so far but I want to add error handling to my page.
In default-filters.php, I added this:
add_filter('authenticate', 'wp_return_login_error', 40, 3);
In users.php, I added this:
function wp_return_login_error(){
if (is_wp_error($user)) {
$error = $user->get_error_message();
wp_redirect( get_permalink() . "/login/?login=failed&reason=" . $error);
exit;
}
}
No matter what, when you enter invalid/blank credentials, the user is simply redirected to the login screen without any indication of what went wrong. I tried adding debug messages in my function and it looks like it isn't being called.
What should I try?
Thanks in advance for your help.

The easiest way around this is to change the error message with this code in your functions.php file:
function login_error_override()
{
return 'Incorrect login details.';
}
add_filter('login_errors', 'login_error_override');

Related

WordPress admin redirect conundrum

I feel stupid for trying this and not realising what would happen.
Anyway.
Using the wp_redirect function...
if( current_user_can('administrator') ) {
$url = "https://example.com";
wp_redirect( $url );
}
...I want anyone with the role of 'administrator' to be redirected to the homepage when they login. Seems easy enough.
But the problem is that any consequent attempt to go to WP admin (not just after they login, but any time) will result in the administrator being redirected to the homepage effectively 'locking' them out of WP admin.
So I guess my question is, is it possible to redirect an administrator to the homepage when they login, but only immediately after they login? And any consequent attempt to go to /wp-admin/ would let them in?
This code should solve your problem.You can add this code in functions.php.I tested and confirmed its working for me.
function admin_redirection_page() {
return 'https://example.com';
}
add_filter('login_redirect', 'admin_redirection_page');
I think you have to fire the above code in hook wp_login.
Example:
function your_function() {
$url = "https://example.com";
wp_redirect( $url );
die();
}
add_action('wp_login', 'your_function');
Also we have use die() whenever we want to redirect.
Thank You

Give access to only two (/home, /inbox) page for a particular user with specific role in wordpress

I want to give only two page (/home, /inbox) access to my user with role "Vendor", if user tries to access other pages than it will automatically redirect to "/inbox" page, I put the below code to achieve the functionality but after adding this to function.php, site again n again redirect and finally dies with message "Page isn't properly redirected". please suggest what is wrong with my tried code or any other solution.
function vendor_redirect() {
global $post;
if(current_user_can('Vendor') && !in_array($post->slug,array("home","inbox"))) {
wp_safe_redirect('/inbox');
}
}
add_action('template_redirect', 'vendor_redirect');
The main issue the way I tried to get the page slug, the correct way to get the slug is "$post->post_name". also I put exit after wp_safe_redirect as well because codex suggest that:
function vendor_redirect() {
global $post;
if(current_user_can('Vendor') && !in_array($post->post_name,array("home","inbox"))) {
wp_safe_redirect(get_permalink(get_page_by_path( 'inbox' )));
exit;
}
}
add_action('template_redirect', 'vendor_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' );

Redirect away from login page

I'm trying to find a way to prevent the login screen from showing up when user makes a mistake filling out the login form.
So, right now, I have the login form printed on a custom template. But if the users makes a mistake filling it out, it redirects them to the wordpress form to try again. I want to redirect away from that.
This is the code I had but it's not working.
add_action('login_form', 'redirect_invalid_login');
function redirect_invalid_login(){
global $error;
if($error)
header('Location: '.get_bloginfo('url').'/client-login/?message=6');
}
Now login_form is too late I believe because stuff already gets printed to the page. I haven't been able to find a hook that works. Is there a hook for this? Or perhaps a different way to do it?
Okay, so here is the solution:
add_action('login_redirect', 'redirect_login', 10, 3);
function redirect_login($redirect_to, $url, $user) {
if($user->errors['empty_password']){
wp_redirect(get_bloginfo('url').'/client-login/?message=6');
}
else if($user->errors['empty_username']){
wp_redirect(get_bloginfo('url').'/client-login/?message=7');
}
else if($user->errors['invalid_username']){
wp_redirect(get_bloginfo('url').'/client-login/?message=8');
}
else if($user->errors['incorrect_password']){
wp_redirect(get_bloginfo('url').'/client-login/?message=9');
}
else{
wp_redirect(get_bloginfo('url').'/client-login');
}
exit;
}
You can try this
add_action( 'wp_login_failed', 'redirect_invalid_login' );
function redirect_invalid_login() {
wp_redirect(get_bloginfo('url').'/client-login/?message=6');
exit;
}

wordpress login redirect function

I am trying to create my own plugin in wordpress. Everything works great but I want to make a loginredirect check for users who want to access to the plugins page.
Here is my function in my functions.php:
function wpuf_auth_redirect_login() {
$user = wp_get_current_user();
if ( $user->id == 0 ) {
nocache_headers();
wp_redirect(get_option('siteurl') . '/wp-login.php?redirect_to=' . urlencode($_SERVER['REQUEST_URI']));
exit();
}
}
And this is from my myplugin.php:
function wpuf_user_edit_profile() {
wpuf_auth_redirect_login(); // if not logged in, redirect to login page
nocache_headers();
wpuf_post_form_style();
wpuf_user_edit_profile_form();
}
add_shortcode('wpuf_editprofile', 'wpuf_user_edit_profile');
It is not working. If I enter the plugins page as a guest in browser it does not redirect. It only shows the template uncomplete. Any help?
Where you say that it 'only shows the template uncomplete', this could indicate that there is a syntax error in your PHP code, and the server has stopped rendering the page at the point of that error.
I would suggest that you check for any errors by turning error reporting on, and seeing what happens from there.

Resources