如何显示用户特定的内容?

时间:2011-04-10 作者:hannit cohen

我有一个只向注册用户显示内容的网站。大部分内容可供所有用户使用,但其中一些内容是特定于用户的(即所有用户都有该页面,但每个用户都可以看到其他人看不到的自己的内容)。对于如何限制(和显示)每个特定用户的特定内容,有什么想法吗?

4 个回复
最合适的回答,由SO网友:Bainternet 整理而成

不久前我编写了类似的代码,我记得我想将其上载到插件库,但一直没有时间,基本上它会在帖子或页面编辑屏幕上添加一个元框,让用户按名称或角色选择特定用户,然后使用the_content 过滤器,所以您可以:

更新:

它刚刚在WordPress插件库中获得批准,您可以下载它User specific content 在此处或仪表板上形成表单i wrote a little about it here.

enter image description here

<?php
/*
Plugin Name: User Specific Content
Plugin URI: http://en.bainternet.info
Description: This Plugin allows you to select specific users by user name, or by role name who can view a  specific post content or page content.
Version: 0.1
Author: Bainternet
Author URI: http://en.bainternet.info
*/

/* Define the custom box */
add_action(\'add_meta_boxes\', \'User_specific_content_box\');

/* Adds a box to the main column on the custom post type edit screens */
function User_specific_content_box() {
    add_meta_box(\'User_specific_content\', __( \'User specific content box\'),\'User_specific_content_box_inner\',\'post\');
    add_meta_box(\'User_specific_content\', __( \'User specific content box\'),\'User_specific_content_box_inner\',\'post\');
}

/* Prints the box content */
function User_specific_content_box_inner() {
    global $post,$wp_roles;
    $savedroles = get_post_meta($post->ID, \'U_S_C_roles\',true);
    //var_dump($savedroles);
    $savedusers = get_post_meta($post->ID, \'U_S_C_users\',true);
    //var_dump($savedusers);
    // Use nonce for verification
    wp_nonce_field( plugin_basename(__FILE__), \'User_specific_content_box_inner\' );
    echo __(\'Select users to show this content to\');
    echo \'<h4>\'.__(\'By User Role:\').\'</h4>\';
    if ( !isset( $wp_roles ) )
        $wp_roles = new WP_Roles();
    foreach ( $wp_roles->role_names as $role => $name ) {
        echo \'<input type="checkbox" name="U_S_C_roles[]" value="\'.$name.\'"\';
        if (in_array($name,$savedroles)){
            echo \' checked\';
        }
        echo \'>\'.$name.\'    \';
    }
    echo \'<h4>\'.__(\'By User Name:\').\'</h4>\';
    $blogusers = get_users(\'blog_id=1&orderby=nicename\');
    $usercount = 0;
    foreach ($blogusers as $user) {
        echo \'<input type="checkbox" name="U_S_C_users[]" value="\'.$user->ID.\'"\';
        if (in_array($user->ID,$savedusers)){
            echo \' checked\';
        }
        echo \'>\'.$user->display_name.\'    \';
        $usercount = $usercount + 1;
        if ($usercount > 5){
            echo \'<br/>\';
            $usercount = 0;
        }
    }
    echo \'<h4>\'.__(\'Content Blocked message:\').\'</h4>\';
    echo \'<textarea rows="3" cols="70" name="U_S_C_message" id="U_S_C_message">\'.get_post_meta($post->ID, \'U_S_C_message\',true).\'</textarea><br/>\'.__(\'This message will be shown to anyone who is not on the list above.\');
}


/* Save Meta Box */
add_action(\'save_post\', \'User_specific_content_box_inner_save\');

/* When the post is saved, saves our custom data */
function User_specific_content_box_inner_save( $post_id ) {
    global $post;
      // verify this came from the our screen and with proper authorization,
      // because save_post can be triggered at other times

      if ( !wp_verify_nonce( $_POST[\'User_specific_content_box_inner\'], plugin_basename(__FILE__) ) )
          return $post_id;

      // verify if this is an auto save routine. 
      // If it is our form has not been submitted, so we dont want to do anything
      if ( defined(\'DOING_AUTOSAVE\') && DOING_AUTOSAVE ) 
          return $post_id;
      // OK, we\'re authenticated: we need to find and save the data
    $savedroles = get_post_meta($post_id, \'U_S_C_roles\',true);
    $savedusers = get_post_meta($post_id, \'U_S_C_users\',true);
    if (isset($_POST[\'U_S_C_roles\']) && !empty($_POST[\'U_S_C_roles\'] )){
        foreach ($_POST[\'U_S_C_roles\'] as $role){
            $new_roles[] = $role;
        }
        update_post_meta($post_id, \'U_S_C_roles\', $new_roles);
    }else{
        if (count($savedroles) > 0){
             delete_post_meta($post_id, \'U_S_C_roles\');
        }
    }
    if (isset($_POST[\'U_S_C_users\']) && !empty($_POST[\'U_S_C_users\'])){
        foreach ($_POST[\'U_S_C_users\'] as $u){
            $new_users[] = $u;
        }
        update_post_meta($post_id, \'U_S_C_users\', $new_users);
    }else{
        if (count($savedusers) > 0){
             delete_post_meta($post_id, \'U_S_C_users\');
        }
    }
    if (isset($_POST[\'U_S_C_message\'])){
        update_post_meta($post_id,\'U_S_C_message\', $_POST[\'U_S_C_message\']);
    }
}

add_filter(\'the_content\',\'User_specific_content_filter\');
function User_specific_content_filter($content){
    global $post,$current_user;

    $savedroles = get_post_meta($post->ID, \'U_S_C_roles\',true);
    $run_check = 0;
    $savedusers = get_post_meta($post->ID, \'U_S_C_users\',true);
    if (!count($savedusers) > 0 && !count($savedroles) > 0 )
        return $content;

    if (isset($savedroles) && !empty($savedroles)){
        foreach ($savedroles as $role){
            if (current_user_can($role)) {
                return $content;
                exit;
            }
        }
        //failed role check
        $run_check = 1;
    }
    if (isset($savedusers) && !empty($savedusers)){
        get_currentuserinfo();
        if (in_array($current_user->ID,$savedusers)){
            return $content;
        }
            //failed both checks
        return get_post_meta($post->ID, \'U_S_C_message\',true);
    }
    return $content;
}
?>

SO网友:onetrickpony

假设此内容是常见的post循环:

$current_user = wp_get_current_user();
if(get_the_author_meta(\'ID\') === $current_user->ID):

  // show the content

endif;
我认为这只在循环内部起作用。

如果您需要在循环之外使用它,那么只需从$current_user->ID:

$query = new WP_Query(\'author\' => $current_user->ID);

SO网友:Maor Barazany

除了一个技巧小马所写的之外,如果网站所有者需要将内容限制给每个用户,您可以开发一个小的自定义元框,该框将出现在每个帖子中,并显示与网站用户的复选框,然后您将在一天内看到对内容有权限的用户所需的post\\u元,您可以设置条件。如果您需要另一种控制内容的方式,最好提供一些更具体的细节,说明应该如何处理和需要如何处理,以便更容易找到合适的解决方案。。。

祝你好运:)

SO网友:Jarnail S

第一次进近:

执行此操作的一种方法是为每个学生创建单独的页面,并将此页面设置为密码保护。这个密码可以给学生

您可以使用WP Private Content Plus(https://wordpress.org/plugins/wp-private-content-plus/) 并使每个页面/帖子仅对指定用户可见您也可以使用第二种方法和第一种方法,以增加安全性,尽管它是可选的。

结束

相关推荐