下面是一个简单的WordPress插件,它可以在主页底部显示今日码字数、网站所有文章总字数、网站运行时间和今天的年月日。插件使用WordPress内置的计数器和时间函数,因此它是轻量级的,不需要连接到外部服务或API。你只需要把这个插件的代码保存为一个PHP文件,然后将它上传到WordPress插件目录中即可。
<?php /** * Plugin Name: Custom Stats * Plugin URI: http://example.com/ * Description: 这个插件用于在网站主页底部显示统计信息。 * Version: 1.0 * Author: Your Name * Author URI: http://example.com/ */ function custom_stats() { // Count the words written today $today = date('Y-m-d'); $args = array( 'post_type' => 'post', 'post_status' => 'publish', 'date_query' => array( array( 'after' => $today, ), ), ); $today_query = new WP_Query($args); $word_count_today = 0; while ($today_query->have_posts()) { $today_query->the_post(); $word_count_today += str_word_count(get_the_content()); } wp_reset_postdata(); // Count the total words on the site $args = array( 'post_type' => 'post', 'post_status' => 'publish', ); $all_query = new WP_Query($args); $word_count_all = 0; while ($all_query->have_posts()) { $all_query->the_post(); $word_count_all += str_word_count(get_the_content()); } wp_reset_postdata(); // Calculate the site's uptime $site_start_date = get_option('site_start_date'); $site_uptime = human_time_diff(strtotime($site_start_date), current_time('timestamp')); // Display the stats echo '<div style="text-align: center; font-size: 16px;">'; echo '<p>今日字数:' . $word_count_today . '字</p>'; echo '<p>总字数:' . $word_count_all . '字</p>'; echo '<p>运行时间:' . $site_uptime . '</p>'; echo '<p>今天是:' . date('Y年m月d日') . '</p>'; echo '</div>'; } function add_custom_stats() { add_action('wp_footer', 'custom_stats'); } add_action('init', 'add_custom_stats'); function site_start_date_activation() { if (!get_option('site_start_date')) { add_option('site_start_date', current_time('mysql')); } } register_activation_hook(__FILE__, 'site_start_date_activation');
这段代码是一个基本的WordPress插件,用于在你的网站主页底部显示统计信息。要使用这个代码,你可以按照以下步骤进行操作:
打开一个文本编辑器,如记事本或Sublime Text。
将上述代码复制并粘贴到文本编辑器中。
将文件保存为“custom-stats.php”。
将“custom-stats.php”文件上传到你的WordPress网站的wp-content/plugins目录中。
登录WordPress后台,在左侧菜单中选择“插件”,找到“Custom Stats”插件并激活它。
刷新你的网站主页,你将会在底部看到显示今日码字数、网站所有文章总字数、网站运行时间和今天的年月日的统计信息。
注意:在上传插件文件之前,请确保你有足够的权限,并且备份你的网站。如果你不确定如何进行这些操作,请咨询你的WordPress主机或管理员。
RNA