ChatGPT: A simple plugin that counts the number of words in published WordPress posts
Here is the complete code for a simple WordPress plugin that counts the number of words in published posts and displays the count on a settings page:
<?php
/*
Plugin Name: Word Count Plugin
Plugin URI: http://your-site.com/word-count-plugin
Description: A simple plugin that counts the number of words in published WordPress posts.
Author: Your Name
Version: 1.0
Author URI: http://your-site.com
*/
// Function to count the number of words in a post
function count_words($post_id) {
$post = get_post($post_id);
$content = $post->post_content;
$word_count = str_word_count($content);
return $word_count;
}
// Function to create the plugin's settings page
function plugin_settings_page() {
add_options_page(
'Word Count Plugin Settings',
'Word Count Plugin',
'manage_options',
'word-count-plugin',
'plugin_settings_page_html'
);
}
add_action('admin_menu', 'plugin_settings_page');
// Function to output the HTML for the plugin's settings page
function plugin_settings_page_html() {
?>
<div class="wrap">
<h1>Word Count Plugin Settings</h1>
<form method="post" action="options.php">
<?php
settings_fields('word_count_plugin_settings');
do_settings_sections('word-count-plugin');
submit_button();
?>
</form>
</div>
<?php
}
// Function to register the plugin's settings and fields
function plugin_settings_init() {
register_setting(
'word_count_plugin_settings',
'word_count_plugin_settings'
);
add_settings_section(
'word_count_plugin_section_stats',
'Statistics',
'word_count_plugin_section_stats_cb',
'word-count-plugin'
);
add_settings_field(
'word_count_plugin_field_total_words',
'Total Number of Words',
'word_count_plugin_field_total_words_cb',
'word-count-plugin',
'word_count_plugin_section_stats'
);
add_settings_field(
'word_count_plugin_field_average_words',
'Average Number of Words per Post',
'word_count_plugin_field_average_words_cb',
'word-count-plugin',
'word_count_plugin_section_stats'
);
}
add_action('admin_init', 'plugin_settings_init');
// Callback function for the 'Total Number of Words' field
function word_count_plugin_field_total_words_cb() {
$options = get_option('word_count_plugin_settings');
$total_words = 0;
$posts = get_posts(array('post_status' => 'publish'));
foreach($posts as $post) {
$total_words += count_words($post->ID);
}
echo '<input type="text" name="word_count_plugin_settings[total_words]" value="' . $total_words . '" readonly />';
}
// Callback function for the 'Average Number of Words per Post' field
function word_count_plugin_field_average_words_cb() {
$options = get_option('word_count_plugin_settings');
$total_words = 0;
$post_count = 0;
$posts = get_posts(array('post_status' => 'publish'));
foreach($posts as $post) {
$total_words += count_words($post->ID);
$post_count++;
}
$average_words = $total_words / $post_count;
echo '<input type="text" name="word_count_plugin_settings[average_words]" value="' . $average_words . '" readonly />';
}
// Callback function for the 'Statistics' section
function word_count_plugin_section_stats_cb() {
echo '<p>Statistics for all published posts:</p>';
}
With this code, your plugin will count the number of words in all published posts and display the total number of words and the average number of words per post on the plugin’s settings page.