admin管理员组文章数量:1122846
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.
本文标签: phpProblem with custom user fields default value and retrieval
版权声明:本文标题:php - Problem with custom user fields default value and retrieval 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736310891a1934541.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论