Add new user image field to new user screen
Support › MB User Meta › Add new user image field to new user screenResolved
- This topic has 7 replies, 2 voices, and was last updated 7 years, 7 months ago by
Truong Giang.
-
AuthorPosts
-
August 30, 2017 at 4:57 PM #6817
sociality
ParticipantHello,
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'> <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>
Then I save the field like this:
if ( isset( $_POST['submit_image_selector'] ) && 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' => esc_html__( 'Logo', 'andydote-eshop-plugin' ), 'id' => $prefix . 'brand_image', 'type' => '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.
August 31, 2017 at 8:44 AM #6822Truong Giang
ParticipantHi there,
Can you give me the full code (where you put setting interface, saving data code, register meta box code)?
Thank you.
August 31, 2017 at 3:11 PM #6836sociality
ParticipantHello,
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!
September 1, 2017 at 9:09 AM #6847Truong Giang
ParticipantYou 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'] ) );
September 4, 2017 at 2:52 PM #6861sociality
ParticipantOk,
You are right. Thank you. I will try it out and get back to you since there is a need.
September 5, 2017 at 9:39 AM #6881Truong Giang
ParticipantOk,
Keep me up to date. Thank you.
September 5, 2017 at 3:15 PM #6891sociality
ParticipantHello 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.
September 6, 2017 at 4:52 PM #6911Truong Giang
ParticipantThat'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.
-
AuthorPosts
- You must be logged in to reply to this topic.