How to change Wordpress Password Reset Email? - wordpress

I want to change the default Password Reset Email in Wordpress 5.2.2
I have tried the following code but it is not working.
add_filter( 'retrieve_password_message', 'my_retrieve_password_message', 10, 4 );
function my_retrieve_password_message( $message, $key, $user_login, $user_data ) {
// Start with the default content.
$site_name = wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES );
$message = __( '(UPDATED)Someone has requested a password reset for the following account:' ) . "\r\n\r\n";
/* translators: %s: site name */
$message .= sprintf( __( 'Site Name: %s' ), $site_name ) . "\r\n\r\n";
/* translators: %s: user login */
$message .= sprintf( __( 'Username: %s' ), $user_login ) . "\r\n\r\n";
$message .= __( 'If this was a mistake, just ignore this email and nothing will happen.' ) . "\r\n\r\n";
$message .= __( 'To reset your password, visit the following address:' ) . "\r\n\r\n";
$message .= '<' . network_site_url( "wp-login.php?action=rp&key=$key&login=" . rawurlencode( $user_login ), 'login' ) . ">\r\n";
// Return the filtered message.
return $message;
}
Any help would be appreciated.

This answer here is working well for me: https://wordpress.stackexchange.com/a/262425
And here is how you can change the email it sends from and the subject.
// Function to change default wordpress#domain.com email address
function wp_sender_email( $original_email_address ) {
return 'YOUR_EMAIL_HERE';
}
// Function to change sender name
function wp_sender_name( $original_email_from ) {
return 'YOUR_SUBJECT_HERE';
}
// Add our functions to WordPress filters
add_filter( 'wp_mail_from', 'wp_sender_email' );
add_filter( 'wp_mail_from_name', 'wp_sender_name' );

Related

WooCommerce restrict coupon to user role error message

I'm using WooCommerce to restrict a coupon to the user role "gold-member" which works fine but I'm trying to change the message displayed when an invalid user tries to use the coupon:
Sorry, coupon code "goldmember10" is not valid with your customer role.
My code
add_filter( 'woocommerce_coupon_error','coupon_error_message_change',10,3 );
function coupon_error_message_change($err, $err_code, $parm ) {
switch ( $err_code ) {
case 100:
$err = sprintf( __( 'Sorry, coupon code "%s" is not valid.', 'woocommerce' ), $parm->get_code() );
break;
}
return $err;
}
$err_code matches 100 per class-wc-coupon.php but the message does not get updated.
This should suffice:
function filter_woocommerce_coupon_error( $err, $err_code, $coupon ) {
// Compare
if ( intval( $err_code ) === WC_COUPON::E_WC_COUPON_INVALID_FILTERED ) {
// Message
$err = sprintf( __( 'Sorry, coupon code "%s" is not valid.', 'woocommerce' ), $coupon->get_code() );
} else {
$err = 'DEBUG INFORMATION, error code number = ' . intval( $err_code );
}
return $err;
}
add_filter( 'woocommerce_coupon_error', 'filter_woocommerce_coupon_error', 10, 3 );

WordPress: template_redirect hook sending wp_mail twice

I have a few restricted pages. If a user tries to access those, I warn them by email using wp_mail and alert admin with some data such as username, URL, time, etc. For that, I am using the template_redirect hook.
All is working fine but wp_mail sending the email twice. I am not sure what is wrong in my code and why it is sending twice.
Callback Function and Hook
cp_write_log() is a custom log function that write a log to debug.log. Entries also adding twice to the debug.log file.
function module_permission_check() {
if ( is_page() ) {
$template = get_page_template_slug();
$post_type = get_cpt_for_template( $template );
if ( ! is_current_user_granted_for_module( $post_type ) ) {
$user = wp_get_current_user();
//$headers = [ 'From: ' . get_bloginfo( 'name' ) . ' <' . get_bloginfo( 'admin_email' ) . '>' ];
// user message
$message = str_replace(
[ '%%name%%', '%%sitename%%' ],
[
$user->data->user_login,
get_bloginfo( 'name' ),
],
cp_get_setting( 'warning_unauthorized_email_message' )
);
if ( wp_mail( $user->data->user_email, 'Warning: Unauthorized access', $message ) ) {
cp_write_log( 'Email sent to ' . $user->data->user_email );
}
global $wp;
$current_url = home_url( add_query_arg( [], $wp->request ) );
$admin_message = "Hello,\n\nA member has tried to access unauthorized content.\n\nUsername: {$user->data->user_login}\nURL: {$current_url}\nLocal Time: " . current_time( 'l, j F Y \a\t H:i ' ) . "\nUTC Time: " . current_time( 'l, j F Y \a\t H:i ', TRUE ) . "\n\nRegards,\n" . get_bloginfo( 'name' );
if ( wp_mail( get_bloginfo( 'admin_email' ), 'Alert: Unauthorized access', $admin_message ) ) {
cp_write_log( 'Email sent to admin' );
}
wp_redirect(home_url());
exit();
}
}
}
add_action('template_redirect', 'module_permission_check');
Update
I found it is only happening on Safari browser. Very strange.

