Adding first and last name to Wordpress registration form - php

My registration form on WP only had the options Username, e-mail and password.
//1. firstname
add_action( 'register_form', 'myplugin_register_form' );
function myplugin_register_form() {
$first_name = ( ! empty( $_POST['first_name'] ) ) ? trim( $_POST['first_name'] ) : '';
?>
<?php
}
//2. Add validation. In this case, we make sure first_name is required.
add_filter( 'registration_errors', 'myplugin_registration_errors', 10, 3 );
function myplugin_registration_errors( $errors, $sanitized_user_login, $user_email ) {
if ( empty( $_POST['first_name'] ) || ! empty( $_POST['first_name'] ) && trim( $_POST['first_name'] ) == '' ) {
$errors->add( 'first_name_error', __( '<strong>ERROR</strong>: You must include a first name.', 'mydomain' ) );
}
return $errors;
}
//3. Finally, save our extra registration user meta.
add_action( 'user_register', 'myplugin_user_register' );
function myplugin_user_register( $user_id ) {
if ( ! empty( $_POST['first_name'] ) ) {
update_user_meta( $user_id, 'first_name', trim( $_POST['first_name'] ) );
}
}
In my registration form (template) I've placed the following:
<label><?php _e('First Name', APP_TD) ?></label>
<input tabindex="3" type="text" name="first_name" class="text regular-text" id="display_name" value="<?php echo esc_attr( wp_unslash( $first_name ) ); ?>" maxlength="100" />
And it works as it should. It grabs the first name and places it in the user profile. However I'm not sure on how to add the last name to it aswell, I'm quite a beginner, and for some reason I can't get the last name to work. Help would be much appreciated.

I've never worked with something like that on my WP. Where have you put this code?
However I would try to execute something like this:
//1. Fistname
add_action( 'register_form', 'myplugin_register_form' ); function myplugin_register_form() {
$first_name = ( ! empty( $_POST['first_name'] ) ) ? trim( $_POST['first_name'] ) : '';
$last_name = ( ! empty( $_POST['last_name'] ) ) ? trim( $_POST['last_name'] ) : '';
?>
<?php
}
//2. Add validation. In this case, we make sure first_name is required.
add_filter( 'registration_errors', 'myplugin_registration_errors', 10, 3 );
function myplugin_registration_errors( $errors, $sanitized_user_login, $user_email ) {
if ( empty( $_POST['first_name'] ) || ! empty( $_POST['first_name'] ) && trim( $_POST['first_name'] ) == '' ) {
$errors->add( 'first_name_error', __( '<strong>ERROR</strong>: You must include a first name.', 'mydomain' ) );
}
if ( empty( $_POST['last_name'] ) || ! empty( $_POST['last_name'] ) && trim( $_POST['last_name'] ) == '' ) {
$errors->add( 'last_name_error', __( '<strong>ERROR</strong>: You must include a last name.', 'mydomain' ) );
}
return $errors;
}
//3. Finally, save our extra registration user meta.
add_action( 'user_register', 'myplugin_user_register' );
function myplugin_user_register( $user_id ) {
if ( ! empty( $_POST['first_name'] ) ) {
update_user_meta( $user_id, 'first_name', trim( $_POST['first_name'] ));
}
if ( ! empty( $_POST['last_name'] ) ) {
update_user_meta( $user_id, 'last_name', trim( $_POST['last_name'] ) );
}
}

Related

Save custom product input to database in WooCommerce

