允许登录用户使用单选按钮(是和否)将其作者个人资料从用户列表中排除

时间:2016-11-14 作者:Zeljko Radic

如果有人能为我指出正确的方向,我将不胜感激,因为在解决以下问题时,我陷入了困境。

所以我有一个WordPress用户的目录列表(http://192.99.201.241/~alumni/alumni-directory/). 我使用get\\u users查询输出用户。我想创建一个函数,让登录的用户能够选择是否要从目录列表中包括/排除他们。

任何帮助都会有帮助。

1 个回复
最合适的回答,由SO网友:Samuel LOL Hackson 整理而成

我会添加一个元键和值to each user first 然后添加meta_keymeta_value 到get\\u users参数数组。

例如,可以调用您的元密钥showme 价值是"yes". 所以只要作者的元值是"no", get_users() 将排除该作者。

Add this code to your functions.php:

<?php // you might not need this line when pasting into functions.php
add_action( \'show_user_profile\', \'so_show_extra_radios\' );
add_action( \'edit_user_profile\', \'so_show_extra_radios\' );

function so_show_extra_radios( $user ) { ?>
    <table class="form-table">
        <tr>
            <th>
                <label for="showme">Show profile</label>
            </th>
            <td>
                <input type="radio" name="showme" value="yes" <?php 
                    if (esc_attr(get_the_author_meta(\'showme\', $user->ID)) == "yes")
                        echo "checked";
                ?>></input> Yes<br>
                <input type="radio" name="showme" value="no" <?php 
                    if (esc_attr(get_the_author_meta(\'showme\', $user->ID)) == "no")
                        echo "checked";
                ?>></input>
            </td>
        </tr>
    </table>
<?php }

add_action( \'personal_options_update\', \'so_save_profile\' );
add_action( \'edit_user_profile_update\', \'so_save_profile\' );

function so_save_profile( $user_id ) {
    if ( !current_user_can( \'edit_user\', $user_id ) ) return false;

    if (wp_kses_post( $_POST[\'showme\'] ) == \'yes\')
        update_usermeta( absint( $user_id ), \'showme\', "yes" );
    else
        update_usermeta( absint( $user_id ), \'showme\', "no" );
}
然后添加您的meta_keymeta_value 作者中的参数。php(或任何检索作者配置文件的地方)as described here.

相关推荐

如何从USERS-edit.php中删除颜色选择器代码

我知道我不应该改变Wordpress的核心。但如果我想在“用户编辑”中使用仪表板用户配置文件页面。php和删除大块代码(如颜色选择器)的方法。从第259行到第336行-我想全部删除。<?php if ( ! ( IS_PROFILE_PAGE && ! $user_can_edit ) ) : ?> <tr class="user-rich-editing-wrap"> <th scope="