WordPress additonal check while login

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;
}

Problems Adding Woocommerce Bookings info into the Description of Google Calendar

I have synched Woocommerce Bookings with Google Calendar so that employees (with access to a shared Google Calendar) can see instantly when a customer books a trip.
I'm trying to get product add-ons to show up in the description as well. So far the only thing showing up are resources and # of persons. The following code kicks back an error:
Fatal error: Call to a member function get_order_item_totals() on a non-object in /home/content/12/10265512/html/wp-content/plugins/woocommerce-bookings/includes/integrations/class-wc-bookings-google-calendar-integration.php on line 454
I'm just learning my way around PHP out of necessity to get this system to fit our needs. Any help or suggestions would be / are greatly appreciated!
public function sync_booking( $booking_id ) {
$event_id = get_post_meta( $booking_id, '_wc_bookings_gcalendar_event_id', true );
$booking = get_wc_booking( $booking_id );
$api_url = $this->calendars_uri . $this->calendar_id . '/events';
$access_token = $this->get_access_token();
$timezone = wc_booking_get_timezone_string();
$product = $booking->get_product();
$summary = '#' . $booking->id . ' - ' . $product->get_title();
$description = '';
// Add resources in description
if ( $resource = $booking->get_resource() ) {
$description .= __( 'Resource #', 'woocommerce-bookings' ) . $resource->ID . ' - ' . $resource->post_title;
}
// Add ProductAdd on in description testing this
if ( $totals = $order->get_order_item_totals() ) {
$i = 0;
foreach ( $totals as $total ) {
$i++;
if ( $i == 1 ) {
$description .= sprintf($total['label']) . PHP_EOL;
$description .= sprintf($total['value']) . PHP_EOL;
}
}
}
// Add persons in description
if ( $booking->has_persons() ) {
$description .= ( '' != $description ) ? PHP_EOL . PHP_EOL : '';
foreach ( $booking->get_persons() as $id => $qty ) {
if ( 0 === $qty ) {
continue;
}
$person_type = ( 0 < $id ) ? get_the_title( $id ) : __( 'Person(s)', 'woocommerce-bookings' );
$description .= sprintf( __( '%s: %d', 'woocommerce-bookings'), $person_type, $qty ) . PHP_EOL;
$description .= wp_kses_post( $product->post->post_excerpt, array() ) . PHP_EOL;
$description .= sprintf("ADDITIONAL NOTES:") . PHP_EOL;
$description .= sprintf("Hey John passing static notes to Calender test") . PHP_EOL;
}
}
Edit
I tried adding Billing Email and Billing Phone and still cannot get it right. All I see in Google Calendar is Email followed by blank space.
$description .= sprintf( __('Email: %s', 'woocommerce'), $order->billing_email ) . PHP_EOL;
$description .= sprintf( __('Phone: %s', 'woocommerce'), $order->billing_phone ) . PHP_EOL;
the wp_kses_post method in the code up above does return the short description from WooCommerce and places into the Google Calendar description so I will try that next.
I'm still pluggin away at this and after looking at some other websites tried the following
$description .= sprintf( __('%s', 'woocommerce'), $order->email_order_items_table( true, false, true )) . PHP_EOL;
Which is used to pass the same information on in an email to the site admin.
<?php echo $order->email_order_items_table( true, false, true ); ?>
I'm still getting a Fatal Error call to a member function on a non-object. I contacted support at WooCommerce a few days ago. I'll report back here incase someon else is trying to do the same thing as me.
I was able to solve the issue by pulling all order info from the email_orders_table. I'm posting my answer in case someone else is attempting to do the same thing. Customer Support at WooCommerce was unable to help stating it was outside of their support policy and considered custom code.
// Add First Name of Guest
$description .= sprintf( __( 'NAME: %s', 'woocommerce-bookings' ), $booking->get_order()->billing_first_name ) . PHP_EOL;
// Add their email address
$description .= sprintf( __('EMAIL: %s', 'woocommerce-bookings'), $booking->get_order()->billing_email ) . PHP_EOL;
// Add their Phone Number
$description .= sprintf( __('PHONE: %s', 'woocommerce-bookings'), $booking->get_order()->billing_phone ) . PHP_EOL;
// Purchase Notes and WooCommerce Product Add Ons
$description .= sprintf("ADDITIONAL NOTES:") . PHP_EOL;
$description .= sprintf ( __('%s', 'woocommerce-bookings'), strip_tags($order->email_order_items_table( $order->is_download_permitted(), true, true ))) . PHP_EOL ;
This works really well after stripping the HTML tags and all the Guest information is shown in a Private Google Calendar for the entire Staff to Reference. Looking back I certainly worked around in a circle. I had forgotten.
do_action( 'woocommerce_email_after_order_table', $order, true, false );

notifications for buddypress Like plugin

I'm using the Buddypress like plugin which adds the feature to like status updates. Now when a user likes something it doesn't sent a status notification like other buddypress components do. I tried to edit the plugin's code to add this functionality and added the following code to bp-like.php, but still didn't work:
/******************************************************
/* Notification
*****************************************************/
function bp_like_setup_globals() {
global $bp, $current_blog;
$bp->bp_like->id = 'bp_like_notifier';
$bp->bp_like->slug = 'BP_LIKE_SLUG';
$bp->bp_like->notification_callback = 'like_format_notifications';
$bp->active_components[$bp->bp_like->slug] = $bp->bp_like->id;
do_action( 'bp_like_setup_globals' );
}
add_action( 'bp_setup_globals', 'bp_like_setup_globals' );
function like_format_notifications( $action, $item_id, $secondary_item_id, $total_items ) {
global $bp;
$link=like_notifier_activity_get_permalink( $item_id );
if( 'activity_like' == $action ) {
if ( (int)$total_items > 1 )
return apply_filters( 'log_multiple_verifications_notification', '' . sprintf( __('You have %d new likes', 'bp-like' ), (int)$total_items ) . '', $total_items );
else
return apply_filters( 'log_single_verification_notification', '' . sprintf( __('You have %d new like', 'bp-like' ), (int)$total_items ) . '', $total_items );
}
do_action( 'like_format_notifications', $action, $item_id, $secondary_item_id, $total_items );
return false;
}
function like_remove_screen_notifications() {
global $bp;
if($has_access)//if user can view this activity, remove notification(just a safeguard for hidden activity)
bp_core_delete_notifications_for_user_by_type( $bp->loggedin_user->id, $bp->bp_like->id, 'new_like' );
}
add_action( 'bp_activity_screen_single_activity_permalink', 'like_remove_screen_notifications' );
//get the thread permalink for activity
function like_notifier_activity_get_permalink( $item_id, $activity_obj = false ) {
global $bp;
if ( !$activity_obj )
$activity_obj = new BP_Activity_Activity( $item_id );
if ( 'activity_comment' == $activity_obj->type )
$link = $bp->root_domain . '/' . BP_ACTIVITY_SLUG . '/p/' . $activity_obj->item_id . '/';
else
$link = $bp->root_domain . '/' . BP_ACTIVITY_SLUG . '/p/' . $activity_obj->id . '/';
return apply_filters( 'like_notifier_activity_get_permalink', $link );
}
and inside bp_like_add_user_like() function I put this after $liked_count = count( $users_who_like ):
bp_core_add_notification( $item_id, $user_id, $bp->bp_like->id, 'activity_like' );
so far this didn't work.. any idea what I'm missing here?
thanks

Resources