Restrict WP User from WP backend logging and profile edit

Restrict WordPress user from Wp back end admin panel login and also restrict from edit profile in back end side.

 

just put below few line code in your active theme funcstions.php file

<?php

add_action( ‘admin_init’, ‘custom_wpadmin_blockusers_init’ );
function custom_wpadmin_blockusers_init() {
if ( !current_user_can( ‘activate_plugins’ ) ) {
wp_redirect( home_url() );
exit;
}
}
show_admin_bar(false);

?>

Wp Backend side add additional field in User profile add edit

To add additional field in WordPress admin backend side User profile add edit screen add below scripting in your current using Wp theme functions.php file

 

<?php

/* start Back end extra User profiel field */

add_action( ‘show_user_profile’, ‘extra_user_profile_fields’ );
add_action( ‘edit_user_profile’, ‘extra_user_profile_fields’ );
add_action( “user_new_form”, “extra_user_profile_fields” );

function extra_user_profile_fields( $user ) {
$auto_approve = get_the_author_meta( ‘auto_approve’, $user->ID);
$user_verify_status = get_the_author_meta( ‘user_verify_status’, $user->ID);
?>
<h3><?php _e(“Extra profile information”, “blank”); ?></h3>
<table class=”form-table”>
<tr>
<th><label for=”auto_approve”><?php _e(“Auto Approve Post”); ?></label></th>
<td>
<select name=”auto_approve” id=”auto_approve” style=”width:180px”>
<option value=”0″ <?php selected( $auto_approve, 0); ?>>No</option>
<option value=”1″ <?php selected( $auto_approve, 1); ?>>Yes</option>
</select>
</td>
</tr>
<tr>
<th><label for=”user_verify_status”><?php _e(“Ac Verification”); ?></label></th>
<td>
<select name=”user_verify_status” id=”user_verify_status” style=”width:180px”>
<option value=”inactive” <?php selected( $user_verify_status, ‘inactive’); ?>>Not Verified</option>
<option value=”active” <?php selected( $user_verify_status, ‘active’); ?>>Verified</option>
</select>
</td>
</tr>

</table>
<?php }

add_action( ‘personal_options_update’, ‘save_extra_user_profile_fields’ );
add_action( ‘edit_user_profile_update’, ‘save_extra_user_profile_fields’ );
add_action( ‘user_register’, ‘save_extra_user_profile_fields’ );

function save_extra_user_profile_fields( $user_id ) {
if ( !current_user_can( ‘edit_user’, $user_id ) ) { return false; }
update_user_meta( $user_id, ‘auto_approve’, $_POST[‘auto_approve’] );
update_user_meta( $user_id, ‘user_verify_status’, $_POST[‘user_verify_status’] );
}

 

?>

WP Backend side profile edit required email error remove

To hide / remove Wp profile edit error as required field value not enter add below few line scripting in your current theme functions.php file

<?php

// remove empty required email error from backend user profile edit
function wpse_ls_empty_email_error( $arg ) {
if ( !empty( $arg->errors[’empty_email’] ) ) unset( $arg->errors[’empty_email’] );
}
add_action( ‘user_profile_update_errors’, ‘wpse_ls_empty_email_error’ );

?>

 

When it help full

In wordpress website when you allow user to register without email id and in some cases User Administrator need to edit user profile from WP back end site.

 

WP user login by Email id and user name without plugin

In WordPress to make it easy for User as Login by user name as well as by email id is very easy by just adding below full lines code in your active theme functions.php files.

<?php
function login_via_email_address($username) {
$user = get_user_by(’email’,$username);
if(!empty($user->user_login))
$username = $user->user_login;
return $username;
}
add_action(‘wp_authenticate’,’login_via_email_address’);

function change_username_with_email_wps_login_text($text){
if(in_array($GLOBALS[‘pagenow’], array(‘wp-login.php’))){
if ($text == ‘Username’){$text = ‘Username / Email’;}
}
return $text;
}
add_filter( ‘gettext’, ‘change_username_with_email_wps_login_text’ );

?>

Adding Extra Fields to the WordPress User Profile

With the following few lines php scripting code, you will able to show edit and update user additional information that store in user meta table.

Below script add one drop down in WP admin back end site profile edit screen and on update action it save value to user meta table.
To see its awesome work Just add the below code to your theme’s functions.php file.

/* start Back end extra User profile field */
add_action( ‘show_user_profile’, ‘extra_user_profile_fields’ );
add_action( ‘edit_user_profile’, ‘extra_user_profile_fields’ );

function extra_user_profile_fields( $user ) {
$auto_approve = get_the_author_meta( ‘auto_approve’, $user->ID);
?>

<option value="0" >No
<option value="1" >Yes

<?php }

add_action( 'personal_options_update', 'save_extra_user_profile_fields' );
add_action( 'edit_user_profile_update', 'save_extra_user_profile_fields' );

