【发布时间】:2018-09-15 20:36:20
【问题描述】:
当钩子被触发/触发时,如何向用户配置文件中的自定义文本字段添加值。我已经能够使用以下代码添加一个名为 example 的自定义字段
add_action( 'show_user_profile', 'extra_user_profile_fields' );
add_action( 'edit_user_profile', 'extra_user_profile_fields' );
function extra_user_profile_fields( $user ) { ?>
<h3><?php _e("Example Section", "blank"); ?></h3>
<table class="form-table">
<tr>
<th><label for="example"><?php _e("Example"); ?></label></th>
<td>
<input type="text" name="example" id="example" value="<?php echo esc_attr( get_the_author_meta( 'example', $user->ID ) ); ?>" class="regular-text" /><br />
<span class="description"><?php _e("This field should add YES when to field when a hook is triggered ad empty if hook not triggered."); ?></span>
</td>
</tr>
</table>
<?php }
并使用以下代码保存输入
add_action( 'personal_options_update', 'save_extra_user_profile_fields' );
add_action( 'edit_user_profile_update', 'save_extra_user_profile_fields' );
function save_extra_user_profile_fields( $user_id ) {
if ( !current_user_can( 'edit_user', $user_id ) ) {
return false;
}
update_user_meta( $user_id, 'example', $_POST['example'] );
}
我希望这个新生成的自定义字段在触发 user_register 挂钩时填充 YES(即,当用户注册时,应将 YES 添加到字段并更新)
这样我就可以获得 YES 值并使用它来动态显示内容。类似的东西
$user = wp_get_current_user();
if ( get_the_author_meta( 'example', $user->ID ) = 'YES') {
//Show this page
} else {
return 'Your are not allowed to view this page';
}
我怎样才能做到这一点?谢谢
【问题讨论】: