WordPress additonal check while login - wordpress

When a user register in my site I have set the user_activation_key column value from wp_users table like that:
$code = sha1( $user_id . time() );
global $wpdb;
$wpdb->update(
$wpdb->prefix.'users', //table name
array( 'user_activation_key' => $code ),
array( 'ID' => $user_id ),
array( '%s' ),
array( '%d' )
);
It's because I want to make activation system by sending an email with clickable link:
$activation_link = add_query_arg(
array(
'key' => $code,
'user' => $user_id
), get_permalink( 44 )
);
$message = "<div style='padding : 20px; border : 1px solid #ddd; color : #000;'>Hello $surname, <br/><br/>Please confirm your email addresss . Click this link to confirm : <a href='$activation_link'>Confirm Now</a><br/><br/></div>";
$to = $email;
$subject = 'Confirm your registration process"';
$body = $message;
$headers = array('Content-Type: text/html; charset=UTF-8');
wp_mail( $to, $subject, $body, $headers );
Now, the column user_activation_key has the hash code and user_status column value is 0
Now the actual question:
When user go to www.site.com/wp-admin that means login page I want to show an error message if the user_status column value is 0.
I don't' have any idea which hook or how can I cehck this while user login?

Use the admin_notices hook to display messages in the backend, the code below is similar to what your after:
add_action('admin_notices', 'account_activation_check');
function account_activation_check() {
global $wpdb;
// setup vars //
$currentID = get_current_user_id();
$user = get_user_by( 'ID', $currentID );
$userStatus = $user->user_status;
// check if user status is 0 //
if($userStatus == 0) {
echo '<div class="error"><p>Your email has not been verified!</p></div>';
}
}

I have solved the issues by using the wp_authenticate_user hook. Here is the code:
add_filter( 'wp_authenticate_user', 'shibbir_authenticate_user', 10, 2 );
function shibbir_authenticate_user( $user ) {
if ( $user->data->user_status == 0 ) {
return new WP_Error( 'error', __( 'Your account is not activate, Please contact site admininstrator.' , 'shibbir' ) );
}
return $user;
}

Related

WooCommerce Shortcode For "Order Again" Customer's Last Order

I'm fairly new with this, but I am learning. I am trying to create a shortcode which can be inserted anywhere on the site, allowing the customer to order their previous order again.
In other words, I need to enable the customer to "order again" their last order. Main problem is, when I try this, the site goes blank (white screen).
Any ideas what's wrong here?
add_shortcode( 'order_again', 'matt_order_again' );
function matt_order_again( $order ) {
if ( is_user_logged_in() ) {
$user_id = get_current_user_id();
$customer = new WC_Customer( $user_id );
$last_order = $customer->get_last_order();
if ( $last_order->has_status( 'completed' ) ) {
$actions['order-again'] = array(
'url' => wp_nonce_url( add_query_arg( 'order_again', $order->get_id(), wc_get_cart_url() ), 'woocommerce-order_again' ),
'name' => __( 'Order again', 'woocommerce' ),
);
}
return $actions;
}
}
I have tried adding the global variable for WC.
try this :
add_shortcode( 'order_again', 'matt_order_again' );
function matt_order_again( $order ) {
if ( is_user_logged_in() ) {
$user_id = get_current_user_id();
$customer = new WC_Customer( $user_id );
$last_order = $customer->get_last_order();
if ( $last_order->has_status( 'completed' ) ) {
$actions['order-again'] = array(
'url' => wp_nonce_url( add_query_arg( 'order_again', $last_order->get_id(), wc_get_cart_url() ), 'woocommerce-order_again' ),
'name' => __( 'Order again', 'woocommerce' ),
);
}
return $actions;
}
}
Or if you want to return a button with url for order again using shorcode use this code (edited).
add_shortcode('order_again', 'matt_order_again');
function matt_order_again($order)
{
if (is_user_logged_in()) {
global $woocommerce;
ob_start();
$user_id = get_current_user_id();
$customer = new WC_Customer($user_id);
$last_order = $customer->get_last_order();
if(!$last_order){
return;
}
if ($last_order->has_status('completed')) {
$url = wp_nonce_url(add_query_arg('order_again', $last_order->get_id(), wc_get_cart_url()), 'woocommerce-order_again');
echo '' . __('Order Again', 'woocomerce') . '';
}
$contents = ob_get_contents();
ob_end_clean();
return $contents;
}
}

Gravity form | Get the sender email to fire a notification

