0

I created 2 custom fields for users on my site, a Company name and a checkbox for whether a user has download access or not. Here is the snippet for the functionality:

add_action( 'edit_user_profile', 'custom_profile_fields' );

function custom_profile_fields( $user ) {

    $company = get_user_meta( $user->ID, 'company', true );
    add_user_meta( 'user', 'canDownload', array( 'type' => 'bool', 'single' => true, 'default' => false ) );

    ?>
        <h3>Company Information</h3>
        <table class="form-table">
            <tr>
                <th><label for="company">Company</label></th>
                <td>
                    <input type="text" name="company" id="company" value="<?php echo esc_attr( $company ) ?>" class="regular-text" />
                </td>
            </tr>
            <?php if ( current_user_can( 'administrator' ) ) : ?>
            <tr>
                <th><label for="canDownload">Can Download</label></th>
                <td>
                    <input type="checkbox" name="canDownload" id="canDownload" value="1" <?php if ( get_user_meta( get_current_user_ID(), 'canDownload', true ) ) echo 'checked'; ?> />
                </td>
            </tr>
            <?php endif; ?>
        </table>
    <?php
}

add_action( 'personal_options_update', 'custom_save_profile_fields' );
add_action( 'edit_user_profile_update', 'custom_save_profile_fields' );
 
function custom_save_profile_fields( $user_id ) {
    
    if( ! isset( $_POST[ '_wpnonce' ] ) || ! wp_verify_nonce( $_POST[ '_wpnonce' ], 'update-user_' . $user_id ) ) {
        return;
    }
    
    if( ! current_user_can( 'edit_user', $user_id ) ) {
        return;
    }
 
    update_user_meta( $user_id, 'company', sanitize_text_field( $_POST[ 'company' ] ) ); 
    
    if ( ! empty( $_POST['canDownload'] ) ) {
        update_user_meta( get_current_user_id(), 'canDownload', true );
    } else {
        update_user_meta( get_current_user_id(), 'canDownload', false );
    }
    
}

function custom_new_modify_user_table( $column ) {
    $column['company'] = 'Company';
    return $column;
}
add_filter( 'manage_users_columns', 'custom_new_modify_user_table' );

function custom_new_modify_user_table_row( $val, $column_name, $user_id ) {
    switch ($column_name) {
        case 'company' :
            return get_the_author_meta( 'company', $user_id );
        default:
    }
    return $val;
}
add_filter( 'manage_users_custom_column', 'custom_new_modify_user_table_row', 10, 3 );

function custom_my_sortable_cake_column( $columns ) {
    $columns['company'] = 'Company';

    return $columns;
}
add_filter( 'manage_users_sortable_columns', 'custom_my_sortable_cake_column' );

I later on use this "canDownload" field to set data-attributes on some HTML to implement a logic for downloading:

    $can_download = get_user_meta($current_user->ID, 'canDownload', true);
    if ( $can_download ) {
        $p->set_attribute( 'data-canDownloadPdf', 'true' );
    }
    else {
        $p->set_attribute( 'data-canDownloadPdf', 'false' );
    }

I am having some issues with this implementation and could use some help. Firstly, newly created users have the field "canDownload" checked by default, I'm not sure why.

And secondly, for some users the 2nd code correctly retrieves the value and sets the correct data attribute, and for most it does not. I'm not sure what I'm doing wrong.

1
  • Looking on the documentation for add_user_meta() it seems like you are not using it correctly. The first parameter should be an user id so I guess what you would like to do is add_user_meta($user->ID, 'canDownload', false, true). Also, the second code probably works only when you really have valid meta data set for the user? Commented Apr 10 at 15:46

0

Browse other questions tagged or ask your own question.