Skip to:
Content
Pages
Categories
Search
Top
Bottom

Add custom field to profile general settings and save it


  • had_hc
    Participant

    @had_hc

    Hi, I’m trying to add some custom fields to the buddypress profile general settings section by modifying the general.php template. There’s no issue in outputting the fields. My issue is only saving the field. From what I know, I can use the wp_nonce_field( 'bp_settings_general' ); nonce in my function to save the field.
    Say I have these in general.php

    <input type="hidden" name="user_id" id="user_id" value="<?php echo $user_id; ?>" />
    <label for="billing_phone" class="">Phone <abbr class="required" title="required">*</abbr></label>
    <input type="tel" class="settings-input " name="billing_phone" id="billing_phone" placeholder="" autocomplete="tel" value="<?php echo get_user_meta($user_id, 'billing_phone', true); ?>"></p>

    Here’s what i did to save it (I put it in function.php)

    unction save_wc_address () {
    	if ( wp_verify_nonce( $_POST['bp_settings_general'] ) ){		
    
    		$user_id = sanitize_text_field( $_POST['user_id'] );
    
    		//sanitize input data
    		$billing_phone = sanitize_text_field( $_POST['billing_phone'] );
    
    		//update user meta
    		update_user_meta( $user_id, 'billing_phone', $billing_phone);
    
    	}
    }
    add_action( 'bp_init', 'save_wc_address' );

    As I said above, it’s not saving when I change the phone number and click submit. Can anybody point me out what I did wrong? Thanks in advance!

Viewing 8 replies - 1 through 8 (of 8 total)

  • coffeywebdev
    Participant

    @coffeywebdev

    I think ‘bp_init’ is the wrong hook.. Try using ‘bp_core_signup_user’

    function save_wc_address() {
     	if ( wp_verify_nonce( $_POST['bp_settings_general'] ) ){		
    
    		$user_id = sanitize_text_field( $_POST['user_id'] );
    
    		//sanitize input data
    		$billing_phone = sanitize_text_field( $_POST['billing_phone'] );
    
    		//update user meta
    		update_user_meta( $user_id, 'billing_phone', $billing_phone);
    
    	}
     }
     add_action( 'bp_core_signup_user', 'save_wc_address');

    source:
    http://wordpress.stackexchange.com/questions/160475/how-to-increase-password-requirements-for-registration


    coffeywebdev
    Participant

    @coffeywebdev

    or simply the ‘user_register’ action hook should work? let me know if that works for you


    shanebp
    Moderator

    @shanebp

    modifying the general.php template

    There is no such template in a standard BP install, afaik.

    But there is an easier way to add a field…
    Why not add a profile field in the proscribed manner?
    In wp-admin, go to Users > Profile Fields and add your field to whichever Group you want.
    All fields in the Base Group will appear on the Registration page.


    had_hc
    Participant

    @had_hc

    Hi shanebpb, what ure referring to is the edit profile tab under the Profile nav menu. What i’m trying to do is add custom field to the General tab under the Settings nav menu. The template I’m talking about is at /buddypress/members/single/settings/ folder of the template folder. Sorry if i wasn’t being clear enough before this.
    That said, having the field displayed is not the issue I’m having, saving th custom field is my issue here. The nonce i was talking about is there in the general.php file and it’s within the <form></form> section, so i figure it can be used somehow to save the field.
    I’ll try what coffeywebdev suggested by using different action hook instead of the ‘init’ hook I’m using now.


    had_hc
    Participant

    @had_hc

    So, after more reading, I’ve managed to solved this (90% solved I would say) by changing the line if ( wp_verify_nonce( $_POST['bp_settings_general'] ) ) that i wrote in the first post to if ( isset($_POST['submit']) && check_admin_referer('bp_settings_general') ). And now my field is saved!

    I would like to say sorry for not studying enough about using wp_verify_nonce() in processing form.

    However, there still a bit of an issue left. I still got ‘No changes were made to your account.’ notification warning in a red box under the cover banner image even though my input is saved. Would anybody point me to what function or hook to use to tell the notification that something has been saved? Thanks!


    shanebp
    Moderator

    @shanebp

    You were clear enough about it being settings – I misread your posts.

    You should use this hook: do_action( 'bp_core_general_settings_after_save' );
    Found in: buddypress\bp-settings\bp-settings-actions.php

    Changing the feedback message is more difficult.
    You could change the content of the message by using this hook:
    apply_filters( 'bp_core_render_message_content', $bp->template_message, $type );
    Found in: buddypress\bp-core\bp-core-functions.php

    But I don’t see how you can change the type of message from error to success.

    So what I would do is – create a new tab on the Settings panel and handle everything separate from the General tab functionality.

    There is lots of documentation about that task, this will get you started…

    
    function had_setup_biz_phone_navigation() {
       bp_core_new_subnav_item( array(
    	'name'                    => 'Billing Phone',
    	'slug'                    => 'bphone',
    	'parent_url'              => bp_loggedin_user_domain() . 'settings/',
    	'parent_slug'             => 'settings',
    	'screen_function'         => 'name of the function to create the content',
    	'show_for_displayed_user' => false
       ) );
    }
    add_action( 'bp_setup_nav',   'had_setup_biz_phone_navigation' );
    

    had_hc
    Participant

    @had_hc

    Ah, I guess i should open up a new tab then to solve the notification issue. Thanks for the help anyway.


    had_hc
    Participant

    @had_hc

    After diving more into the filters mentioned by shanebp, I managed to manipulate the notification message. For those interested, I just add bp_core_add_message( $message, $type ); function into the form process function with $message as the message when updated and $type as ‘success’ to give it a green color success notification.

    However, I’m most probably gonna move those fields to their own tab anyway.

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