-
-
Save anastis/ca224eecc6b24ee593f80c52e3afd9b1 to your computer and use it in GitHub Desktop.
<?php | |
/* | |
Plugin Name: Custom Registration Fields | |
Plugin URI: | |
Description: | |
Version: 0.1 | |
Author: CSSIgniter | |
Author URI: | |
License: GPLv2 or later | |
License URI: http://www.gnu.org/licenses/gpl-2.0.html | |
*/ | |
/** | |
* Front end registration | |
*/ | |
add_action( 'register_form', 'crf_registration_form' ); | |
function crf_registration_form() { | |
$year = ! empty( $_POST['year_of_birth'] ) ? intval( $_POST['year_of_birth'] ) : ''; | |
?> | |
<p> | |
<label for="year_of_birth"><?php esc_html_e( 'Year of birth', 'crf' ) ?><br/> | |
<input type="number" | |
min="1900" | |
max="2017" | |
step="1" | |
id="year_of_birth" | |
name="year_of_birth" | |
value="<?php echo esc_attr( $year ); ?>" | |
class="input" | |
/> | |
</label> | |
</p> | |
<?php | |
} | |
add_filter( 'registration_errors', 'crf_registration_errors', 10, 3 ); | |
function crf_registration_errors( $errors, $sanitized_user_login, $user_email ) { | |
if ( empty( $_POST['year_of_birth'] ) ) { | |
$errors->add( 'year_of_birth_error', __( '<strong>ERROR</strong>: Please enter your year of birth.', 'crf' ) ); | |
} | |
if ( ! empty( $_POST['year_of_birth'] ) && intval( $_POST['year_of_birth'] ) < 1900 ) { | |
$errors->add( 'year_of_birth_error', __( '<strong>ERROR</strong>: You must be born after 1900.', 'crf' ) ); | |
} | |
return $errors; | |
} | |
add_action( 'user_register', 'crf_user_register' ); | |
function crf_user_register( $user_id ) { | |
if ( ! empty( $_POST['year_of_birth'] ) ) { | |
update_user_meta( $user_id, 'year_of_birth', intval( $_POST['year_of_birth'] ) ); | |
} | |
} | |
/** | |
* Back end registration | |
*/ | |
add_action( 'user_new_form', 'crf_admin_registration_form' ); | |
function crf_admin_registration_form( $operation ) { | |
if ( 'add-new-user' !== $operation ) { | |
// $operation may also be 'add-existing-user' | |
return; | |
} | |
$year = ! empty( $_POST['year_of_birth'] ) ? intval( $_POST['year_of_birth'] ) : ''; | |
?> | |
<h3><?php esc_html_e( 'Personal Information', 'crf' ); ?></h3> | |
<table class="form-table"> | |
<tr> | |
<th><label for="year_of_birth"><?php esc_html_e( 'Year of birth', 'crf' ); ?></label> <span class="description"><?php esc_html_e( '(required)', 'crf' ); ?></span></th> | |
<td> | |
<input type="number" | |
min="1900" | |
max="2017" | |
step="1" | |
id="year_of_birth" | |
name="year_of_birth" | |
value="<?php echo esc_attr( $year ); ?>" | |
class="regular-text" | |
/> | |
</td> | |
</tr> | |
</table> | |
<?php | |
} | |
add_action( 'user_profile_update_errors', 'crf_user_profile_update_errors', 10, 3 ); | |
function crf_user_profile_update_errors( $errors, $update, $user ) { | |
if ( $update ) { | |
return; | |
} | |
if ( empty( $_POST['year_of_birth'] ) ) { | |
$errors->add( 'year_of_birth_error', __( '<strong>ERROR</strong>: Please enter your year of birth.', 'crf' ) ); | |
} | |
if ( ! empty( $_POST['year_of_birth'] ) && intval( $_POST['year_of_birth'] ) < 1900 ) { | |
$errors->add( 'year_of_birth_error', __( '<strong>ERROR</strong>: You must be born after 1900.', 'crf' ) ); | |
} | |
} | |
add_action( 'edit_user_created_user', 'crf_user_register' ); | |
/** | |
* Back end display | |
*/ | |
add_action( 'show_user_profile', 'crf_show_extra_profile_fields' ); | |
add_action( 'edit_user_profile', 'crf_show_extra_profile_fields' ); | |
function crf_show_extra_profile_fields( $user ) { | |
?> | |
<h3><?php esc_html_e( 'Personal Information', 'crf' ); ?></h3> | |
<table class="form-table"> | |
<tr> | |
<th><label for="year_of_birth"><?php esc_html_e( 'Year of birth', 'crf' ); ?></label></th> | |
<td><?php echo esc_html( get_the_author_meta( 'year_of_birth', $user->ID ) ); ?></td> | |
</tr> | |
</table> | |
<?php | |
} |
Thanks for your code! it was a saver. Please I have a request.
I want to 3 more fields, that is Phone number, Gender and Address. Please any idea how to go about this? I tried adding it though but the result was zero (0) in the backend view.
I will be grateful for your support.
Thanks again
@malaspigar
Any news? i too want 3 more fields. But to was zero. :/
Merci beaucoup pour cela. Juste quelques notes. Quand dans un vrai plugin, cela vaut la peine d'appeler ces fonctions comme
// champs utilisateur personnalisés add_action ( ' user_new_form ' , array ( $ this , ' crf_admin_registration_form ' )); add_action ( ' user_profile_update_errors ' , array ( $ this , ' crf_user_profile_update_errors ' ), 10 , 3 ); add_action ( ' edit_user_created_user ' , array ( $ this , ' crf_user_register ' )); add_action ( ' show_user_profile ' , array ( $ this , ' crf_show_extra_profile_fields ' )); add_action ( ' edit_user_profile ' , array ( $ this , ' crf_show_extra_profile_fields ' ));
Prenez en compte les 2 paramètres de
add_action( 'user_profile_update_errors', array( $this,'crf_user_profile_update_errors'), 10, 3 );
, car c'est une erreur courante de l'écrire dans le tableau ...Une autre question. Comment rendre les champs personnalisés modifiables dans la page Profil utilisateur? Jusqu'à présent, il
crf_show_extra_profile_fields
ne montre que les champs, mais je ne peux pas les modifier sur place.
Merci beaucoup pour cela. Juste quelques notes. Quand dans un vrai plugin, cela vaut la peine d'appeler ces fonctions comme
// champs utilisateur personnalisés add_action ( ' user_new_form ' , array ( $ this , ' crf_admin_registration_form ' )); add_action ( ' user_profile_update_errors ' , array ( $ this , ' crf_user_profile_update_errors ' ), 10 , 3 ); add_action ( ' edit_user_created_user ' , array ( $ this , ' crf_user_register ' )); add_action ( ' show_user_profile ' , array ( $ this , ' crf_show_extra_profile_fields ' )); add_action ( ' edit_user_profile ' , array ( $ this , ' crf_show_extra_profile_fields ' ));
Prenez en compte les 2 paramètres de
add_action( 'user_profile_update_errors', array( $this,'crf_user_profile_update_errors'), 10, 3 );
, car c'est une erreur courante de l'écrire dans le tableau ...Une autre question. Comment rendre les champs personnalisés modifiables dans la page Profil utilisateur? Jusqu'à présent, il
crf_show_extra_profile_fields
ne montre que les champs, mais je ne peux pas les modifier sur place.
Did you find the way to edit your custom fields after registration ?
Excellent! How do I create a input field that is a file? I need that users upload a CV.
Thanks,
Francisco
Excellent! How do I create a input field that is a file? I need that users upload a CV.
Thanks,
Francisco
did you do it? to add more fields
add_action( 'show_user_profile', array( $this,'crf_show_extra_profile_fields') ); add_action( 'edit_user_profile', array( $this,'crf_show_extra_profile_fields') );
how add name surname ?
this code not work for me
add_action( 'show_user_profile', array( $this,'crf_show_extra_profile_fields') ); add_action( 'edit_user_profile', array( $this,'crf_show_extra_profile_fields') );
how add name surname ?
this code not work for me
in functions.php you should use:
add_action( 'user_register', 'crf_user_register' );
function crf_user_register( $user_id ) {
if ( ! empty( $_POST['nombrecompleto'] ) ) {
update_user_meta( $user_id, 'NAMEFIELD', sanitize_text_field( $_POST['NAMEFIELD'] ) );
}
}
add_filter( 'registration_errors', 'crf_registration_errors', 10, 3 );
function crf_registration_errors( $errors, $sanitized_user_login, $user_email ) {
if ( empty( $_POST['NAMEFIELD'] ) ) {
$errors->add( 'NAMEFIELD_error', __( '<strong>Error</strong>: Por favor add the field.', 'crf' ) );
}
return $errors;
}
add_action( 'register_form', 'crf_registration_form' );
function crf_registration_form() {
$valor_NAMEFIELD = ! empty( $_POST['NAMEFIELD'] ) ? ( $_POST['NAMEFIELD'] ) : '';
?>
Please fill the field <input type="text" class="input" name="NAMEFIELD" required value="<? if ($valor_NAMEFIELD != "") {echo $valor_NAMEFIELD;}?>" id="NAMEFIELD" minlength="5">
<br />
<br />
<?
}
add_action( 'user_profile_update_errors', 'crf_user_profile_update_errors', 10, 3 );
function crf_user_profile_update_errors( $errors, $update, $user ) {
if ( $update ) {
return;
}
}
add_action( 'edit_user_created_user', 'admincrf_user_register' );
function admincrf_user_register( $user_id ) {
if ( ! empty( $_POST['nombrecompleto'] ) ) {
update_user_meta( $user_id, 'NAMEFIELD', sanitize_text_field( $_POST['NAMEFIELD'] ) );
}
}
add_action( 'user_new_form', 'crf_admin_registration_form' );
function crf_admin_registration_form( $operation ) {
if ( 'add-new-user' !== $operation ) {
// $operation may also be 'add-existing-user'
return;
}
$valor_NAMEFIELD = ! empty( $_POST['NAMEFIELD'] ) ? ( $_POST['NAMEFIELD'] ) : '';
?>
<table class="form-table">
<tr>
<th>Datos personalizados</th>
<td>
Fill the field <input type="text" class="input" name="NAMEFIELD" required value="<? if ($valor_NAMEFIELD != "") {echo $valor_NAMEFIELD;}?>" id="NAMEFIELD" minlength="5">
</td>
</tr>
</table>
<?
}
//guardar datos al editar perfil propio
add_action( 'personal_options_update', 'crf_update_profile_fields' );
add_action( 'edit_user_profile_update', 'crf_update_profile_fields' );
function crf_update_profile_fields( $user_id ) {
if ( ! current_user_can( 'edit_user', $user_id ) ) {
return false;
}
if ( ! empty( $_POST['NAMEFIELD'] ) ) {
update_user_meta( $user_id, 'NAMEFIELD', sanitize_text_field( $_POST['NAMEFIELD'] ) );
}
}
add_action( 'show_user_profile', 'crf_show_extra_profile_fields' );
add_action( 'edit_user_profile', 'crf_show_extra_profile_fields' );
function crf_show_extra_profile_fields( $user ) {
$valor_NAMEFIELD = get_the_author_meta( 'NAMEFIELD', $user->ID );
?>
<table class="form-table">
<tr>
<th>Datos personalizados</th>
<td>
Fill the field <input type="text" class="input" name="NAMEFIELD" required value="<? if ($valor_NAMEFIELD != "") {echo $valor_NAMEFIELD;}?>" id="NAMEFIELD" minlength="5">
</td>
</tr>
</table>
<?
}
?>
Great code, been spending too many hours over trying to get similar to work!
How do I go about adding a pre-populated Select field type? Job title for example.
Great code, been spending too many hours over trying to get similar to work!
How do I go about adding a pre-populated Select field type? Job title for example.
Hi!
You have to play a little with HTML and use something like this in your HTML form
1with all the fields you need.
Greetings,
Francisco
Thanks a lot for this. Just few notes. When in a real plugin it's worth to call these functions like
Specifically take care of the 2 parameters of
add_action( 'user_profile_update_errors', array( $this,'crf_user_profile_update_errors'), 10, 3 );
, since it's a common error to write it down in the array...Another question. How to make the custom fields editable in the User Profile page? So far the
crf_show_extra_profile_fields
is only showing the fields, but I cannot edit them in place.