Is it possible to login wordpress admin dashboard with defined username and password? - wordpress

Is there any way to login in wordpress admin dashboard with defined username and password for example
<?php
$username = 'my_username';
$password = 'my_password';
if ($_POST['username'] == $username && $_POST['password']){
echo "Valid credentials";
}else{
echo "Invalid credentials";
}
?>

Yes, that's possible, you can do like this by first checking if the creds are valid, then set up user cookies and log them in automatically:
$username = 'my_username';
$password = 'my_password';
$creds = array();
$creds['user_login'] = $username;
$creds['user_password'] = $password;
$creds['remember'] = true;
$user = wp_signon( $creds, false );
if ( !is_wp_error($user) ){
$user_id = $user->ID;
// Log the user in
wp_set_current_user( $user_id, $user_login );
wp_set_auth_cookie( $user_id );
// Perform any required redirections
wp_redirect( home_url() );
exit();
}else{
echo "Invalid credentials";
}
However, make sure that this runs before headers are send to work perfectly, you may hook it to template_redirects for example.

Related

Program to creating WordPress user login system for some defined user roles

I have updated my code, but the problem is get_the_ID not getting any value. Can you please help. This code was adding function.php in child theme.
// Works in single post outside of the Loop
add_filter( 'authenticate', 'myplugin_auth_signon', 30, 3 );
function myplugin_auth_signon( $user, $username, $password ) {
$user = get_user_by( 'login', $username );
$roles = $user->roles['0'];
$id = get_the_ID();
echo $id;
if ( is_page( $id == 400380 ) )
{
echo "Employee Page";
$user = new WP_Error( 'denied', "Customer have no permission to login from this employee login form" );
}
if ( is_page( $id == 399649 ) )
{
echo "Customer";
$user = new WP_Error( 'denied', "Employee have no permission to login from this customer login form" );
}
return $user;
}
You can restrict other role from login use authenticate filter hook.
add_filter( 'authenticate', 'myplugin_auth_signon', 30, 3 );
function myplugin_auth_signon( $user, $username, $password ) {
$user = get_user_by( 'login', $username );
$roles = $user->roles['0'];
if($roles != 'sales' && is_page('YOUR_SALES_PAGE_ID')){
$user = new WP_Error( 'denied', "You have not permission to login from this form" );
return $user;
}
return $user;
}
Replace YOUR_SALES_PAGE_ID with your current sales login page id.
For more help see this link : Click here
User roles comes from database ? check it before, then do your logic.

Woocommerce - Redirect users based on username

I would like to redirect certain usernames to a chosen URL when logged on.
I did try the below but it did not work.
https://www.cozmoslabs.com/docs/profile-builder-2/developers-knowledge-base/redirect/redirect-based-username/
Any help would be appreciated!
I tried the below code in my functions but did not work.
function wc_custom_user_redirect( $redirect, $user ) {
// Get user name
$username = $user->user_login;
// Conditions
switch ( $username ) {
case 'rosinarep':
$redirect = get_permalink( get_page_by_path( '/product-category/gifting/' ) );
break;
default:
$redirect = wp_get_referer() ? wp_get_referer() : home_url();
}
return $redirect;
}
add_filter( 'woocommerce_login_redirect', 'wc_custom_user_redirect', 10, 2 );
}
Can you please try below code snippet in your theme's functions.php file or custom plugin file.
function wc_custom_user_redirect( $redirect, $user ) {
// Get user name
$username = $user->user_login;
// Conditions
switch ( $username ) {
case 'admin':
$redirect = get_permalink( get_page_by_path( 'sample-page' ) );
break;
case 'username1':
$redirect = get_permalink( get_page_by_path( 'sample-page-2' ) );
break;
case 'username2':
$redirect = get_permalink( get_page_by_path( 'sample-page-3' ) );
break;
case 'username3':
$redirect = get_permalink( get_page_by_path( 'sample-page-4' ) );
break;
default:
$redirect = wp_get_referer() ? wp_get_referer() : home_url();
}
return $redirect;
}
add_filter( 'woocommerce_login_redirect', 'wc_custom_user_redirect', 10, 2 );
Also please confirm that you have already created all this redirect pages.
Check below link to get more details about this filter.
http://hookr.io/filters/woocommerce_login_redirect/