In order to send a post form submission notification with wp_mail(), I would like to get the sender email.
I have created an hidden field into my form where I can get the user logged email.
What is the function I can use to get the user logged email to send the notification ?
The hidden field ID into the form is 4.
I have created a variable :
$subscribers = rgar( $entry, '4' );
But it's not working...
My code to send the notification :
add_action( 'gform_after_submission', 'notification_mail', 10, 2 );
function notification_mail( $entry, $form ) {
$created_posts = gform_get_meta( $entry['id'], 'gravityformsadvancedpostcreation_post_id' );
foreach ( $created_posts as $post )
{
$post_id = $post['post_id'];
$subscribers = rgar( $entry, '4' );
$subject = 'Merci';
$message = "<p>Bonjour,</p>
<p>Merci d'avoir rempli notre formulaire</p>";
$headers = array('Content-Type: text/html; charset=UTF-8','From: SMP <noreply#smp.fr>');
$content_type = function() { return 'text/html'; };
add_filter( 'wp_mail_content_type', $content_type );
wp_mail( $subscribers, $subject, $message, $headers );
remove_filter( 'wp_mail_content_type', $content_type );
}
}
Thank you so much !
FX
Ok... I just wrote this function to get the user mail (my user is logged when he complets the form) :
$user = wp_get_current_user();
$subscriber = esc_html( $user->user_email );

WordPress - set display name from nickname with register process

It is possible set display name from entered string into nickname registration field? I trying do this with simple hook, but after all it is not work.
function set_default_display_name( $user_id ) {
$user = get_userdata( $user_id );
$name = $user->nickname;
$args = array(
'ID' => $user_id,
'display_name' => $name
);
wp_update_user( $args );
}
add_action( 'user_register', 'set_default_display_name' );
By default immediately after registration the display name was set from WP username (login) not nickname. Can sombody help me to set a display name from nickname?
// change default display name format
add_action('user_register', 'registration_save_displayname', 1000);
function registration_save_displayname($user_id) {
if ( isset( $_POST['first_name'])){
$pretty_name = $_POST['first_name'];
wp_update_user( array ('ID' => $user_id, 'display_name'=> $pretty_name) ) ;
}
}

How is wordpress user validation works?

I have created a custom registration form in the front end using wp_insert_user() function:
$data = array();
$data['user_login'] = $_POST['username'];
$data['user_email'] = $_POST['email'];
$data['user_pass'] = wp_generate_password ( 12, false );
$data['role'] = 'pending';
$user = wp_insert_user( $data );
if ( is_wp_error($user) ){
$error = $user->get_error_message();
echo json_encode( array( 'loggedin' => false, 'info' => $error ) );
} else {
wp_new_user_notification( $user, $data['user_pass'] );
}
But I want the user to receive an activation email with activation link. How can I do this?
If you look at the WordPress codex for the wp_new_user_notification method (http://codex.wordpress.org/Function_Reference/wp_new_user_notification#Examples), there is an example of how to redefine this function. In that example, functionality is added to send an email to the created user.

Filter the Buddypress bp_core_signup_user

I want to send out a e-mail to the site admin that a new Buddypress user has signed up. That is what the first function is for. It works great. The second function is to append a few xprofile custom fields into the e-mail. Sadly the filter in the 2e function is not working. How can I make it work? Or how can I combine them so that they work?
function my_pending( $user_id, $user_login, $user_password, $user_email, $usermeta ) {
// Send the email notification.
wp_mail( 'some#some.com', $user_login . ' bla bla', 'bla bla' );
}
add_action( 'bp_core_signup_user', 'my_pending', 10, 5 );
And the second function:
function custom_activation_email_body( $message, $user_id, $key ) {
$field1 = xprofile_get_field_data( '11', $user_id );
$field2 = xprofile_get_field_data( '12', $user_id );
$field3 = xprofile_get_field_data( '4', $user_id );
$message .= sprintf( __( "Username: %s Email: %s Membership Type: %s", 'lang' ), $field1, $field2, $field3 );
return $message;
}
add_filter('bp_core_signup_user', 'custom_activation_email_body', 10, 3);
If you use a field id, don't use it as a string, so
xprofile_get_field_data( '11', $user_id );
should be
xprofile_get_field_data( 11, $user_id );
And I think your filter is applied to the email sent to the new user.
So move your xprofile calls into the add_action function.
Review wp_mail to see how to setup the fields.

Resources