Heute an dieser Stelle mal wieder ein etwas älterer, aber noch immer aktueller Artikel. Ich bin schon seit ewigen Zeiten Nutzer des Plugins Statify und hatte die Idee auf Grundlage dieses Plugins die Seite „Beiträge“ im WordPress Backend um eine Spalte „Anzahl Seitenaufrufe“ zu erweitern. Herausgekommen ist nachfolgendes Snippet.
<?php
/**
* Plugin Name: Postviews for Statify
* Plugin URI: https://horst-scheuer.net/
* Description: Zeigt mit Hilfe des Plugin "Statify" die Anzahl Seitenaufrufe unter "Beiträge" an.
* Version: 1.4
* Author: Horst Scheuer
* Author URI: https://horst-scheuer.net/
* License: GPL v2 or later
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
*/
if ( ! class_exists( 'WP' ) ) {
die();
}
function add_css_to_wp_dashboard() {
?>
<style>
#dashboard-widgets td a {
text-decoration: none;
}
.fixed .column-postviews {
width: 3.5em;
padding: 8px 8px;
text-align: right;
}
th .postviews-bubble:before {
content: "\f185";
font: normal 20px/.5 dashicons;
speak: never;
display: inline-block;
padding: 0;
top: 4px;
left: 4px;
position: relative;
vertical-align: top;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-decoration: none!important;
color: #3c434a;
}
@media screen and (max-width: 782px) {
.fixed .column-postviews {
text-align: left;
}
}
</style>
<?php
}
add_action( 'admin_head', 'add_css_to_wp_dashboard' );
function get_views_of_post_from_statify( $link ) {
global $wpdb;
$results = $wpdb->get_results(
$wpdb->prepare(
"SELECT COUNT(`target`) as `count` FROM `$wpdb->statify` WHERE `target` = %s", $link
),
OBJECT
);
return $results[0]->count;
}
if( in_array( 'statify/statify.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) {
function add_views_column( $columns ) {
$new_columns = array( 'postviews' => __( '<span class="postviews-bubble" title="Aufrufe"> <span class="screen-reader-text">Aufrufe</span></span>' ), );
return array_merge( $columns, $new_columns );
}
add_filter( 'manage_posts_columns', 'add_views_column', 5 );
add_filter( 'manage_pages_columns', 'add_views_column', 5 );
function display_views_column( $column_name ) {
switch( $column_name ) {
case 'postviews':
echo get_views_of_post_from_statify( str_replace( home_url(), '', get_permalink() ) );
}
}
add_action( 'manage_posts_custom_column', 'display_views_column', 5, 2 );
add_action( 'manage_pages_custom_column', 'display_views_column', 5, 2 );
}
/**
* This is the end - Beautiful friend - This is the end ...
* Jim Morrison
*/
Die Funktion ermittelt die Anzahl Seitenaufrufe aus der Datenbanktabelle wp_statify, die vom Plugin Statify angelegt wird und erzeugt eine neue Spalte auf der Seite „Beiträge“.