wordpress-Add Remember me username on custom login form

I need to add remember me checkbox for username on custom login form.Like when user will fill the username and password and if he check remember me option then i nedd to save his username.I have created a cutom login form and after submit this is my code.but it does not remember username.
$username = sanitize_user($_POST['email']);
$password = $_POST['password'];
$remember = $_POST["rememberme"];
$creds = array();
$creds['user_login'] = $username;
$creds['user_password'] = $password;
$creds['remember'] = $remember;
$user = wp_signon( $creds, false );
//$user1 = apply_filters('authenticate', null, $username, $password);
if ( is_wp_error($user) )
{
echo $user->get_error_message();
}elseif ( get_user_meta( $user->ID, 'has_to_be_activated', true ) != false )
{
$user = new WP_Error('activation_failed', __('<strong>ERROR</strong>: User is not activated.'));
echo $user->get_error_message();
}else{
$secure_cookie = is_ssl() ? true : false;
wp_set_auth_cookie( $user->ID, true, $secure_cookie );
echo "1";
}
Try this documentation.
It will help you.

Wordpress - How to login user to frontend programmatically

I created function for loging in users on frontend using this example: https://gist.github.com/iandunn/8162246
After user logs in is_user_logged_in() function returns true only inside this function where I placed code for login part.
How do I login users globally?
This is my code:
function programmatic_login( $username ) {
if ( is_user_logged_in() ) {
wp_logout();
}
add_filter( 'authenticate', 'allow_programmatic_login', 10, 3 );
$user = wp_signon( array( 'user_login' => $username ) );
remove_filter( 'authenticate', 'allow_programmatic_login', 10, 3 );
if ( is_a( $user, 'WP_User' ) ) {
$user_id = $user->ID;
if( $user ) {
wp_set_current_user( $user_id, $user->user_login );
wp_set_auth_cookie( $user_id );
do_action( 'wp_login', $user->user_login );
}
if ( is_user_logged_in() ) {
return true;
}
}
return false;
}
function allow_programmatic_login( $user, $username, $password ) {
return get_user_by( 'login', $username );
}
function process_login(){
// this comes from login form
$username = $_POST["login_username"];
programmatic_login( $username );
// it returns true only here, on any other function it returns false
if(is_user_logged_in()){
echo "ok";
}else{
echo "not ok";
}
}
This is one example where I try to check if user is logged in, outside previous function:
add_filter('wp_nav_menu_items', 'add_login_logout_link', 10, 2);
function add_login_logout_link($items, $args) {
$loginPage = get_page_by_title("Login");
$registerPage = get_page_by_title("Register");
if(is_user_logged_in()){
$items .= "<li><a href='" . wp_logout_url('index.php') . "' title='Logout'>Logout</a></li>";
}else{
$items .= "<li><a href='". site_url() . '/' . '?page_id=' . $loginPage->ID ."'>Login</a></li><li><a a href='". site_url() . '/' . '?page_id=' . $registerPage->ID ."'>Register</a></li>";
}
return $items;
}
On codex wp_set_current_user there is an example to set the current user and log them in.
$user_id = 12345;
$user = get_user_by( 'id', $user_id );
if( $user ) {
wp_set_current_user( $user_id, $user->user_login );
wp_set_auth_cookie( $user_id );
do_action( 'wp_login', $user->user_login );
}
The simplest fix is to use wp_login_form()
see reference: http://codex.wordpress.org/Function_Reference/wp_login_form
If i understand correctly this should handle everything you want.
This will set the auth cookie and can redirect to the page you want, the login works globally

