1

I am using a Wordpress installation with Wordpress Social Login as a "canvas" for an HTML5-based board game.

In the file /wp-content/themes/twentythirteen-child/front-page.php I get the numeric ID of the viewing user (returned by wp_get_current_user() method) and then her or his Facebook data (id, first name, last name, gender) from the $wpdb->prefix . 'wslusersprofiles' table - and can display the game and score for that user at the front page of my website.

My question please: underneath the game board I would like to show a list of other players (maybe a "top 10" or maybe a list of all players with the help of DataTables jQuery plugin).

I would like to have a link like /player/12345 or /player-12345 for each player in the list - so that it is possible to click on it and then display the first name and score for that player.

I keep reading Theme Handbook and looking at Wordpress Template Hierarchy and just don't understand - what should be the path of my user profile file (something like /wp-content/themes/twentythirteen-child/page-templates/player.php?) and especially how to pass the numeric id part (the 12345 in the above example) to that file?

UPDATE:

I have added functions.php and page-templates/page-player.php as suggested by Jevuska in his very detailed answer.

Unfortunately, I get 404 error, when trying to open /player-1 (that would be me as admin) or /player-12345 - even though I flush rewrite rules unconditionally in wp_loaded hook.

I am using PHP 5.3 on CentOS 6.7 Linux

  • 1
    I test your child theme and thats work. I use PHP 7. Have you create new page with slug player from admin? – Jevuska Apr 6 '16 at 9:21
  • 1
    It just for additional information, if you use Child Theme, WP say: Note that the previous method was to import the parent theme stylesheet using @import: this is no longer best practice. – Jevuska Apr 6 '16 at 9:46
2

In regard to WordPress post paged, I would use /player-12345 or other structure as url specific page rather than /player/12345.

How to create new url structure for page

Creating additional url structure, you need WP_Rewrite, there are a couple of filters that you can fire with your function. For Page, you can filter page_rewrite_rules. See example code on documentation https://codex.wordpress.org/Class_Reference/WP_Rewrite#Examples. Pay attention for notes in those documentation.

This is sample code related with your url structure /player-12345. Make sure your page slug is player. Add this code in theme file functions.php or inside your plugin.

add_filter( 'page_rewrite_rules','my_insert_rewrite_rules' );
add_filter( 'query_vars','my_insert_query_vars' );
add_action( 'wp_loaded','my_flush_rules' );

// flush_rules() if our rules are not yet included
function my_flush_rules()
{
    $rules = get_option( 'rewrite_rules' );

    if ( ! isset( $rules['(player)-(\d*)$'] ) )
    {
        global $wp_rewrite;
        $wp_rewrite->flush_rules();
    }
}

// Adding a new rule page slug with number
function my_insert_rewrite_rules( $rules )
{
    $newrules = array();
    $newrules['(player)-(\d*)$'] = 'index.php?pagename=$matches[1]&player_id=$matches[2]';

    return $newrules + $rules; //combine all page rules
}

// Adding the player_id var so that WP recognizes it
function my_insert_query_vars( $vars )
{
    array_push( $vars, 'player_id' );
    return $vars;
}

Now you have url structure page with user id /player-12345, create page link in your front-page base on your database. You can use get_page_link and combine with your list user id.

Create page or custom page

Then how to display our user data in front-end ?

Create new page ( Player with slug player from admin ), it will use default page.php template, you can use just it. But if you need in different layout, creating new file theme page-{slug}.php ( without template name ) is my prefered method, in your case page-player.php.

Create duplicate code from the page.php, and customize as necessary. You don't have to create custom wp_query with loop inside. Just use normal loop wp_query as is.

Since we have additional query parameter for page is player_id with user ID value, with get_query_var( 'player_id' ) it make easy to retrieve user data.

This is simple code to display user data, I just use user query, you can implement on your way with data from your new table. Filtering the_content with function to retrieve user data and keep the original page content ( you can overwrite too ). Add in functions.php.

add_filter( 'the_content', 'my_the_content_filter' );
/**
 * Filter page content
 * by filter the_content
 * Use condition statement is_page( slug ) with get_query_var( 'player_id' )
 * 
 * @param  string $content Oringinal text
 * @return string New Content with or without original text
 */
function my_the_content_filter( $content )
{
    if ( is_page( 'player' ) && ! empty( get_query_var( 'player_id' ) ) )
    {
        $player_id = intval( get_query_var( 'player_id' ) );

        /**
         * Add user info only ( overwrite page content )
           $content = _my_function_player_info( $player_id );
         */

        /**
         * Include your page content and additional content of user info
         */
        $content .= _my_function_player_info( $player_id );
    }
    return $content;
}

/**
 * Set your user data, html, table, etc
 * @see param https://developer.wordpress.org/reference/functions/get_user_by/
 * @param  integer $player_id User ID
 * @return string User data front-end
 */
function _my_function_player_info( $player_id )
{
    ob_start();
    $user = get_user_by( 'id', intval( $player_id ) );
    ?>
    <table>
        <tr>
            <td><?php _e( 'Name', 'text_domain' ); ?></td>
            <td><?php echo $user->first_name . ' ' . $user->last_name; ?></td>
        </tr>
    </table>
    <?php
    $html = ob_get_clean();
    return $html;
}

Another filter such as loop_start or loop_end to display user data before or after content loop, and use echo in output, or implement your function direct into custom page. Just abuse this code as your need.

  • 1
    You in child, twentythirteen don't have page-templates, just add page-player.php the same path with functions.php. – Jevuska Apr 6 '16 at 8:55
  • 1
    I have moved page-player.php into /wp-content/themes/twentythirteen-child dir and unfortunately still get 404 error, even after restarting Apache. Do I need some special PHP-comment in my new page template file? – Alexander Farber Apr 6 '16 at 8:59
  • 1
    Have you create new page with slug player from admin? – Jevuska Apr 6 '16 at 9:29
  • How to do that please? As admin I go to /wp-admin/post-new.php?post_type=page and then? How to make it "player" page? – Alexander Farber Apr 6 '16 at 10:13
  • 1
    Create title "Player" and you will see WP create page permalink with slug 'player' automatically, then publish. – Jevuska Apr 6 '16 at 10:19

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.