Search code examples
wordpressuser-profile

Add exrtra user approve status filed in user profile, visible to admin only and send mail if checkbox is checked at first time in wordpress?


I want to give the functionality to admin to approve user status manually. I was able to give this functionality, but having some issue in this functionality. How can I hide this field from user's profile for user role. i.e. Field should be visible to admin only. If I hide this filed from user profile using jQuery then when user hits update profile button at that time value is updated. I want value to be same as per user selected (set by admin). I also want to send mail to user if checkbox is selected from uncheck to check, but currently it is sending mail every time. How can i solve out this issue?

/** Add user approve field */ 
add_action( 'user_new_form', 'Add_user_fields' );
add_action( 'edit_user_profile', 'Add_user_fields' );
add_action( 'show_user_profile', 'Add_user_fields' );

function Add_user_fields( $user ) { ?>
    <h2 class="user_prmission">User Permission</h3>

    <table class="form-table">
        <tr class="user-approve_status-wrap">
            <th><label for="dropdown">Approve User Permission</label></th>
            <td>
                <?php 
                    //get dropdown saved value
                    $checked = (isset($user->artwork_approved) && $user->artwork_approved) ? ' checked="checked"' : ''; 
                ?>

                <label for="artwork_approved">
                    <input name="artwork_approved" type="checkbox" id="artwork_approved" value="1" <?php echo $checked; ?>>
                    <?php _e('Approve Status','AA'); ?>
                </label>
            </td>
        </tr>
    </table>
<?php   
}

/* Update selected option **/
add_action( 'user_register', 'save_user_fields');
add_action( 'personal_options_update', 'save_user_fields' );
add_action( 'edit_user_profile_update', 'save_user_fields' );
function save_user_fields( $user_id ) {

    if ( !current_user_can( 'edit_user', $user_id ) )
        return false;

    update_user_meta($user_id, 'artwork_approved', isset($_POST['artwork_approved']));

    /* Send email to agent to notify about thier account is approved. **/ 

     if (get_user_meta($user_id, 'artwork_approved', true)) {
        // sent mail if checkbox is selected from unchecked to checked,and hide this filed from user profile only visible to admin.
    }
}

Solution

  • What I understand from your question is, you have added a custom field on user profile which is a checkbox. You want that field to be editable only by admins and not by the users.

    If this understanding is correct, you can check the user capabilities of current user while rendering the custom field on user profile page. That way, if current user has administrator privileges only then the custom field will be rendered. If a non admin user is logged in then the custom field will not be rendered.

    Also, in your function to add custom field, you are setting the value of checkbox to '1' irrespective of whether it is checked or not. I guess that is causing the email to be sent every time. You should set the value of checkbox based on your condition.

    So your function to add custom field will become as follows.

    /** Add user approve field */ 
    add_action( 'user_new_form', 'Add_user_fields' );
    add_action( 'edit_user_profile', 'Add_user_fields' );
    add_action( 'show_user_profile', 'Add_user_fields' );
    
    function Add_user_fields( $user ) { ?>
        if( !current_user_can( 'manage_options' ) ){
            return;
        }
        <h2 class="user_prmission">User Permission</h3>
        <table class="form-table">
        <tr class="user-approve_status-wrap">
            <th><label for="dropdown">Approve User Permission</label></th>
            <td>
                <?php 
                    //get dropdown saved value
                    if( isset($user->artwork_approved) && $user->artwork_approved ){
                         $checked = 'checked="checked"';
                         $value = '1';
                    }else{
                         $checked = null;
                         $value = null;
                    }
                ?>
    
                <label for="artwork_approved">
                    <input name="artwork_approved" type="checkbox" id="artwork_approved" value="<?php echo $value; ?>" <?php echo $checked; ?>>
                    <?php _e('Approve Status','AA'); ?>
                </label>
            </td>
        </tr>
        </table>
        <?php   
        }
    

    Hope this helps.