在自动创建的页面上提取和显示用户信息

时间:2015-02-23 作者:Dz1

在为新用户创建页面时,是否有方法获取下面的代码块以提取给定用户信息的列表。它为新用户创建了一个新页面,但我希望它能够从用户配置文件中提取并显示一些信息。

function my_create_page($user_id){
    $the_user = get_userdata($user_id);
    $new_user_name = $the_user->user_login;
    $my_post = array();
    $my_post[\'post_title\'] = $new_user_name;
    $my_post[\'post_type\'] = \'page\';
    $my_post[\'post_content\'] = \'\';
    $my_post[\'post_status\'] = \'publish\';
    $my_post[\'post_theme\'] = \'user-profile\';
    wp_insert_post( $my_post );
}
add_action(\'user_register\', \'my_create_page\');

2 个回复
SO网友:Tim Malone

如果我正确理解您的问题,您应该能够从$the\\u用户那里获取您想要的信息。

如果您使用:

$my_post[\'post_content\'] = print_r($the_user,true);
您应该能够在页面中查看所有可用的用户信息。一旦你确切知道你想要什么,你就可以很好地用以下内容来回应它:

$my_post[\'post_content\'] = "Username: ".$the_user->user_login."<br />Roles:".$the_user->roles;
如果那不是你想要的,告诉我!

SO网友:3pepe3

您的问题的解决方案可以通过页面模板来完成,但首先我们需要修复您的预览代码

<?php
/**/
function add_user_page($user_id){
    $user = get_userdata($user_id);
    $role_with_page = "subscriber";
    if( reset($user->roles) != $role_with_page ) return; //Exit if user role doesn\'t allows ownership of page
    $post_type = \'page\'; /// I would prefer a custom post type like \'user_upages\'
    global $wpdb;
    $where = get_posts_by_author_sql( $post_type, true, $user_id );
    if( $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->posts $where" ) >= 1 ) return; // Exit if the user already have a page
    $title = $user->user_login;
    $template = \'user-profile.php\'; // if the post type where a CPT you could create just the file single-$posttype.php
    $user_page = array(
        \'post_title\'    => $title,
        \'post_type\'     => $post_type,
        \'post_content\'  => \'\', // leave empty the post content so the user can edit this just in case than you will enable this option...
        \'post_author\'   => $user_id,
        \'post_status\'   => \'publish\',
        \'post_theme\'    => \'user-profile\', // ???? Are you talkin about page_template ?
        \'page_template\' => $template
        );
    if( $post_id = wp_insert_post( $user_page ) ){
        if( $post_type == \'page\' ) update_post_meta( $post_id, \'_wp_page_template\',  $template );
        __update_post_meta( $post_id, \'foobar\', \'some value\' ); // Save all the data you need as post_meta
        __update_post_meta( $post_id, \'user_owner\', $user_id ); // This is just in case you dont want to give ownership (post_author) to the user
        update_user_meta( $user_id, \'user_owns_unique_page\', $post_id ); // Store the page owned by this user
        update_user_meta( $user_id, \'some_random_stuff\', rand( 0,10 ) ); // Store the page owned by this user
    }
    //do_action(\'acf/save_post\' , $post_id); /* Uncomment this if you use ACF (than you should) ;) */
    return $post_id;
}
add_action( \'user_register\', \'add_user_page\' );
add_action( \'set_user_role\', \'add_user_page\' );

?>
您应该保持post\\u内容的原样,因为这是静态数据,不会检索用户数据。如果您真的想更新post\\u内容,那么只需添加一个短代码(您应该在functions.php中编写短代码)。例如:。

\'post_content\'  => "[show_user_data id=\'{$user_id}\']",
然后,如果要使用page\\u模板,请使用您的用户配置文件。php应该像这样:

<?php
/*
 * Template Name: User profile page
 * Description: A Page Template for users.
 */

get_header();

while ( have_posts() ) :
    the_post();
    $user_id = get_the_author_meta( "ID" ); // or get_post_meta( get_the_ID(), \'user_owner\', true );
    $user = get_userdata( $user_id );
    $user_name = $user->first_name." ".$user->last_name;
    $some_random_stuff = get_user_meta( $user_id, \'some_random_stuff\', $single ); 
    $foobar = get_post_meta( get_the_ID() , \'foobar\', true );
    ?>
    <ul>
        <li><?php echo $user_name ?></li>
        <li><?php echo $some_random_stuff ?></li>
        <li><?php echo $foobar ?></li>
        <li><?php the_content(); ?></li>
    </ul>
    <?php
endwhile;
get_footer();
?>
您可以阅读以下内容:https://codex.wordpress.org/Page_Templates

希望这有帮助。

结束

相关推荐

设置所有常见的PHP/WordPress版本组合的漫游脚本

我正在寻找一种快速重复设置的方法testing / debugging environment 对于我们的插件,很可能使用Vagrant. 项目,如VVV 专注于单个环境设置(或几个环境,如stable/trunk),而我要寻找的是一个脚本,它可以设置如下环境:wp39-php52。本地wp40-php52。本地wp41-php52。局部wp39-php53。本地wp40-php53。本地的等等(你知道的)有这样的事情吗?我找到的最接近的是WordPress Vagrant Boxes 至少PHP版本是这