Quantcast
Channel: WordPress › Support » Forum: Hacks - Recent Topics
Viewing all articles
Browse latest Browse all 8245

mike62 on "Adding a checkbox to the reg form, then customizing the admin notification email"

$
0
0

Site, for reference: https://www.hilarydruxman.com/my-account/

I have made a checkbox appear on the registration form that says "Send me more info about becoming a wholesaler." I have used the below code to achieve this (which I took from here):

//add newsletter and wholesale checkboxes to the registration form

add_filter( 'register_form', 'adding_custom_registration_fields' );
function adding_custom_registration_fields( ) {

	//lets make the field required so that i can show you how to validate it later;
	//echo '<div class="form-row form-row-wide"><label for="reg_firstname">'.__('First Name', 'woocommerce').' <span class="required">*</span></label><input type="text" class="input-text" name="firstname" id="reg_firstname" size="30" value="'.esc_attr($_POST['firstname']).'" /></div>';

	echo '';
	echo '<div class="form-row form-row-wide"><label for="reg_subscribe"><input type="checkbox" name="reg_subscribe" id="reg_subscribe" value="'.esc_attr($_POST['is_subscribed']).'" />Subscribe to the newsletter</label></div>';
	echo '<div class="form-row form-row-wide"><label for="reg_wholesaler"><input type="checkbox" name="reg_wholesaler" id="reg_wholesaler" value="'.esc_attr($_POST['is_wholesaler']).'" />Send me more info about becoming a wholesaler</label></div>';
}

//Updating user meta after registration successful registration
add_action('woocommerce_created_customer','adding_extra_reg_fields');

function adding_extra_reg_fields($user_id) {

	extract($_POST);
	//add new fields to the user profile
	add_user_meta($user_id, '_is_subscribed', $is_subscribed );
	add_user_meta($user_id, '_is_wholesaler', $is_wholesaler );

	update_user_meta($user_id, '_is_subscribed', $is_subscribed);
	update_user_meta($user_id, '_is_wholesaler', $is_wholesaler);

}

I'm not sure if I did the add_user_meta stuff right; I've never added new fields to the profile on registration before.

I need to get the admin notification email to include a note about the wholesaler checkbox. I'm trying to follow the instructions here. It says to make a plugin, so I've added the following code to a PHP file and uploaded it to my plugins directory, and activated it. But it doesn't seem to be working (I don't get a notification email at all):

<?php
	defined('ABSPATH') or die("No script kiddies please!");

	/**
 * Plugin Name: Customize the admin new user notification email
 * Plugin URI:
 * Description: Customize the admin new user notification email
 * Version: 1.0
 * Author: Mike Berg
 * Author URI:
 * License: GPL2
 */

// Redefine user notification function
// http://codex.wordpress.org/Function_Reference/wp_new_user_notification

if ( !function_exists('wp_new_user_notification') ) {

	function wp_new_user_notification( $user_id, $plaintext_pass = '' ) {

		$user = new WP_User( $user_id );

		$user_login = stripslashes( $user->user_login );
		$user_email = stripslashes( $user->user_email );
		$user_wholesaler = stripslashes( $user->_is_wholesaler );

		$message  = sprintf( __('New user registration on %s:'), get_option('blogname') ) . "\r\n\r\n";
		$message .= sprintf( __('Username: %s'), $user_login ) . "\r\n\r\n";
		$message .= sprintf( __('E-mail: %s'), $user_email ) . "\r\n";
		$message .= sprintf( __('Wholesale: %s'), $user_wholesaler ) . "\r\n";

		@wp_mail(
			get_option('admin_email'),
			sprintf(__('[%s] New User Registration'), get_option('blogname') ),
			$message
		);

		if ( empty( $plaintext_pass ) )
			return;

		$message  = __('Hi there,') . "\r\n\r\n";
		$message .= sprintf( __("Welcome to %s! Here's how to log in:"), get_option('blogname')) . "\r\n\r\n";
		$message .= wp_login_url() . "\r\n";
		$message .= sprintf( __('Username: %s'), $user_login ) . "\r\n";
		$message .= sprintf( __('Password: %s'), $plaintext_pass ) . "\r\n\r\n";
		$message .= sprintf( __('If you have any problems, please contact me at %s.'), get_option('admin_email') ) . "\r\n\r\n";
		$message .= __('Adios!');

		wp_mail(
			$user_email,
			sprintf( __('[%s] Your username and password'), get_option('blogname') ),
			$message
		);
	}
}
?>

What am I missing? Any help would be very much appreciated.


Viewing all articles
Browse latest Browse all 8245

Trending Articles