Thank You Counter Button WordPress Plugin Filter Hooks

Content Filter

Content Filter


There are next filter hooks available for the Thank You Counter Button (TYCB) WordPress plugin:

  • Side bar Thanks Stat widget has filter hook 'thanks_stat_sidebar' for its content.
  • Admin dashboard ‘Latest Thanks’ widget has filter hook 'thanks_stat_dashboard' for its content.
  • Admin dashboard ‘Latest Thanks’ widget has filter hook 'thanks_stat_dashboard_row' which allows to change content of every row at this widget;
  • Thank You button has filter hook 'thanks_thankyou_button' for its html code now.
  • Sidebar ‘Latest Thanks’ widget has filter hook 'thanks_stat_sidebar_item' which allows to change content of every widget row;
  • Sidebar ‘Latest Thanks’ widget has filter hook 'thanks_stat_sidebar_total_quant' which allows to change content of total quant counter placed at the sidebar widget;


You can use those hooks in your plugins or just in function.php of your WP theme. Look on this code sample for your reference:

function thankStatModify($output) {
// $output is the HTML source code of widget or button to modify. 
// I added text to it here, from the begin and to the end just for example.
 
  return 'before '.$output.' after';
 
}
 
add_filter('thanks_stat_sidebar', thankStatModify);
// and / or
add_filter('thanks_stat_dashboard', thankStatModify);
// and / or
add_filter('thanks_thankyou_button', thankStatModify);

More Advanced Example 1

Let’s make something more useful together. For example add the last thanks post date and time information at the top of the ‘Latest Thanks’ TYCB dashboard widget. We will use two filter hooks for it:
1) thanks_stat_dashboard – in order to add information to the top of the widget;
2) thanks_stat_dashboard_row – in order to calculate the last thanks post date and time without use of any row of MySQL code.
First define the variable $flag_last_time where we will store last thanks date and time:

// tycb dashboard widget mod
// Variable to store last thanks date 
$flag_last_time = '';

Next, let’s define function to call with thanks_stat_dashboard filter in order to add new information to the top of TYCB dashboard widget. Date and time information is assigned to the $flag_last_time variable already:

function thankStatModifyDashboardHeader($output) { 
    global $flag_last_time;
    $prefix = ''; 
    // Check the data 
    if ($flag_last_time != 'NA') {
        // Add the last thanks date at the beginning of the TYCB dashboard widget 
        $prefix = '<p class="ws-plugin-update">'.__('Last Thank Date', 'thankyou').' : '.
mysql2date((get_option('date_format').', '.get_option('time_format')), $flag_last_time, true).'</p>';
    } 
  return $prefix.$output;
}

What we made?
1) declare $flag_last_time variable as global. It is needed to have access to its value changed in some other place. We will see where a little later.
2) If $flag_last_time has date and time information (that is not equal to ‘NA’) prepare its value to show at the top of TYCB dashboard widget, place especially formatted value to the $prefix variable. We wrap it to the <p> tag and use mysql2date() WordPress core function in order to have human readable date and time value according to the blog locale settings.
mysql2date() function is defined in \wp-includes\functions.php
As mentioned above its purpose is to converts MySQL DATETIME field to the user specified date format.

Third, we define function which will be called for every data row of the TYCB dashboard widget withl usage of the thanks_stat_dashboard_row filter hook for that.

function thankStatModifyDashboardRow($data) {
  global $flag_last_time;
  // take date and time from the first row
  if ($flag_last_time=='' && $data['updated'] != '') {
    // Check the kind 
    if ($data['kind'] == 'latest_thanked') { 
      // Store the last thanks date 
      $flag_last_time = $data['updated'];
    } else { 
      $flag_last_time = 'NA';
    } 
  } 
     return $data; 
}

As the data rows in the ‘Latest thanks’ widget is sorted in the descending order already it is enought to take date and time from the first row only. For the first row variable $flag_last_time has empty value yet. We check it with

  if ($flag_last_time=='' && $data['updated'] != '') {

Then we check if the widget has ‘Latest Thanked’ kind

if ($data['kind'] == 'latest_thanked') {

and catch the date and time needed

$flag_last_time = $data['updated'];

If we have TYCB widget of another kind (‘Most Thanked’) then we assign ‘NA’ (not available) value to the flag_last_time variable.

Finally we need to add our functions to the TYCB widget filter hooks.

// Adding hooks to TYCB dashboard 
add_filter('thanks_stat_dashboard', thankStatModifyDashboardHeader); 
add_filter('thanks_stat_dashboard_row', thankStatModifyDashboardRow);

That’s all.

What do you need to have this feature at your blog TYCB dashboard widget? Just copy/paste the code to your blog functions.php file. You can download this code with one click from this link.

Tags: ,