CodeIgniter and Wordpress Integration

I have a forum developed with CodeIgniter. And I have my website, powered by Wordpress.
Basically what I'm looking to do is when a user registers on the forums, it adds those details to the Wordpress user table, so they're registered on both.
My code for the forum...
/**
* Create new user record
*
* #param array
* #param bool
* #return array
*/
// username email password last_ip key
function create_user($data)
{
$sql = "INSERT INTO users (username,
email,
password,
last_ip,
created,
activated
) VALUES (
?, ?, ?, ?, ?, ?
)";
$this->db->query($sql, array(
$data['username'],
$data['email'],
$data['password'],
$data['last_ip'],
date("Y-m-d H:i:s", utc_time()),
$data['activated']
));
if ($user_id = $this->db->insert_id()) {
$this->create_profile($user_id);
return TRUE;
}
return FALSE;
}
function activate_user($username)
{
$this->db->query("UPDATE users SET activated = 1 WHERE username = ?", $username);
return $this->db->affected_rows();
}
and the Wordpress register class...
/**
* Handles registering a new user.
*
* #param string $user_login User's username for logging in
* #param string $user_email User's email address to send password and add
* #return int|WP_Error Either user's ID or error on failure.
*/
function register_new_user( $user_login, $user_email ) {
$errors = new WP_Error();
$sanitized_user_login = sanitize_user( $user_login );
$user_email = apply_filters( 'user_registration_email', $user_email );
// Check the username
if ( $sanitized_user_login == '' ) {
$errors->add( 'empty_username', __( '<strong>ERROR</strong>: Please enter a username.' ) );
} elseif ( ! validate_username( $user_login ) ) {
$errors->add( 'invalid_username', __( '<strong>ERROR</strong>: This username is invalid because it uses illegal characters. Please enter a valid username.' ) );
$sanitized_user_login = '';
} elseif ( username_exists( $sanitized_user_login ) ) {
$errors->add( 'username_exists', __( '<strong>ERROR</strong>: This username is already registered, please choose another one.' ) );
}
// Check the e-mail address
if ( $user_email == '' ) {
$errors->add( 'empty_email', __( '<strong>ERROR</strong>: Please type your e-mail address.' ) );
} elseif ( ! is_email( $user_email ) ) {
$errors->add( 'invalid_email', __( '<strong>ERROR</strong>: The email address isn’t correct.' ) );
$user_email = '';
} elseif ( email_exists( $user_email ) ) {
$errors->add( 'email_exists', __( '<strong>ERROR</strong>: This email is already registered, please choose another one.' ) );
}
do_action( 'register_post', $sanitized_user_login, $user_email, $errors );
$errors = apply_filters( 'registration_errors', $errors, $sanitized_user_login, $user_email );
if ( $errors->get_error_code() )
return $errors;
$user_pass = wp_generate_password( 12, false);
$user_id = wp_create_user( $sanitized_user_login, $user_pass, $user_email );
if ( ! $user_id ) {
$errors->add( 'registerfail', sprintf( __( '<strong>ERROR</strong>: Couldn’t register you... please contact the webmaster !' ), get_option( 'admin_email' ) ) );
return $errors;
}
update_user_option( $user_id, 'default_password_nag', true, true ); //Set up the Password change nag.
wp_new_user_notification( $user_id, $user_pass );
return $user_id;
}
I don't need all the extra bumph of the Wordpress class, I'm just looking for a barebones forum values addition to the Wordpress user table.
Any help?
Two database configs will be required in your codeigniter config file:
$db['wordpress']
$db['default'] //priority
To use the wordpress DB assign it a var in your main controller.
$this->wordpress_db = $this->load->database('wordpress', TRUE);
//Normal query
$this->db->query();
//wordpress Query
$this->wordpress_db->query();
The rest should become obvious

Resources