• Resolved pekos

    (@pekos)


    I want to change the footballpool_matchpredictions_row_template and instead of this :

    $row_template .= '<td class="score">%score%</td></tr>';

    i want to make the $row_template to output

    $row_template .= '<td class="score"><span class="user_score">%score%</span></td></tr>';

    I am trying to following your example but I cannot manage to do it. Can you provide support a simple working plugin example so I can achieve my expected results ?

    Thank you

Viewing 7 replies - 1 through 7 (of 7 total)
  • Plugin Author AntoineH

    (@antoineh)

    May I ask why you would want to do this? I don’t see the added value of inserting a span here. Or do you want to change more?

    Anyway, if you want to change something in the template, you’ll have to completely reconstruct the template in your callback function by copying the code from the plugin and then making your changes before you return the output. Like so:

    $row_template = '<tr class="%current_user_css_class%">
    <td><a href="%user_url%">%user_name%</a></td>
    <td class="home">%home_score%</td>
    <td class="match-hyphen">-</td>
    <td class="away">%away_score%</td>';
    if ( $pool->has_jokers ) {
    $row_template .= '<td title="%joker_title_text%"><div class="nopointer %joker_css_class%"></div></td>';
    }
    $row_template .= '<td class="score"><span class="user_score">%score%</span></td></tr>';

    return $row_template;

    Or, if the changes are small and no other logic is needed, you can sometimes simply do a text replace. Like I do in this example.

    Hope this helps.

    One note: in my next version of the plugin I will have a small update to this row template. So, when you upgrade, please make sure you compare your extension output with my new version (or you may miss some new features).

    Thread Starter pekos

    (@pekos)

    Well it is not only the span with class I need to insert there…

    Even though the string replace function seems OK for other functionalities, in this one I need to introduce other changes in the template which are mainly for styling reasons.

    However, it has now came to my attention that I would like to also change the template html start and introduce <thead> after the <table> and make the th elements part of the <thead> element.
    In this way I can use the “table sorter” plugin which requires that thead is correctly declared in the table structure.

    In order to achieve the above I suppose I don’t need an football pool extension plugin rather I can use a PHP snippet to do so right ? Would you be able to provide the code I a looking for ?

    Thank you

    Plugin Author AntoineH

    (@antoineh)

    Not sure what you mean by PHP snippet, but extending my plugin is done by hooking into filters or actions. My preferred way of doing this is by creating an extension (because those can be easily enabled or disabled, and it’s easier to spot what changes you’ve done). But that is just one method to get it done. You can also include the code in your theme’s functions.

    I can help you by giving a ‘canvas’ that you can work with. Will spend some time during tonight’s games.

    Plugin Author AntoineH

    (@antoineh)

    Might as well do it straight away. This example code will do nothing if you don’t change it. It will simply return the same template. But if you build up your $template vars in the corresponding functions in the way that you would like your html output, then you have what you need.

    Thread Starter pekos

    (@pekos)

    Hi Antoine

    regarding placing the code in an extension (plugin) or in functions.php, we are on the same track. For testing purposes i am using the functions.php method, but the final product will be an extension as you prefer.

    Now regarding the code, this is what I ended up doing, however I am 100% sure that the code can be improved. For example the $pool variable should be global and available to template_start, template_row.

    Also i am not sure if the $match_info is already available or what I am doing is correct i.e. define the same logic [SQL query] as yours to get the $match_info details.

    Nevertheless, see my working code below, and feel free, should you have enough time, to make edits.

    <?php
    /**
    ?* Plugin Name: Football Pool Match Predictions
    ?* Description: Overwrites for the Match Predictions view templates.
    ?* Version: 1.0
    ?* Author: Antoine Hurkmans
    ?* Author URI: mailto:[email protected]
    ?* License: MIT
    ?*/
    /*******************************************************
    ?* ? ?HOW TO USE THIS PLUGIN
    ?*
    ?* 1. Save this file in the /wp-content/plugins folder.
    ?* 2. Activate the plugin via the Plugins screen in the WP admin.
    ?*
    ?*******************************************************/

    add_filter( 'plugins_loaded', ['FootballPoolMatchPredictionsViewTemplate', 'init_extension'] );

    class FootballPoolMatchPredictionsViewTemplate {
    ? ? public static function init_extension() {
    ? ? ? ? // Display a message if the Football Pool plugin is not activated.
    ? ? ? ? if ( ! class_exists( 'Football_Pool' ) ) {
    ? ? ? ? ? ? add_action( 'admin_notices', [__CLASS__, 'no_fp_plugin_error'] );
    ? ? ? ? ? ? return;
    ? ? ? ? }
    ? ? ? ? // The three filters for the individual templates
    ? ? ? ? add_filter( 'footballpool_matchpredictions_template_start', [__CLASS__, 'template_start'], 10, 2 );
    ? ? ? ? add_filter( 'footballpool_matchpredictions_template_end', [__CLASS__, 'template_end'], 10, 2 );
    ? ? ? ? add_filter( 'footballpool_matchpredictions_row_template', [__CLASS__, 'template_row'], 10, 2 );
    ? ? }

    ? ? public static function template_start( $template_start, $match_info ) {
    ? ? ? ? global $wpdb, $pool;
    ? ? ? ? $prefix = FOOTBALLPOOL_DB_PREFIX;
    ? ? ? ? $sql = "SELECT
    ? ? ? ? ? ? ? ? ? ? m.home_team_id, m.away_team_id,
    ? ? ? ? ? ? ? ? ? ? p.home_score, p.away_score, p.has_joker, u.ID AS user_id, u.display_name AS user_name ";
    ? ? ? ? if ( $pool->has_leagues ) $sql .= ", l.id AS league_id ";
    ? ? ? ? $sql .= "FROM {$prefix}matches m
    ? ? ? ? ? ? ? ? LEFT OUTER JOIN {$prefix}predictions p
    ? ? ? ? ? ? ? ? ? ? ON ( p.match_id = m.id AND m.id = %d )
    ? ? ? ? ? ? ? ? RIGHT OUTER JOIN {$wpdb->users} u
    ? ? ? ? ? ? ? ? ? ? ON ( u.ID = p.user_id ) ";
    ? ? ? ? if ( $pool->has_leagues ) {
    ? ? ? ? ? ? $sql .= "INNER JOIN {$prefix}league_users lu ON ( u.ID = lu.user_id )
    ? ? ? ? ? ? ? ? ? ? INNER JOIN {$prefix}leagues l ON ( l.id = lu.league_id ) ";
    ? ? ? ? } else {
    ? ? ? ? ? ? $sql .= "LEFT OUTER JOIN {$prefix}league_users lu ON ( lu.user_id = u.ID ) ";
    ? ? ? ? ? ? $sql .= "WHERE ( lu.league_id <> 0 OR lu.league_id IS NULL ) ";
    ? }

    ? ? ? ? $sql .= "ORDER BY u.display_name ASC";
    ? ? ? ? $sql = $wpdb->prepare( $sql, $match_info['id'] );
    ? ? ? ? $predictions = $wpdb->get_results( $sql, ARRAY_A );
    ? ? ? ? $rows = apply_filters( 'footballpool_statistics_matchpredictions', $predictions, $match_info );
    ? ? ? ? $layout_class = $pool->match_table_layout === 0 ? 'classic-layout' : 'new-layout';
    ? ? ? ? $template_start = sprintf( '<style>
    ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? table.tablesorter thead tr th {border:0;}
    ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? </style>
    ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? <table class="matchinfo match-%d %s statistics tablesorter">
    ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? <thead>
    ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? <tr>
    ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? <th class="custom-username">%s</th>
    ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? <th colspan="%d" class="custom-prediction sortless">%s</th>
    ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? <th class="custom-score">%s</th>
    ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? </tr>
    ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? </thead>'
    ? ? ? ? ? ? , $match_info['id']
    ? ? ? ? ? ? , $layout_class
    ? ? ? ? ? ? , __( 'name', 'football-pool' )
    ? ? ? ? ? ? , ( $pool->has_jokers ? 4 : 3 )
    ? ? ? ? ? ? , __( 'prediction', 'football-pool' )
    ? ? ? ? ? ? , __( 'score', 'football-pool' )
    ? ? ? ? );
    ? ? ? ? return $template_start;
    ? ? }

    ? ? public static function template_end( $template ) {
    ? ? ? ? return '<!-- PEKOS --></tbody></table>';
    ? ? ? ? //return $template;
    ? ? }

    ? ? public static function template_row( $template ) {
    ? ? ? ? $template_row = '<tr class="%current_user_css_class%">
    ? ? ? ? ? ? ? ? ? ? ? ? <td><a href="%user_url%">%user_name%</a></td>
    ? ? ? ? ? ? ? ? ? ? ? ? <td class="home">%home_score%</td>
    ? ? ? ? ? ? ? ? ? ? ? ? <td class="match-hyphen">-</td>
    ? ? ? ? ? ? ? ? ? ? ? ? <td class="away">%away_score%</td>
    ? ? ? ? ? ? ? ? ? ? ? ? <td class="score"><span class="user-points">%score%</span></td></tr>';
    ? ? ? ? //if ($pool->has_jokers) {$row_template .= '<td title="%joker_title_text%"><div class="nopointer %joker_css_class%"></div></td>';}
    ? ? ? ? //$row_template .= '<td class="score"><span class="user-points">%score%</span></td></tr>';
    ? ? ? ? return $template_row;
    ? ? }
    ? ? public static function no_fp_plugin_error() {
    ? ? ? ? $plugin_data = get_plugin_data( __FILE__ );
    ? ? ? ? $plugin_name = $plugin_data['Name'] ?? __CLASS__;
    ? ? ? ? echo "<div class='error'><p>The Football Pool plugin is not activated. ",
    ? ? ? ? ? ? "Make sure you activate it so the extension plugin '{$plugin_name}' has some use.</p></div>";
    ? ? }
    }

    Thank you in advance

    Plugin Author AntoineH

    (@antoineh)

    Didn’t test it, but I think you can indeed simplify the template_start function. The query and data set is not needed. So something like this:

        public static function template_start( $template_start, $match_info ) {
    global $pool;
    $layout_class = $pool->match_table_layout === 0 ? 'classic-layout' : 'new-layout';
    $template_start = sprintf( '<style>
    table.tablesorter thead tr th {border:0;}
    </style>
    <table class="matchinfo match-%d %s statistics tablesorter">
    <thead>
    <tr>
    <th class="custom-username">%s</th>
    <th colspan="%d" class="custom-prediction sortless">%s</th>
    <th class="custom-score">%s</th>
    </tr>
    </thead>'
    , $match_info['id']
    , $layout_class
    , __( 'name', 'football-pool' )
    , ( $pool->has_jokers ? 4 : 3 )
    , __( 'prediction', 'football-pool' )
    , __( 'score', 'football-pool' )
    );
    return $template_start;
    }
    Thread Starter pekos

    (@pekos)

    Hi Antoine

    this works as expected so once again thank you.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Change template’ is closed to new replies.