我以前做过这个,所以我在这里重用代码。你需要做一些调整,使其与你的网站相匹配。您还需要Peter\'s Login Redirect 插件。
我没有解释代码,因为现在完成所有代码需要太多时间。如果您有任何问题,请添加评论,我将尽力解释。我还假设你有WordPress方面的知识
为客户区创建自定义帖子类型
//CLIENT AREA
add_action(\'init\', \'create_client_area\');
function create_client_area() {
register_taxonomy( \'client_category\', array(), array(
\'hierarchical\' => true,
\'labels\' => array(
\'name\' => \'Clients\',
\'singular_name\' => \'Client\'
),
\'show_ui\' => true,
\'query_var\' => true,
\'rewrite\' => array( \'slug\' => \'clientarea\' )
));
register_post_type( \'client\', array(
\'labels\' => array(
\'name\' => \'Client Area\',
\'singular_name\' => \'Client Project\'
),
\'supports\' => array(\'title\', \'editor\', \'thumbnail\', \'comments\'),
\'public\' => true,
\'hierarchical\' => false,
\'show_ui\' => true,
\'show_in_menu\' => true,
\'capability_type\' => \'post\',
\'rewrite\' => array(
\'slug\' => \'clientarea/%client_category%\',
\'with_front\' => false
),
\'query_var\' => true,
//\'has_archive\' => \'work\',
\'has_archive\' => false,
\'taxonomies\' => array( \'client_category\' )
));
}
add_filter(\'post_type_link\', \'client_area_permalink\', 10, 4);
function client_area_permalink($post_link, $post, $leavename, $sample) {
if ( false !== strpos( $post_link, \'%client_category%\' ) ) {
$client_category = get_the_terms( $post->ID, \'client_category\' );
if(!$client_category) { $client_category = array(); }
$post_link = str_replace( \'%client_category%\', array_pop($client_category)->slug, $post_link );
}
return $post_link;
}
为每个用户创建术语
// CLIENT ROLE
add_role( \'client\', \'Client\', array(\'read\') );
// CREATE TERM ON REGISTER
add_action(\'user_register\', \'create_client_category\', 10, 3);
function create_client_category( $user_ID ) {
$user = new WP_User( $user_ID );
if( in_array(\'client\', $user->roles) ) {
$userdata = get_userdata($user_ID);
if( !term_exists($userdata->user_nicename, \'client_category\') ) {
wp_insert_term( $userdata->user_login, \'client_category\', array(
\'slug\' => $userdata->user_nicename ));
}
}
}
添加凭据签入
Specialized Header function show_404() {
global $wp_query;
$wp_query->set_404();
status_header( 404 );
get_template_part( 404 ); exit();
}
if(is_page()) {
if( current_user_can(\'client\') ) {
global $user_ID;
$user = get_userdata( $user_ID );
if(term_exists($user->user_nicename, \'client_category\')) {
wp_redirect( home_url(\'/clientarea/\'.$user->user_nicename.\'/\') ); exit;
} else { show_404(); }
} elseif( !current_user_can(\'administrator\') ) { show_404(); }
} elseif(is_tax()) {
if( !current_user_can(\'administrator\') && get_userdata($user_ID)->user_nicename != $wp_query->queried_object->slug) { // not admin and not user page
show_404();
}
} elseif(is_single()) {
global $user_ID;
$user = get_userdata( $user_ID );
$terms = get_the_terms($post->id, \'client_category\');
foreach( $terms as $term ) {
if($term->slug != $user->user_nicename) $deny = true;
}
if( !current_user_can(\'administrator\') && $deny) {
show_404();
}
} else {
// dont know what goes here...thinking.
}