We want a plugin informing us on a post we're about to read by the help of various informations.
- Reading time
- Number of words
- Number of characters
- The position of the info block
- The title of the block
We'll have a control on their display.
- Display the plugin in the admin panel
- Create a setting page
- Sanitize all of the setting page inputs
- Render the result on client-side
add_action('admin_menu', array($this, 'adminPage'));
function adminPage()
{
add_options_page(
'Word Count Settings',
'Word Count',
'manage_options',
'word-count-settings-page',
array($this, 'ourHTML')
);
}
function ourHTML() { ?>
<div class='wrap'>
<h1>Word Count Settings</h1>
<form action='options.php' method='POST'>
<?php
settings_fields('wordcountplugin');
do_settings_sections('word-count-settings-page');
submit_button();
?>
</form>
</div>
<?php }
add_settings_section(
'wcp_first_section',
null,
null,
'word-count-settings-page'
);
add_settings_field(
'wcp_location',
'Display Location',
array($this, 'locationHTML'),
'word-count-settings-page',
'wcp_first_section'
);
register_setting(
'wordcountplugin',
'wcp_location',
array(
'sanitize_callback' => array($this, 'sanitizeLocation'),
'default' => '0'
)
);
add_settings_field(
'wcp_healine',
'Headline Text',
array($this, 'headlineHTML'),
'word-count-settings-page',
'wcp_first_section'
);
register_setting(
'wordcountplugin',
'wcp_healine',
array(
'sanitize_callback' => 'sanitize_text_field',
'default' => 'Post Statistics'
)
);
add_settings_field(
'wcp_wordcount',
'Word Count',
array($this, 'checkboxHTML'),
'word-count-settings-page',
'wcp_first_section',
array('theName' => 'wcp_wordcount')
);
register_setting(
'wordcountplugin',
'wcp_wordcount',
array(
'sanitize_callback' => 'sanitize_text_field',
'default' => '1'
)
);
add_settings_field(
'wcp_charcount',
'Character Count',
array($this, 'checkboxHTML'),
'word-count-settings-page',
'wcp_first_section',
array('theName' => 'wcp_charcount')
);
register_setting(
'wordcountplugin',
'wcp_charcount',
array(
'sanitize_callback' => 'sanitize_text_field',
'default' => '1'
)
);
add_settings_field(
'wcp_readtime',
'Read Time',
array($this, 'checkboxHTML'),
'word-count-settings-page',
'wcp_first_section',
array('theName' => 'wcp_readtime')
);
register_setting(
'wordcountplugin',
'wcp_readtime',
array(
'sanitize_callback' => 'sanitize_text_field',
'default' => '1'
)
);
function sanitizeLocation($input){
if($input != '0' AND $input != '1') {
add_settings_error(
'wcp_location',
'wcp_location_error',
'Display location not known.'
);
return get_option('wcp_location');
}
return $input;
}
function checkboxHTML($args)
{ ?>
<input type="checkbox" name="<?php echo $args['theName'] ?>" value="1" <?php checked(get_option($args['theName']), 1) ?>>
<?php }
function headlineHTML()
{ ?>
<input type="text" name="wcp_headline" value="<?php echo esc_attr(get_option('wcp_headline')) // ?>">
<?php }
function locationHTML()
{ ?>
<select name="wcp_location">
<option value="0" <?php selected(get_option('wcp_location'), 0) ?>>Beginning of post</option> <!-- get_option Retrieves an option value based on an option name -->
<option value="1" <?php selected(get_option('wcp_location'), 1) ?>>End of post</option>
</select>
<?php }
function ifWrap($content) {
if (is_main_query() AND is_single() AND (
get_option('wcp_wordcount', '1') OR
get_option('wcp_charcount', '1') OR
get_option('wcp_readtime', '1')
)) {
return $this->createHTML($content);
}
}
function createHTML($content){
$html = '<h3>'. esc_html(get_option('wcp_headline', 'Post Statistics')) . '</h3><p>';
if(get_option('wcp_wordcount', '1') OR get_option('wcp_readtime', '1')) {
$wordCount = str_word_count(strip_tags($content));
}
if(get_option('wcp_wordcount', '1')) {
$html .= 'This post has ' . $wordCount . ' words.<br>';
}
if(get_option('wcp_charcount', '1')) {
$html .= 'This post has ' . strlen(strip_tags($content)) . ' characters.<br>';
}
if(get_option('wcp_readtime', '1')) {
$html .=
'This post will take about ' .
round($wordCount/255) .
' minute' . (round($wordCount/255) > 1 ? 's' : '') .
' to read.<br>';
}
$html .= '</p>';
if(get_option('wcp_location', '0') == '0')
{
return $html . $content;
}
return $content . $html;
}