Skip to content

Instantly share code, notes, and snippets.

View Add word count to Twenty Seventeen entry meta section
echo '<div class="entry-meta">';
if ( is_single() ) {
twentyseventeen_posted_on();
} else {
echo twentyseventeen_time_link();
twentyseventeen_edit_link();
};
echo '<div id="word-count">This post contains ' . word_count() . ' words.</div>';
echo '</div><!-- .entry-meta -->';
View Post reading time in WordPress template
echo '<div id="reading-time">This post will take you about ' . reading_time() . ' to read.</div>';
View Delay WordPress posts from being sent to RSS feed
function delay_rss_feed($where) {
global $wpdb;
if ( is_feed() ) {
$now = gmdate('Y-m-d H:i:s');
$wait = '10';
$device = 'MINUTE';
$where.= " AND TIMESTAMPDIFF($device, $wpdb->posts.post_date_gmt, '$now') > $wait ";
}
View Display last updated date instead of publishing date in WordPress
function display_last_updated_date() {
$u_time = get_the_time('U');
$u_modified_time = get_the_modified_time('U');
if ($u_modified_time >= $u_time + 86400) {
$updated_date = get_the_modified_time('F jS, Y');
echo '<div class="last-updated">This post was last updated on ' , $updated_date , '.</div>';
}
}
View Calculate estimated reading time for WordPress post from number of words
function reading_time() {
$content = get_post_field( 'post_content', $post->ID );
$word_count = str_word_count( strip_tags( $content ) );
$readingtime = ceil($word_count / 200);
if ($readingtime == 1) {
$timer = " minute";
} else {
$timer = " minutes";
}
View Post word count in WordPress page template
echo '<div class="word-count">This post contains ' . word_count() . ' words.</div>';
View Count words in WordPress post without HTML tags
function word_count() {
$content = get_post_field( 'post_content', $post->ID );
$word_count = str_word_count( strip_tags( $content ) );
return $word_count;
}
View Obscure WordPress login errors via functions.php
function obscure_login_errors(){
return "That wasn't quite right...";
}
add_filter( 'login_errors', 'obscure_login_errors' );
View Add Google Analytics tracking code via WordPress functions.php
function add_googleanalytics() { ?>
<script async src="https://www.googletagmanager.com/gtag/js?id=XX-XXXXXXXX-X"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'XX-XXXXXXXX-X');
</script>
View customize WordPress toolbar 10
function conditionally_remove_admin_bar() {
if (!current_user_can('administrator') && !is_admin()) {
show_admin_bar(false);
}
}
add_action('after_setup_theme', 'conditionally_remove_admin_bar');