function save_extra_user_profile_fields( $user_id )
{
if ( !current_user_can( 'edit_user', $user_id ) ) { return false; }
update_user_meta( $user_id, 'auto_approve', $_POST['auto_approve'] );
}

User Last Login with sorting in WordPress

// Save wp user last login time in User meta

function update_user_lastlogin($user_login ) {
$user = get_user_by( ‘login’, $user_login );
update_user_meta( $user->ID, ‘wp-last-login’, time());
}

add_action(‘wp_login’,’update_user_lastlogin’);

function add_lastlogin_user_column($columns) {
return array_merge( $columns, array(‘wp-last-login’ => __(‘Last Login’,’wp-last-login’)) );
}
add_filter(‘manage_users_columns’ , ‘add_lastlogin_user_column’);

function manage_lastlogin_status_column( $value, $column_name, $user_id )
{
if ( ‘wp-last-login’ == $column_name ) {
$value = __( ‘Never.’, ‘wp-last-login’ );
$last_login = (int) get_user_meta( $user_id, ‘wp-last-login’, true );

if ( $last_login ) {
$format = apply_filters( ‘wpll_date_format’, get_option( ‘date_format’ ) );
$value = date_i18n( $format, $last_login );
}
}
return $value;
}
add_filter(‘manage_users_custom_column’, ‘manage_lastlogin_status_column’, 10, 3);

function add_sortable( $columns ) {
$columns[‘wp-last-login’] = ‘wp-last-login’;
return $columns;
}
add_filter( ‘manage_users_sortable_columns’,’add_sortable’ );

function wpse_ls_pre_user_query($user_search) {
global $wpdb,$current_screen;
if ( ‘users’ != $current_screen->id )
return;
$vars = $user_search->query_vars;
if(‘wp-last-login’ == $vars[‘orderby’]){
$user_search->query_from .= ” INNER JOIN {$wpdb->usermeta} m1 ON {$wpdb->users}.ID=m1.user_id AND (m1.meta_key=’wp-last-login’)”;
$user_search->query_orderby = ‘ ORDER BY UPPER(m1.meta_value) ‘. $vars[‘order’];
}
}

add_action(‘pre_user_query’,’wpse_ls_pre_user_query’);

Get Remaining Time from given Expire future date

function ls_remaining_time( $expire_timestamp, $left_red = ‘right’ ){
if( !empty( $expire_timestamp ) && $expire_timestamp > -1 ){
$diff = $expire_timestamp – current_time( ‘timestamp’ );

if( $diff > 0 ){

$secondsInAMinute = 60;
$secondsInAnHour = 60 * $secondsInAMinute;
$secondsInADay = 24 * $secondsInAnHour;

/* extract days */
$days = floor( $diff / $secondsInADay );

/* extract hours */
$hourSeconds = $diff % $secondsInADay;
$hours = floor( $hourSeconds / $secondsInAnHour );

/* extract minutes */
$minuteSeconds = $hourSeconds % $secondsInAnHour;
$minutes = floor( $minuteSeconds / $secondsInAMinute );

/* extract the remaining seconds */
$remainingSeconds = $minuteSeconds % $secondsInAMinute;
$seconds = ceil( $remainingSeconds );

if( $days > 0 ){
if( $days == 1 ){
$remaining = ‘1 ‘.__( ‘day’, ‘couponxl’ );
}
else{
$remaining = $days.’ ‘.__( ‘days’, ‘couponxl’ );
}
}
else if( $hours > 0 ){
if( $hours == 1 ){
$remaining = ‘1 ‘.__( ‘hour’, ‘couponxl’ );
}
else{
$remaining = $hours.’ ‘.__( ‘hours’, ‘couponxl’ );
}
}
else if( $minutes > 0 ){
if( $minutes == 1 ){
$remaining = ‘1 ‘.__( ‘minute’, ‘couponxl’ );
}
else{
$remaining = $minutes.’ ‘.__( ‘minutes’, ‘couponxl’ );
}
}
else if( $seconds > 0 ){
if( $seconds == 1 ){
$remaining = ‘1 ‘.__( ‘second’, ‘couponxl’ );
}
else{
$remaining = $seconds.’ ‘.__( ‘seconds’, ‘couponxl’ );
}
}
}
else{
$remaining = __( ‘Expired’, ‘couponxl’ );
}
}
else{
$remaining = __( ‘Unlimited Time’, ‘couponxl’ );
}

if( $left_red == ‘right’ ){
return __( ‘Expires in: ‘, ‘couponxl’ ).'<span class=”red-meta”>’.$remaining.'</span>’;
}
else{
return ‘<span class=”red-meta”>’.__( ‘Expires in: ‘, ‘couponxl’ ).'</span>’.$remaining;
}
}