I use the following code to the database:
$attribute_R_0 = wc_get_product( $post_id );
$title_top_R_0 = isset( $_POST['custom_text_field_title_related'] ) ? $_POST['custom_text_field_title_related'] : '';
$attribute_R_0->update_meta_data( 'custom_text_field_title_related', sanitize_text_field( $title_top_R_0 ) );
$attribute_R_0->save();
but I want to use a condition to avoid creating a blank table in db, which one is correct? A or B?
A:
if ('custom_text_field_title_related') {
$attribute_R_0 = wc_get_product( $post_id );
$title_top_R_0 = isset( $_POST['custom_text_field_title_related'] ) ? $_POST['custom_text_field_title_related'] : '';
$attribute_R_0->update_meta_data( 'custom_text_field_title_related', sanitize_text_field( $title_top_R_0 ) );
$attribute_R_0->save();
}
B:
$attribute_R_0 = wc_get_product( $post_id );
$nui_1 = $attribute_14->get_meta( 'custom_text_field_title_related' );
if ($nui_1 ){
$title_top_R_0 = isset( $_POST['custom_text_field_title_related'] ) ? $_POST['custom_text_field_title_related'] : '';
$attribute_R_0->update_meta_data( 'custom_text_field_title_related', sanitize_text_field( $title_top_R_0 ) );
$attribute_R_0->save();
}
None of them… To avoid a blank entry in database table, you should use instead:
if ( isset( $_POST['custom_text_field_title_related'] ) && ! empty( $_POST['custom_text_field_title_related'] ) ) {
$attribute_R_0 = wc_get_product( $post_id );
$attribute_R_0->update_meta_data( 'custom_text_field_title_related', sanitize_text_field( $_POST['custom_text_field_title_related'] ) );
$attribute_R_0->save();
}
or better:
$field_key_R_0 = 'custom_text_field_title_related';
if ( isset( $_POST[$field_key_R_0] ) && ! empty( $_POST[$field_key_R_0] ) ) {
$attribute_R_0 = wc_get_product( $post_id );
$attribute_R_0->update_meta_data( $field_key_R_0, sanitize_text_field( $_POST[$field_key_R_0] ) );
$attribute_R_0->save();
}

How to validate WooCommerce checkout form

In woocommerce the checkout form (Town/City) not validating with this code.
// Check if Ship to a different address is set, if it's set then validate shipping fields.
if (!empty($ship_to_different_address)) {
if (empty(trim($billing_city)) || !ctype_alpha($billing_city)) {
wc_add_notice(__('Only alphabets are alowed in <strong>Shipping First Name</strong>.'), 'error');
}
if (empty(trim($shipping_city)) || !ctype_alpha($shipping_city)) {
wc_add_notice(__('Only alphabets are alowed in <strong>Shipping Last Name</strong>.'), 'error');
}
}
Something like this may do the trick...
function so_61559494_validate_checkout() {
if( ! empty( $_POST['ship_to_different_address'] ) ) {
$billing_city = ! empty( $_POST['billing_city'] ) ? trim( $_POST['billing_city'] ) : '';
if ( empty( $billing_city ) || ! ctype_alpha( $billing_city ) ) {
$notice = __('Only alphabets are alowed in <strong>Billing City</strong>.', 'your-text-domain' );
throw new Exception( $notice );
}
$shiping_city = ! empty( $_POST['shiping_city'] ) ? trim( $_POST['shiping_city'] ) : '';
if ( empty( $shipping_city ) || ! ctype_alpha( $shipping_city ) ) {
$notice = __('Only alphabets are alowed in <strong>Shipping City</strong>.', 'your-text-domain' );
throw new Exception( $notice );
}
}
}
add_action( 'woocommerce_checkout_process', 'so_61559494_validate_checkout' );
It's entirely untested though, so use with caution.

Cannot get metadata from database for custom post types

