php - using wp_authenticate() to redirect certain users when logging in -
our website using wordpress - woocommerce login page customers login.
i trying use wp_authenticate() achieve following:
1) customer login our new website, enter username , password, , hit login button. in case want see woocommerce login file, click here.
2) our new website goes through list see if username matches. if username matches, don't @ password, redirect user other url such google.com
3) if username doesn't match our list, let them login usual.
with jquery, helped me come following code:
var names = new array(”bill”, ”jim”, ”bob”); // names array, , in uppercase var dest_url = ”http://www.website.com”; // url want send them jquery(document).ready(function () { jquery(”input[name=’login’]”).click(function(event){ event.preventdefault(); // prevent default form action var current_name = jquery(”#username”).val(); current_name = current_name.trim().touppercase(); if ( -1 != jquery.inarray(current_name, names) ) { alert(”redirecting ” + current_name + ” ” + dest_url); window.location = dest_url; // send desired url } else document.getelementsbyclassname(”login”)[0].submit(); // input name not on our list, normal submit action }); });
but not sure if wp_authenticate() can contain jquery script inside. suggestion appreciated.
first, recommend doing in php, not javascript.
second, have couple of options, leveraging built-in functionality of wordpress.
if care username, , not care if logged in right password, leverage filter found in wp_authenticate()
// filter wp_authenticate fires apply_filters( 'authenticate', null, $username, $password );
knowing that, write quick little plugin, or add code theme's functions.php file:
// run filter priority 9999 (last, or close last), after core wp filters have run add_filter('authenticate', 'redirect_certain_users', 9999, 3); // custom filter function function redirect_certain_users( $user, $username, $password) { // assumes write function called get_list_of_users_to_redirect returns array similar in sample code $redirect_users = get_list_of_users_to_redirect(); // if user in array of users redirect, send them away if ( in_array( $username, $redirect_users ) ) { header("location:http://www.example.com"); die(); } return $user; }
note code untested, should @ least 90% of way there.
Comments
Post a Comment