How to pass a numeric id to a page template?

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.

Leave a Comment