WordPress 站点统计的小工具版本

[info]很多 WordPress 的博主都喜欢在首页侧边栏摆放 “站点统计” 这样的功能。但是这些几乎都是在 sidebar.php 文件里,一些新用户或是对PHP代码不熟悉的用户,在设置博客统计的建站日期之类的不会操作甚至是遇到麻烦或者是有人想要更简便的操作,在小工具里统一管理,设置时间等,于是我研制了 WordPress 站点统计的小工具版本。[/info]

WordPress 站点统计 [小工具版]

[warn]动手操作前,请做好相关文件的备份,以防万一。[/warn]
以下就是这个小工具的 PHP代码,你只要用文本编辑器(一定不要是 Windows 的记事本,可以用 Notepad++ 软件编辑),把以下代码拷贝进去,保存为 widget_siteinfo.php。

<?php
add_action(
‘widgets_init’, create_function(‘return register_widget(“siteinfo”);’));
class siteinfo extends WP_Widget {
function siteinfo() {
global $prename;
$this
>WP_Widget(‘siteinfo’, $prename.‘[iEdonX] 站点信息’, array( ‘description’ => ‘显示您站点的统计信息。注意:建站年月日必须为有效的数字,错误的输入将显示 1970年1月1日。’ ));
}
function widget($args, $instance) {
extract($args, EXTR_SKIP);
echo $before_widget;
$title = apply_filters(‘widget_name’, $instance[‘title’]);
$createyear = $instance[‘createyear’];
$createmonth = $instance[‘createmonth’];
$createday = $instance[‘createday’];
echo $before_title.$title.$after_title;
echo ‘<ul>’;
if ($createyear== || (int)$createyear <= 0){
$createyear = 
‘1970’;
}
if ($createmonth==
 || (int)$createmonth > 12 || (int)$createmonth <= 0 ){
$createmonth = 
‘1’;
}
if ($createday==
 || (int)$createday > 31 || (int)$createday <= 0 ){
$createday = 
‘1’;
}
show_siteinfo($createyear,$createmonth,$createday);
echo 
‘</ul>’;
echo $after_widget;
}
function update($new_instance, $old_instance) {
$instance = $old_instance;
$instance[‘title’] = strip_tags($new_instance[‘title’]);
$instance[‘createyear’] = strip_tags($new_instance[‘createyear’]);
$instance[‘createmonth’] = strip_tags($new_instance[‘createmonth’]);
$instance[‘createday’] = strip_tags($new_instance[‘createday’]);
return $instance;
}
function form($instance) {
$instance = wp_parse_args( (array) $instance, array(
‘title’ => ‘站点信息’,
‘createyear’ => ‘1970’,
‘createmonth’ => ‘1’,
‘createday’ => ‘1’)
);
?>
<p><label>标题:<input class=“widefat” id=“<?php echo $this->get_field_id(‘title’); ?>” name=“<?php echo $this->get_field_name(‘title’); ?>” type=“text” value=“<?php echo $instance[‘title’]; ?>” /></label></p>
<p><label>建站年份:<input class=“widefat” id=“<?php echo $this->get_field_id(‘createyear’); ?>” name=“<?php echo $this->get_field_name(‘createyear’); ?>” type=“number” value=“<?php echo $instance[‘createyear’]; ?>” /></label></p>
<p><label>建站月份:<input class=“widefat” id=“<?php echo $this->get_field_id(‘createmonth’); ?>” name=“<?php echo $this->get_field_name(‘createmonth’); ?>” type=“number” value=“<?php echo $instance[‘createmonth’]; ?>” /></label></p>
<p><label>建站日期:<input class=“widefat” id=“<?php echo $this->get_field_id(‘createday’); ?>” name=“<?php echo $this->get_field_name(‘createday’); ?>” type=“number” value=“<?php echo $instance[‘createday’]; ?>” /></label></p>
<?php $title = strip_tags($instance[‘title’]);
$createyear = strip_tags($instance[
‘createyear’]);
$createmonth = strip_tags($instance[
‘createmonth’]);
$createday = strip_tags($instance[
‘createday’]);
}
}
function show_siteinfo($year,$month,$day){
global $wpdb;
?
>
<li><a>文章总数:<?php $count_posts = wp_count_posts(); echo $published_posts = $count_posts->publish;?> 篇</a></li>
<li><a>标签总数:<?php echo $count_tags = wp_count_terms(‘post_tag’); ?> 个</a></li>
<li><a>评论总数:<?php $count_comments = get_comment_count();echo $count_comments[‘approved’];?> 条</a></li>
<li><a>建站日期:<?php $text = $year.‘年’.$month.‘月’.$day.‘日’;echo $text; ?></a></li>
<li><a>最后更新:<?php $last = $wpdb->get_results(“SELECT MAX(post_modified) AS MAX_m FROM $wpdb->posts WHERE (post_type = ‘post’ OR post_type = ‘page’) AND (post_status = ‘publish’ OR post_status = ‘private’)”);$last = date(‘Y年n月j日’, strtotime($last[0]->MAX_m));echo $last; ?></a></li>
<?php $str = $year.‘-‘.$month.‘-‘.$day;
$d = new DateTime( $str );
$time = $d-
>format(‘Y-m-d’); ?>
<li><a>运行天数:<?php echo floor((time()-strtotime($time))/86400); ?> 天</a></li>
<? }; ?>

启用方案一:(不保险)

找到主题目录下类似 widget 字样的文件夹(可能因主题不同而不同),再找找目录下widget.php,打开。找到类似以下代码:

include(‘widget-xxx.php’);

在下面加入:

include(‘widget-siteinfo.php’);

保存,并把 widget_siteinfo.php 保存到和这个文件相同的目录。

 启用方案二:

在主题 functions.php 中 <?php …….. ?> 之前插入

include(‘widget-siteinfo.php’);

保存即可。

使用案例:

登录并打开 WordPress 后台,依次点击:“外观” — “小工具”,把 “[iEdonX] 站点信息” 拖入右方,并输入正确的建站年月日(错误的输入将显示 1970年1月1日)

siteinfo_widget

 

祝你使用愉快,效果可见本博首页。


4 responses to “WordPress 站点统计的小工具版本”

  1. wordpress很早就更新了,尽量不要新建class。
    参见我之前更新的:http://plugins.trac.wordpress.org/browser/leniy-tweaks/trunk/leniy_blogstat/main.php

    1. 额。。习惯用类了。。

Leave a Reply to iEdon Cancel reply

Your email address will not be published. Required fields are marked *