Show a simple public user profile for all registered users (not only for authors)

In a simple WordPress plugin, I redirected commentator links to URLs /user/user_idwith the following code:

define('PROFILE', '<a href="/user/%d">%s</a>');

function my_get_comment_author_link($comment_ID) {
    global $comment;
    return sprintf(PROFILE, $comment->user_id, 
                            $comment->comment_author);
}

add_action('get_comment_author_link',
           'my_get_comment_author_link');

How can I show a simple public user profile (for all registered users, not just authors) that display the username at this URL?

I think I should create a PHP script in my child's 2013 theme -

<?php
/* Template Name: Public User Profile */
?>

<?php get_header(); ?>

<?php 
      $my_query = new WP_Query($args);
      // somehow get the user ID out of URL?
      // somehow get the WP_User object?
      printf('<p>Name: %s</p>', $user->display_name);
?>

<?php get_footer(); ?>

But how to get WordPress to execute this PHP script when the URL is /user/user_idre-set?

Should I use the rewrite API here and how? I tried the following but see no effect:

                    add_rewrite_rule('/user/(\d+)',
                                     'index.php?XXX=$matches[1]',
                                     'top'
                    );
                    flush_rewrite_rules();

- , , , ? ( - " " ).

UPDATE:

the_content hook - , /user/user_id?

function my_content($content)
{
    if (NOT /user/user_id CALLED)
        return $content;

    // otherwise somehow extract the user_id
    // somehow get the corresponding WP_User
    return sprintf('<p>Name: %s</p>', $user->display_name);
}

add_action('the_content', 'my_content');
+4
3

(, ) :

  • , ( ). .

  • add_rewrite_rule init, :

    public static function init()
    {
        // ... your code  ...
    
        add_rewrite_rule(
            'user/([^/]*)/?',
            'index.php?wpcg_user=$matches[1]',
            'top'
        );              
    }
    

    /?, user/123, user/123/. add_rewrite_rule() .

  • - add_rewrite_endpoint:

    public static function init()
    {
        // ... your code  ...
    
        add_rewrite_endpoint( 'user', EP_ALL );
    }
    

    , : EP_ALL, EP_PAGES, EP_PERMALINK, .... , , /user/123/ ?user=123.

  • template_redirect template_include, , .

  • GET, wpcg_user, query_vars, :

    public static function query_vars( $vars )
    {
        $vars[] = 'wpcg_user';
        return $vars;
    }
    

    get_query_var( 'wpcg_user' ).

  • get_query_var( 'wpcg_user') , , URL- http://example.com/user/123/.

  • WordPress tpl-user.php, . locate_template :

    if ( '' != locate_template( 'tpl-user.php') ) 
    
  • template_redirect, , :

    public static function template_redirect()
    {
        $wpcg_user = get_query_var( 'wpcg_user' );
    
        $tpl = 'tpl-user.php';  // Your custom template file.
    
        if( ! empty( $wpcg_user ) )
        {
            if ( '' == locate_template( $tpl, TRUE ) )
                include( plugin_dir_path( __FILE__ ) . $tpl );  
    
            exit();
        }
    }
    

    , , tpl-user.php, , wp-city-gender. , locate_template(), , .

  • tpl-user.php :

    <?php
     /* Template Name: Public User Profile */
     ?>
    
    <?php get_header(); ?>
    
    <div>
    <?php 
    // Get the user query:
    $wpcg_user = get_query_var( 'wpcg_user' );  
    
    // Get corresponding user by id:
    $user = get_user_by( 'id', absint( $wpcg_user ) );
    
    if( $user ):
        printf('<p> Name: %s </p>',   $user->display_name                      );
        printf('<p> City: %s </p>',   get_user_meta( $user->ID, 'city',   TRUE));
        printf('<p> Gender: %s </p>', get_user_meta( $user->ID, 'gender', TRUE));
    else:
        _e( 'Sorry, no user found!' );
    endif;
    ?>
    </div>
    
    <?php get_footer(); ?>
    

    get_user_by() .

  • slug , get_user_by( 'slug', $wpcg_user ); get_user_by( 'id', absint( $wpcg_user ) );.

  • :

    add_action( 'template_redirect',  array( CNAME, 'template_redirect') );
    add_filter( 'query_vars',         array( CNAME, 'query_vars') );
    //add_action( 'template_include', array( CNAME, 'template_include') );
    

    hook.

  • , , Monkeyman Rewrite Analyzer. , .

    , /user/1/, :

    wpcg_user inspection

    , . , . .

Update:

:

, , /templates/profile.php?

:

if ( '' == locate_template( 'templates/profile.php', TRUE ) )
    include( plugin_dir_path( __FILE__ ) . 'templates/profile.php' );

:

  • /wp-content/themes/MYTHEME/templates/profile.php , .

  • : /wp-content/plugins/wp-city-gender/templates/profile.php.

, __FILE__ wp-city-gender.

+5

URL?

if (is_page_template ('public_profile.php')) {   // , public_profile.php( ). }

is_page_template()

, , . . get_page_template()

+1

Wordpress ( , - ) www.example.com/author/[USER NAME], URL- -

www.example.com/author/<?php echo $comment->comment_author;?>

author.php .

+1

Source: https://habr.com/ru/post/1541760/


All Articles