You might find yourself wanting to add an additional custom column to the stats table. Maybe you want to display some extra piece of information about authors in the general stats view (ex. their email, their employment status), or maybe you want to display extra information about posts in an author detailed view. Even though there is no native plugin’s function that will get the job done in just one function call, some light custom coding is enough.
Since stats are displayed in a table, all that we have to do is add a new column to it, and specify how it should be populated per each author/post.
Custom column to general stats view
If you want to append extra information to authors, the relevant WordPress hooks to use are:
- the filter
ppc_general_stats_format_stats_after_cols_default
, to add the extra column to the header/footer of the stats table. It takes the current columns array as only parameter: add the new column and return the updated array; - the filter
ppc_general_stats_each_field_empty_value
, to populate the new column. It takes four parameters:$field_value
– current field value. Change this to the information you would like to display in this new column.$column_name
– the column that is being processed for this filter call. To avoid overwriting all other columns values, make sure to check that$column_name == new_col_name
.$author_stats
– the full stats array for the current author.$author_id
– user ID of the author being processed.
In the following example, we add a custom column to display the user email address. We also show how to make additional checks on user’s permissions and page attributes.
add_filter( 'ppc_general_stats_format_stats_after_cols_default', 'ppc_add_email_col_html', 10 ); add_filter( 'ppc_general_stats_each_field_empty_value', 'ppc_email_td_html', 10, 4 ); /** * Adds custom field column to stats page. * * Hooks to PPC_HTML_functions::get_html_stats() - ppc_general_stats_html_cols_after_default. * * @param $cols array Stats columns */ function ppc_add_email_col_html( $cols ) { global $ppc_global_settings; //Only add column to general stats page if( $ppc_global_settings['current_page'] != 'stats_general' ) return $cols; $perm = new PPC_permissions(); if( $perm->can_mark_as_paid() ) $cols['author_email'] = __( 'Email', 'ppc' ); return $cols; } /** * Populates custom field in stats page. * * Hooks to PPC_HTML_functions::get_html_stats() - ppc_general_stats_html_after_each_default. * * @param $author int author id * @param $formatted_data array formatted stats * @param $raw_data array sorted stats */ function ppc_email_td_html( $field_value, $column_name, $author_stats, $author_id ) { global $ppc_global_settings; //Only add column to general stats page if( $ppc_global_settings['current_page'] != 'stats_general' ) return $field_value; //Only populate the email column if( $column_name != 'author_email' ) return $field_value; $perm = new PPC_permissions(); if( $perm->can_mark_as_paid() OR $perm->can_see_paypal_functions() ) { $user_data = get_userdata( $author_id ); $author_email = $user_data->user_email; return $author_email; } return $field_value; }
Custom column to author stats view
If you want to append extra information to posts, the relevant WordPress hooks to use are:
- the filter
ppc_detailed_stats_format_stats_after_cols_default
, to add the extra column to the header/footer of the stats table. It takes the current columns array as only parameter: add the new column and return the updated array; - the filter
ppc_detailed_stats_each_field_empty_value
, to populate the new column. It takes four parameters:$field_value
– current field value. Change this to the information you would like to display in this new column.$column_name
– the column that is being processed for this filter call. To avoid overwriting all other columns values, make sure to check that$column_name == new_col_name
.$post_stats
– the full stats array for the current post.$post_id
– ID of the post being processed.
In the following example we show how to add an extra column that displays the MD5 hash of the post title. Agreed: it is entirely useless, but we honestly think we have already included all information you might be looking for!
add_filter( 'ppc_general_stats_format_stats_after_cols_default', 'ppc_add_md5_title_col_html', 10 ); add_filter( 'ppc_general_stats_each_field_empty_value', 'ppc_md5_title_html', 10, 4 ); /** * Adds custom field column to stats page. * * Hooks to PPC_HTML_functions::get_html_stats() - ppc_author_stats_html_cols_after_default. * * @param $cols array Stats columns */ function ppc_add_md5_title_col_html( $cols ) { $cols['md5_title'] = __( 'MD5 Title', 'ppc' ); return $cols; } /** * Populates custom field in stats page. * * Hooks to PPC_HTML_functions::get_html_stats() - ppc_author_stats_html_after_each_default. * * @param $author int author id * @param $formatted_data array formatted stats * @param $raw_data array sorted stats */ function ppc_md5_title_td_html( $field_value, $column_name, $post_stats, $author_id ) { global $ppc_global_settings; //Only populate the md5 title column if( $column_name == 'md5_title' ) $field_value = md5($ return $field_value; $author_email = $user_data->user_email; return $author_email; } return $field_value; }