Add new user image field to new user screen

Support MB User Meta Add new user image field to new user screenResolved

Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #6817
    socialitysociality
    Participant

    Hello,

    I am trying to add a field in the add new user screen in backend using the "user_new_form" action hook. I just want to use the media upload manager of wordpress as you do.

    My code is this:

        <tr class="form-field">
            <th scope="row"><label for="andydote_brand_image"><?php esc_html_e( 'Logo', 'andydote-eshop-plugin' ) ?> </label></th>
            <td>
                <img />' height='100'&gt;
                &lt;input id="upload_brand_image_button" type="button" class="button" value="&lt;?php esc_html_e( 'Add Media', 'andydote-eshop-plugin' ); ?&gt;" /&gt;
                &lt;input type='hidden' name='andydote_brand_image' id='andydote_brand_image' value='&lt;?php echo get_option( 'media_selector_attachment_id' ); ?&gt;'&gt;
                &lt;input type="submit" name="submit_image_selector" value="Save" class="button-primary"&gt;
            &lt;/td&gt;
        &lt;/tr&gt;
    

    Then I save the field like this:

    if ( isset( $_POST['submit_image_selector'] ) &amp;&amp; isset( $_POST['andydote_brand_image'] ) ) {
        update_user_meta( 'media_selector_attachment_id', absint( $_POST['andydote_brand_image'] ) );
    }
    

    Well the problem is that when I hit "add new user" in order to save the user, when I go to the user details in the backend your plugin does not show the image I saved.

    The code for creating a new user meta field is this:

            array(
                'name' =&gt; esc_html__( 'Logo', 'andydote-eshop-plugin' ),
                'id'   =&gt; $prefix . 'brand_image',
                'type' =&gt; 'image_advanced',
            ),
    

    Is there any way to use your code somehow or instead we should find out why although I save the image in the add new user screen, it does not show up in the user details screen ( user-edit.php ).

    Hope I am clear 🙂

    Thank you in advance.

    #6822
    Truong GiangTruong Giang
    Participant

    Hi there,

    Can you give me the full code (where you put setting interface, saving data code, register meta box code)?

    Thank you.

    #6836
    socialitysociality
    Participant

    Hello,

    Yes of course.

    This is the code for declaring the meta box:

    /***********************************************************************
     * A. ADD META BOXES FOR USERS
     * *********************************************************************/
     function colours_meta_boxes( $meta_boxes ) {
    	$prefix = 'andydote_';
    	
    	// structure
    	$meta_boxes[] = array(
    		'title' => esc_html__('Brand Info', 'andydote-eshop-plugin'),
    		'type'  => 'user',
    		'fields' => array(
    			array(
    				'name' => esc_html__( 'Logo', 'andydote-eshop-plugin' ),
    				'id'   => $prefix . 'brand_image',
    				'type' => 'image_advanced',
    			),
    		),
    
    	);
    
        return $meta_boxes;
    }
    add_filter( 'rwmb_meta_boxes', 'colours_meta_boxes' );

    This is the code for adding the new field in "add new user" screen:

    // 1b.add new form elements in user new screen
    function andydote_user_new_form() {
        ?>
        
        <h2><?php esc_html_e( 'Brand Info', 'andydote-eshop-plugin' ); ?></h2>
        
    	<table class="form-table">
    
    		
    		<tr class="form-field">
    			<th scope="row"><label for="andydote_brand_image"><?php esc_html_e( 'Logo', 'andydote-eshop-plugin' ) ?> </label></th>
    			<td>
    				<img />' height='100'>
    				<input id="upload_brand_image_button" type="button" class="button" value="<?php esc_html_e( 'Add Media', 'andydote-eshop-plugin' ); ?>" />
    				<input type='hidden' name='andydote_brand_image' id='andydote_brand_image' value='<?php echo get_option( 'media_selector_attachment_id' ); ?>'>
    				<input type="submit" name="submit_image_selector" value="Save" class="button-primary">
    			</td>
    		</tr>
    		
    	</table>
    	
        <?php
    }
    add_action( 'user_new_form', 'andydote_user_new_form' );

    This is the code for saving the image field:

    // 3.save our extra registration user meta
    function andydote_user_register( $user_id ) {
        if ( isset( $_POST['submit_image_selector'] ) && isset( $_POST['andydote_brand_image'] ) ) {
    		update_user_meta( 'media_selector_attachment_id', absint( $_POST['andydote_brand_image'] ) );
    	}
    }
    add_action( 'user_register', 'andydote_user_register' );

    If you are interested, I use this reference: http://jeroensormani.com/how-to-include-the-wordpress-media-selector-in-your-plugin/

    Thank you!

    #6847
    Truong GiangTruong Giang
    Participant

    You miss user id at this command:

    
    update_user_meta( 'media_selector_attachment_id', absint( $_POST['andydote_brand_image'] ) );
    

    It should be:

    
    update_user_meta( $user_id, 'media_selector_attachment_id', absint( $_POST['andydote_brand_image'] ) );
    
    #6861
    socialitysociality
    Participant

    Ok,

    You are right. Thank you. I will try it out and get back to you since there is a need.

    #6881
    Truong GiangTruong Giang
    Participant

    Ok,

    Keep me up to date. Thank you.

    #6891
    socialitysociality
    Participant

    Hello Truong,

    Unfortunately it does not seem to work, although I made the modification you mentioned.

    Any other ideas? It seems that the metabox plugin does not retrieve the image I save.

    Thank you in advance.

    #6911
    Truong GiangTruong Giang
    Participant

    That's strange. I followed the article you give but it doesn't work. Please add this code to your plugin and give me the result:

    `
    var_dump( A USER ID, 'media_selector_attachment_id', true );
    `

    A USER ID is the id of a user which has the image.

Viewing 8 posts - 1 through 8 (of 8 total)
  • You must be logged in to reply to this topic.