Already tried using save_post and updated_post actions.
Here's the code:
function la_add_custom_metabox() {
add_meta_box(
'la_meta',
__( 'Dados de Cadastro' ),
'la_meta_callback',
'cota',
'normal',
'core'
);
}
add_action( 'add_meta_boxes', 'la_add_custom_metabox' );
My custom field:
function la_meta_callback(){
wp_nonce_field( basename( __FILE__ ), 'la_cotas_nonce' );
$la_stored_meta = get_post_meta( $post->ID ); ?>
<div>
<div class="meta-row">
<div class="meta-th">
<label for="credito" class="dwwp-row-title"><?php _e( 'Crédito', 'la-cotas' ); ?></label>
</div>
<div class="meta-td">
<input type="text" class="dwwp-row-content" name="credito" id="credito" value="<?php if ( ! empty ( $la_stored_meta['credito'] ) ) { echo esc_attr( $la_stored_meta['credito'][0] ); } ?>"/>
</div>
</div>
</div>
Function that saves in database
function la_meta_save( $post_id ) {
// Checks save status
$is_autosave = wp_is_post_autosave( $post_id );
$is_revision = wp_is_post_revision( $post_id );
$is_valid_nonce = ( isset( $_POST[ 'la_cotas_nonce' ] ) && wp_verify_nonce( $_POST[ 'la_cotas_nonce' ], basename( __FILE__ ) ) ) ? 'true' : 'false';
// Exits script depending on save status
if ( $is_autosave || $is_revision || !$is_valid_nonce ) {
return;
}
if ( isset( $_POST[ 'credito' ] ) ) {
update_post_meta( $post_id, 'credito', sanitize_text_field( $_POST[ 'credito' ] ) );
}
}
add_action( 'save_post', 'la_meta_save' );
I tried many tutorials in internet, and i couldn't find what i'm doing wrong.
expected: The data to be inserted in the database and being displayed in the custom edit page field.
Edit 1: I discovered that the data is being saved, but its not being displayed on the edit fields.
Solved: For those who are interested: I wasn't passing $post on the save_meta callback
//This solved the problem
function la_meta_callback($post){ ... }

WordPress - how to add custom meta box to a specific admin page?

How can I add my custom meta box to a specific page only on the admin page?
Here is my custom meta box code which I got it from here:
/**
* Adds a meta box to the post editing screen
*/
function prfx_custom_meta() {
add_meta_box( 'prfx_meta', __( 'Meta Box Title', 'prfx-textdomain' ), 'prfx_meta_callback', array( 'post', 'page') );
}
add_action( 'add_meta_boxes', 'prfx_custom_meta' );
/**
* Outputs the content of the meta box
*/
function prfx_meta_callback( $post ) {
// echo 'This is a meta box';
wp_nonce_field( basename( __FILE__ ), 'prfx_nonce' );
$prfx_stored_meta = get_post_meta( $post->ID );
if ($post_slug == 'home') {
?>
<p>
<label for="meta-text" class="prfx-row-title"><?php _e( 'Example Text Input', 'prfx-textdomain' )?></label>
<input type="text" name="meta-text" id="meta-text" value="<?php if ( isset ( $prfx_stored_meta['meta-text'] ) ) echo $prfx_stored_meta['meta-text'][0]; ?>" />
</p>
<?php
}
}
/**
* Saves the custom meta input
*/
function prfx_meta_save( $post_id ) {
// Checks save status
$is_autosave = wp_is_post_autosave( $post_id );
$is_revision = wp_is_post_revision( $post_id );
$is_valid_nonce = ( isset( $_POST[ 'prfx_nonce' ] ) && wp_verify_nonce( $_POST[ 'prfx_nonce' ], basename( __FILE__ ) ) ) ? 'true' : 'false';
// Exits script depending on save status
if ( $is_autosave || $is_revision || !$is_valid_nonce ) {
return;
}
// Checks for input and sanitizes/saves if needed
if( isset( $_POST[ 'meta-text' ] ) ) {
update_post_meta( $post_id, 'meta-text', sanitize_text_field( $_POST[ 'meta-text' ] ) );
}
}
add_action( 'save_post', 'prfx_meta_save' );
I just want to add the meta box to my home page. but now on other pages and posts I still see the meta title:
Any idea how I stop it from showing on other pages and posts?
EDIT:
/**
* Add custom meta box to a specific page in the WP admin.
*
* # http://themefoundation.com/wordpress-meta-boxes-guide/
* # http://www.farinspace.com/page-specific-wordpress-meta-box/
*/
function my_meta_init() {
// Get post/page ID.
$post_id = $_GET['post'] ? $_GET['post'] : $_POST['post_ID'] ;
// Get post/page slug.
$post = get_post($post_id);
$slug = $post->post_name;
// checks for post/page slug.
if ($slug == 'home') {
add_meta_box( 'prfx_meta', __( 'Meta Box Title', 'prfx-textdomain' ), 'prfx_meta_callback', array( 'post', 'page') );
}
add_action( 'add_meta_boxes', 'prfx_meta_save' );
}
add_action('admin_init','my_meta_init');
/**
* Outputs the content of the meta box
*/
function prfx_meta_callback( $post ) {
// echo 'This is a meta box';
wp_nonce_field( basename( __FILE__ ), 'prfx_nonce' );
$prfx_stored_meta = get_post_meta( $post->ID );
?>
<p>
<label for="meta-text" class="prfx-row-title"><?php _e( 'Example Text Input', 'prfx-textdomain' )?></label>
<input type="text" name="meta-text" id="meta-text" value="<?php if ( isset ( $prfx_stored_meta['meta-text'] ) ) echo $prfx_stored_meta['meta-text'][0]; ?>" />
</p>
<?php
}
/**
* Saves the custom meta input
*/
function prfx_meta_save( $post_id ) {
// Checks save status
$is_autosave = wp_is_post_autosave( $post_id );
$is_revision = wp_is_post_revision( $post_id );
$is_valid_nonce = ( isset( $_POST[ 'prfx_nonce' ] ) && wp_verify_nonce( $_POST[ 'prfx_nonce' ], basename( __FILE__ ) ) ) ? 'true' : 'false';
// Exits script depending on save status
if ( $is_autosave || $is_revision || !$is_valid_nonce ) {
return;
}
// Checks for input and sanitizes/saves if needed
if( isset( $_POST[ 'meta-text' ] ) ) {
update_post_meta( $post_id, 'meta-text', sanitize_text_field( $_POST[ 'meta-text' ] ) );
}
}
add_action( 'save_post', 'prfx_meta_save' );
You can try this condition
add_action('admin_init','my_meta_init');
function my_meta_init()
{
$post_id = $_GET['post'] ? $_GET['post'] : $_POST['post_ID'] ;
// checks for post/page ID
if ($post_id == '84')
{
add_meta_box('my_all_meta_1', 'My Custom Meta Box 1', 'my_meta_setup_1', 'page', 'normal', 'high');
}
add_action('save_post','my_meta_save');
}
/**
* Adds a meta box to the post editing screen
*/
function prfx_custom_meta() {
$current_user = wp_get_current_user();
if($current_user->roles[0] === 'administrator') {
add_meta_box( 'prfx_meta', __( 'Meta Box Title', 'prfx-textdomain' ), 'prfx_meta_callback', array( 'post', 'page') );
}
}
add_action( 'add_meta_boxes', 'prfx_custom_meta' );
/**
* Outputs the content of the meta box
*/
function prfx_meta_callback( $post ) {
// echo 'This is a meta box';
wp_nonce_field( basename( __FILE__ ), 'prfx_nonce' );
$prfx_stored_meta = get_post_meta( $post->ID );
if ($post_slug == 'home') {
?>
<p>
<label for="meta-text" class="prfx-row-title"><?php _e( 'Example Text Input', 'prfx-textdomain' )?></label>
<input type="text" name="meta-text" id="meta-text" value="<?php if ( isset ( $prfx_stored_meta['meta-text'] ) ) echo $prfx_stored_meta['meta-text'][0]; ?>" />
</p>
<?php
}
}
/**
* Saves the custom meta input
*/
function prfx_meta_save( $post_id ) {
// Checks save status
$is_autosave = wp_is_post_autosave( $post_id );
$is_revision = wp_is_post_revision( $post_id );
$is_valid_nonce = ( isset( $_POST[ 'prfx_nonce' ] ) && wp_verify_nonce( $_POST[ 'prfx_nonce' ], basename( __FILE__ ) ) ) ? 'true' : 'false';
// Exits script depending on save status
if ( $is_autosave || $is_revision || !$is_valid_nonce ) {
return;
}
// Checks for input and sanitizes/saves if needed
if( isset( $_POST[ 'meta-text' ] ) ) {
update_post_meta( $post_id, 'meta-text', sanitize_text_field( $_POST[ 'meta-text' ] ) );
}
}
add_action( 'save_post', 'prfx_meta_save' );
Use this js it will resolve your problem. this will show the metabox on perticular template or page..
(function($){
$(document).ready(function() {
var $page_template = $('#pageid')
,$metabox1 = $('#metaboxid');
$page_template.change(function() {
if ($(this).val() == 'templatename') {
$metabox1.show();
} else {
$metabox1.hide();
}
}).change();
});
})(jQuery);
if you are using this in admin then use admin hook and add this code in functions.php

How do I validate agree terms checkbox for woocommerce registration form?

I have stmbled into a bit of a problem with the woocommerce registration form. I wanted to add a agree to the terms checkbox that would actually work.
I added the code that shows the checkbox in form-login.php (from the templates/my account folder).
I tried to modify the code in wc-user-functions.php and class-wc-form-handler.php from includes folder and now it does not recognize when the user checks the box. It gives me the Please accept terms error every time regardless if i check the box or not.
Does anyone know where is my mistake?
form-login.php added code
<p class="form-row form-row-wide">
<label for="accept_terms" class="inline"><span class="required"></span></label>
<input name="accept_terms" type="checkbox" id="accept_terms" value="empty";/>
<?php _e( 'Am citit si sunt de acord cu' ); ?> <?php _e( 'termenii si conditiile', 'woocommerce' ); ?>
</p>
wc-user-functions.php added code
// Check the accept terms
if ( empty( $accept_terms ) ) {
return new WP_Error( 'registration-error-accept-terms', __( 'Please accept terms', 'woocommerce' ) );
}
wc-class-form-handler.php modified code
public static function process_registration() {
if ( ! empty( $_POST['register'] ) && isset( $_POST['_wpnonce'] ) && wp_verify_nonce( $_POST['_wpnonce'], 'woocommerce-register' ) ) {
$username = 'no' === get_option( 'woocommerce_registration_generate_username' ) ? $_POST['username'] : '';
$password = 'no' === get_option( 'woocommerce_registration_generate_password' ) ? $_POST['password'] : '';
$email = $_POST['email'];
$accept_terms = $_POST['accept_terms'];
try {
$validation_error = new WP_Error();
$validation_error = apply_filters( 'woocommerce_process_registration_errors', $validation_error, $username, $password, $email, $accept_terms);
if ( $validation_error->get_error_code() ) {
throw new Exception( $validation_error->get_error_message() );
}
// Anti-spam trap
if ( ! empty( $_POST['email_2'] ) ) {
throw new Exception( __( 'Anti-spam field was filled in.', 'woocommerce' ) );
}
$new_customer = wc_create_new_customer( sanitize_email( $email ), wc_clean( $username ), $password);
if ( is_wp_error( $new_customer ) ) {
throw new Exception( $new_customer->get_error_message() );
}
if ( apply_filters( 'woocommerce_registration_auth_new_customer', true, $new_customer ) ) {
wc_set_customer_auth_cookie( $new_customer );
}
wp_safe_redirect( apply_filters( 'woocommerce_registration_redirect', wp_get_referer() ? wp_get_referer() : wc_get_page_permalink( 'myaccount' ) ) );
exit;
} catch ( Exception $e ) {
wc_add_notice( '<strong>' . __( 'Error', 'woocommerce' ) . ':</strong> ' . $e->getMessage(), 'error' );
}
}
}
Any help would be greatly appreciated!
First, you should not edit core WooCommerce files. You will lock yourself into the code and never be able to update, which is bad. And there's really no need and WooCommerce is littered with hooks and filters which permit you to make modifications from your own themes/plugins. The following should go in your theme's functions.php or preferably into its own custom plugin.
This first part will add the terms checkbox. If you wish to show the exact same terms field as on the checkout page, then you can just use the same template.
function so_33122634_add_field_to_registration(){
wc_get_template( 'checkout/terms.php' );
}
add_action( 'woocommerce_register_form', 'so_33122634_add_field_to_registration' );
Then on the woocommerce_process_registration_errors we can check the $_POST and throw exceptions if it isn't there. WooCommerce will collect all the exceptions and show the appropriate error messages.
function so_33122634_validation_registration( $errors, $username, $password, $email ){
if ( empty( $_POST['terms'] ) ) {
throw new Exception( __( 'You must accept the terms and conditions in order to register.', 'text-domain' ) );
}
return $errors;
}
add_action( 'woocommerce_process_registration_errors', 'so_33122634_validation_registration', 10, 4 );

Categories