Wordpress:为用户创建新的 usermeta 字段

发布于 2024-11-09 18:36:43 字数 1145 浏览 0 评论 0原文

如何创建带有下拉选择值的新用户元字段?

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

例如,

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

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

我正在使用 wp_get_current_user 函数(),它完全满足我的需要,但我只需要一个新的自定义用户元字段。在示例中,新的用户元字段将为“artwork_approved”。

示例:

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

似乎没有可用的插件,但我确实需要此功能。我真的很感激一些关于创建带有下拉选项的新用户元的建议。

*更新:

我使用 Register Plus Redux 创建了一个新的用户元字段,名为“Artwork Approved”。我将其设置为下拉选项,其中包含“否”和“是”选项。默认设置为“否”选项。

这创建了“Artwork Approved”用户元字段。我管理用户帐户并选择“是”或“否”。现在,通过这个新的用户元字段,我使用一个函数来检查当前用户的艺术品是否已批准且值为“是”。然后它应该显示特定的代码。

这是我与新 usermeta 字段一起使用的 if 语句:

<?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 语句也只会显示“其他”部分。

我不知道为什么它不识别我在声明中的“是”选项。

谢谢

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.

For example,

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.

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."

Example:

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.

*UPDATE:

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.

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.

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 } ?> 

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 don't know why it isn't recognizing the Yes Option as I have it in the statement.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

挥剑断情 2024-11-16 18:36:46

第二个代码块不应该读取:

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', $_POST['artwork_approved']);
}

update_user_meta 保存的值是 $_POST['artwork_approved'] 而不是 isset($_POST['artwork_approved'] )

Shouldn't the second block of code read:

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', $_POST['artwork_approved']);
}

The value saved by update_user_meta is $_POST['artwork_approved'] not isset($_POST['artwork_approved']).

吃素的狼 2024-11-16 18:36:45

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

要将字段添加到表单,您可以挂钩 show_user_profileedit_user_profile 操作并输出表单字段 HTML。下面使用复选框而不是下拉菜单。

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_updateedit_user_profile_update 操作,获取字段的值并将其保存为用户元数据。

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)) {

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

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 
}

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']));
}

Your condition would then be as below.

if (get_user_meta($current_user->ID, 'artwork_approved', true)) {
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文