• Resolved piitr64

    (@piitr64)


    Hi.
    How can I add extra field with date picker?
    Now I have:

    function wpb_new_contactmethods( $contactmethods ) {
    // Add Facebook
    $contactmethods['facebook'] = 'Facebook';
    // Add Twitter
    $contactmethods['twitter'] = 'Twitter';
    return $contactmethods;
    }
    add_filter('user_contactmethods','wpb_new_contactmethods',10,1);

    but I need too add date_of_birth with date picker.
    Is it possible?
    Thanks for answer.

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Author Shea Bunge

    (@bungeshea)

    Hey,

    The code you have here can only be used to adding text fields – it’s quite limited.

    In order to incorporate something like a date picker, you’re going to need a lot of extra code. I am sure that it’s possible, but will require a fair amount of research to piece together.

    Alternatively, you could use a plugin like Advanced Custom Fields to make the process easier.

    Plugin Author Shea Bunge

    (@bungeshea)

    Hello again,

    After writing that, I decided to just do the research and put together some code to do what you’re looking for.

    Here is is:

    function shea_user_profile_dateofbirth( $user ) {
    	?>
    	<h2><?php _e( 'Extra Information' ); ?></h2>
    	<table class="form-table">
    		<tr>
    			<th><label for="dateofbirth"><?php _e( 'Date of Birth' ); ?></label></th>
    			<td>
    				<input type="date" id="dateofbirth" name="dateofbirth" value="<?php
    					echo esc_attr( get_the_author_meta( 'dateofbirth', $user->ID ) ); ?>">
    			</td>
    		</tr>
    	</table>
    	<?php
    }
    
    add_action( 'show_user_profile', 'shea_user_profile_dateofbirth' );
    add_action( 'edit_user_profile', 'shea_user_profile_dateofbirth' );
    
    function shea_enqueue_date_picker() {
    	wp_enqueue_script( 'jquery-ui-datepicker' );
    	wp_enqueue_style( 'jquery-ui-datepicker' );
    	
    	?>
    	<script>
    	jQuery(document).ready(function() {
    		jQuery('#dateofbirth').datepicker();
    	});
    	</script>
    	<?php
    }
    
    add_action( 'admin_enqueue_scripts', 'shea_enqueue_date_picker' );
    
    function shea_save_user_dateofbirth( $user_id ) {
    	if ( current_user_can( 'edit_user', $user_id ) ) { 
    		update_user_meta( $user_id, 'dateofbirth', $_POST['dateofbirth'] );
    	}
    }
    
    add_action( 'personal_options_update', 'shea_save_user_dateofbirth' );
    add_action( 'edit_user_profile_update', 'shea_save_user_dateofbirth' );

    Unfortunately, it’s not possible to insert a new field into any existing section aside from ‘Personal Options’, so I’ve had to add it in a new section near the bottom.

    Thread Starter piitr64

    (@piitr64)

    Thanks.
    Working perfect.

    Plugin Author Shea Bunge

    (@bungeshea)

    Excellent, great to hear

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Date Picker’ is closed to new replies.