Wordpress:为用户创建一个新的usermeta字段 [英] Wordpress: Create a new usermeta field for users

查看:460
本文介绍了Wordpress:为用户创建一个新的usermeta字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何创建具有下拉选择值的新usermeta字段?

How do I create a new usermeta field with a drop down selection values?

我想为具有新值的所有用户创建条件语句我想要的自定义字段。

Im want to create a conditional statement for all users with a certain value of the new custom field I want.

例如,

新字段为:已批准
下拉值是:是和否

The new field would be: Approved The drop down values are: Yes and No

条件语句将识别批准字段值为是的所有用户。然后它将发布代码。

The conditional statement will recognize all users with the Approved field value of Yes. Then it will post a code.

我正在使用wp_get_current_user function(),它确实满足我的需要,但我只需要一个新的自定义usermeta字段。在示例中,新的usermeta字段将为 artwork_approved。

Im working with the wp_get_current_user function(), which does exactly what I need, but I just need a new custom usermeta field. In the example the new usermeta field would be "artwork_approved."

示例:

wp_get_current_user();
if ($current_user->artwork_approved == 'Yes'){
    echo 'Thank you for approving your artwork!';
}

似乎没有插件,我真的需要此功能。我非常感谢有关使用下拉选项创建新用户元数据的一些建议。

There seems to be no plugin for this and I really need this feature. I would really appreciate some advice on creating a new usermeta with drop down options.

* 更新:

我使用Register Plus Redux创建了一个新的usermeta字段,称为 Artwork Approved。我将其设置为下拉选项,并带有否和是选项。

I used the Register Plus Redux to create a new usermeta field, called "Artwork Approved." I made it a drop down option, with options "No" and "Yes." The No option is set as default.

这将创建 Artwork Approved usermeta字段。我管理用户帐户,然后选择是或否。现在,在此新的usermeta字段中,Im使用了一种功能,该功能应检查当前用户是否具有值为是的批准艺术品。

This created the "Artwork Approved" usermeta field. I manage user accounts and choose either Yes or No. Now with this new usermeta field, Im using a function that should check if the current user has the Artwork Approved with value of Yes. Then it is supposed show a certain code.

这是在新的usermeta字段中使用的if语句:

Here is the if statement Im using with the new usermeta field:

<?php global $current_user; get_currentuserinfo(); if ($current_user->artwork_approved == 'Yes') { ?>

echo 'Your artwork is approved';

<?php } else { ?>         

echo 'Your artwork is not approved';

<?php } ?> 

但是发生的是它没有识别if语句的第一部分。如果我登录了任何已批准艺术品的帐户,即使我为已批准艺术品选择了是,if语句也仅显示 else部分。

But what's happening is it's not recognizing the first part of the if statement. If I log into any account with the artwork approved, the if statement only shows the "else" part even if I have the option "Yes" for Artwork Approved.

I不知道为什么它不能像我在语句中那样识别Yes选项。

I don't know why it isn't recognizing the Yes Option as I have it in the statement.

谢谢

推荐答案

您可以创建一个简单的插件来挂接到用户配置文件操作并添加新字段。

You can create a simple plugin to hook into the user profile actions and add a new field.

添加该字段到表单,您可以插入 show_user_profile edit_user_profile 操作并输出表单字段HTML。以下使用复选框而不是下拉菜单。

To add the field to the form you can hook into the show_user_profile and edit_user_profile actions and output the form field HTML. The below uses a checkbox rather than a drop-down.

add_action('show_user_profile', 'my_user_profile_edit_action');
add_action('edit_user_profile', 'my_user_profile_edit_action');
function my_user_profile_edit_action($user) {
  $checked = (isset($user->artwork_approved) && $user->artwork_approved) ? ' checked="checked"' : '';
?>
  <h3>Other</h3>
  <label for="artwork_approved">
    <input name="artwork_approved" type="checkbox" id="artwork_approved" value="1"<?php echo $checked; ?>>
    Artwork approved
  </label>
<?php 
}

然后,您需要加入 personal_options_update edit_user_profile_update 操作,获取字段的值并将其保存为用户元。

Then you need to hook into the personal_options_update and edit_user_profile_update actions, get the value of your field and save this as user meta.

add_action('personal_options_update', 'my_user_profile_update_action');
add_action('edit_user_profile_update', 'my_user_profile_update_action');
function my_user_profile_update_action($user_id) {
  update_user_meta($user_id, 'artwork_approved', isset($_POST['artwork_approved']));
}

您的情况将如下所示。

if (get_user_meta($current_user->ID, 'artwork_approved', true)) {

这篇关于Wordpress:为用户创建一个新的usermeta字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