如果用户已登录,则仅显示特定页面

时间:2011-11-03 作者:Howdy_McGee

我有一个wordpress网站,用户必须登录才能看到他们的特定图库。我的问题是,我们的所有用户都是成员,所有成员都可以看到所有其他成员页面。这很难解释,所以我会把你们和我的联系起来。

登录此客户端并将鼠标悬停在“Members”>“Members Page”上后,可以清除其他客户端库,即使它们都需要密码。如何隐藏所有其他页面?我曾想过让页面标题与客户名称相同,但这可能会带来太多不便。有什么建议或帮助吗?

我认为比较userID和galleryID是可行的,但只有在一个完美的世界里。如果发生了一件不幸的事,那么整个比较都会失败。

然后我想把标题和用户名进行比较,但拼写错误的可能性太大了,我觉得这会让我很沮丧。

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

大问题。

因此,首先,为了使库保持独立,最好为它们提供自己的自定义帖子类型。我们稍后将对此进行检查。。。

<?php
add_action( \'init\', \'wpse32840_register_post_type\' );
function wpse32840_register_post_type()
{
    register_post_type(
        \'client_gallery\',
        array(
            \'label\'                 => __( \'Client Galleries\' ), // probably needs better lables
            \'public\'                => true, // allow people to see this on the front end
            \'exclude_from_search\'   => true, // cant be brought up via a search
            \'show_in_nav_menus\' => false, // not in nav menus (we\'ll do this manually)
            \'rewrite\'               => array( \'slug\' => \'gallery\' )
        )
    );      
}
要为每个用户创建库,请连接到user_register 并插入新的client_gallery 邮递然后设置usermeta 值。我们可以稍后将其用于许多内容。它也比检查作者或帖子等更可靠。

<?php
add_action( \'user_register\', \'wpse32840_capture_register\' );
function wpse32840_capture_register( $user_id )
{
    $userobj = get_user_by( \'id\', $user_id );

    $gallery_id = wp_insert_post(
        array(
            \'post_type\'     => \'client_gallery\',
            \'post_title\'    => sprintf( \'%s gallery\', $userobj->user_login )
        ), 
        true
    );

    if( is_wp_error( $gallery_id ) ) $gallery_id = 0;

    update_user_meta( $user_id, \'wpse32840_gallery_id\', absint( $gallery_id ) );
}
您还应该能够从管理区域更改库,因此edit_user_profile 添加下拉列表以更改用户的库。还挂钩到edit_user_profile_update 保存更新后的新ID。

<?php
add_action( \'edit_user_profile\', \'wpse32840_user_profile\' );
function wpse32840_user_profile( $user )
{
    $gallery_id = get_user_meta( $user->ID, \'wpse32840_gallery_id\', true );

    // get all of our galleries!
    $galleries = get_posts(
        array(
            \'post_type\'     => \'client_gallery\',
            \'numberposts\'   => -1,
            \'post_status\'   => \'any\'
        )
    );

    // no galleries?  bail.
    if( ! $galleries ) return;

    // nonce
    wp_nonce_field( \'wpse32840_nonce\', \'wpse32840_nonce\', false );

    // our fields
    echo \'<h3>\' . __( \'Gallery\' ) . \'</h3>\';
    echo \'<select name="wpse32840_gallery">\';
    echo \'<option \' . selected( $gallery_id, 0, false ). \' value="0">No gallery</option>\';
    foreach( $galleries as $g )
    {
        echo \'<option \' . selected( $gallery_id, $g->ID, false ) . \' value="\' . absint( $g->ID ) . \'">\' . esc_html( $g->post_title ) . \'</option>\';
    }
    echo \'</select>\';
}

/**
 * Save the user\'s gallery ID from the user edit page.
 */
add_action( \'edit_user_profile_update\', \'wpse32840_save_user\' );
function wpse32840_save_user( $user_id )
{
    if( ! isset( $_POST[\'wpse32840_nonce\'] ) || ! wp_verify_nonce( $_POST[\'wpse32840_nonce\'], \'wpse32840_nonce\' ) ) 
        return;

    if( ! isset( $_POST[\'wpse32840_gallery\' ] ) )
        return;

    update_user_meta( $user_id, \'wpse32840_gallery_id\', absint( $_POST[\'wpse32840_gallery\'] ) );
}
接下来,连接到template_redirect 并阻止普通用户访问这些client\\u gallery页面。您也可以在这里获取当前用户及其对应的库。如果您正在查看的帖子与他们的图库不匹配,请将其返回主页。

<?php
add_action( \'template_redirect\', \'wpse32840_check_user\' );
function wpse32840_check_user()
{
    // not on a client gallery?  bail
    if( ! is_singular( \'client_gallery\' ) ) return;

    // is the is an admin?
    if( current_user_can( \'manage_options\' ) ) return;

    // thrown non logged in users back to the home page
    if( ! is_user_logged_in() )
    {
        wp_redirect( home_url(), 302 );
        exit();
    }

    $user = wp_get_current_user();
    $gallery_id = get_queried_object_id();

    // if the user isn\'t an admin or isn\'t assigned this gallery, send them back to the home page
    if( $gallery_id != get_user_meta( $user->ID, \'wpse32840_gallery_id\', true ) )
    {
        wp_redirect( home_url(), 302 );
        exit();
    }
    // By here, the user is authenticated, let them continue on their merry way.
}

And, finally, what you really wanted to know!

最后,你可以加入wp_nav_menu_items 更改给定菜单的输出。这是一个非常简单的示例,仅在用户登录时返回主页链接和库链接。您可以在此处为登录用户构建自己的菜单,也可以使其保持简单。

<?php
add_filter( \'wp_nav_menu_items\', \'wpse32840_filter_nav\', 10, 2 );
function wpse32840_filter_nav( $items, $args )
{
    /**
     * you can check for theme location here via $args... check to see if this is the main nav
     * in twenty eleven, we can check to see if this is the "primary" menu
     * Other thigns will be different.  If we\'re not in the primary menu, bail
     */
    if( \'primary\' != $args->theme_location ) return $menu;

    // Not logged in?  return the menu
    if( ! is_user_logged_in() ) return $items;

    // if this is an admin, return the menu unaltered.
    if( current_user_can( \'manage_options\' ) ) return $items;

    // our user is logged in an not an admin, build them a new menu

    // get our current user
    $user = wp_get_current_user();

    // get the users gallery
    $gallery_id = get_user_meta( $user->ID, \'wpse32840_gallery_id\', true );
    $gallery = get_post( absint( $gallery_id ) );

    $items = \'<li class="menu-item"><a href="\' . esc_url( home_url() ) . \'">Home</a></li>\';
    $items .= \'<li class="menu-item"><a href="\' . esc_url( get_permalink( $gallery ) ) . \'">Gallery</a></li>\';
    return $items;
}
这就是整件事as a plugin.

SO网友:Kevin Langley Jr.

您只需使用wp\\u get\\u current\\u user()检查帖子的作者,甚至可以为允许查看特定帖子并进行检查的用户ID设置帖子元。

结束

相关推荐

Membership Plugin

我正在为高级博客内容寻找一个好的插件。我的客户希望提供30次免费试用以及每月/6个月/每年付费的计划。它应该为每个人显示一个除此之外的帖子,即使他们作为客人在网站上,没有任何会员资格、试用或特价。它还需要通过贝宝接收付款,并在付款后自动打开订阅。我想能够自定义注册页面,以添加地址和电话号码等其他字段。