One of the powerful features of WordPress is its hooks and filters, allowing you to modify the behavior and appearance of your site without changing the core code.
In this article, we’ll demonstrate how to use MonaWP filters to replace the read time element in your theme with custom code or even remove it entirely.
Install a Custom Code Inserter Plugin
To add custom PHP code easily, install a plugin like Insert Headers and Footers. This plugin allows you to insert code snippets without modifying your theme files directly.
Add Custom PHP Code
Once the plugin is installed, navigate to Code Snippets -> Add New -> Add Your Custom Code. Make sure to set the language to PHP, then paste the following code snippet.
Remove read time everywhere:
// Define filter function to replace the read time with nothing
function replace_read_time_with_nothing($html) {
return ''; // Return an empty string to replace the read time HTML
}
add_filter('monawp_entry_read_time_html_filter', 'replace_read_time_with_nothing', 10, 1);
Code Explanation: Removing the Read Time Element
The provided code snippet demonstrates how to use WordPress hooks and filters to modify the read time element of your posts.
The first part defines a filter function, replace_read_time_with_nothing
, which takes the original HTML content as input and returns an empty string, effectively removing the read time element.
The second part hooks this filter function into the monawp_entry_read_time_html_filter
filter, ensuring that whenever this filter is applied within the theme or plugin, the read time element will be removed.
The filter is given a priority of 10
, which is the default, and expects one parameter, the HTML string.
This allows developers to easily customize the output of the read time element without directly altering the theme’s core files.
More Snippets:
Remove Read Time on Single Posts:
// Define filter function to replace the read time with nothing on single posts
function replace_read_time_on_single_posts($html) {
if (is_single()) {
return ''; // Return an empty string to replace the read time HTML
}
return $html; // Return the original HTML on other pages
}
add_filter('monawp_entry_read_time_html_filter', 'replace_read_time_on_single_posts', 10, 1);
Show Read Time Only On Single Posts:
// Define filter function to replace the read time with nothing on single posts
function replace_read_time_on_single_posts($html) {
if (!is_single()) {
return ''; // Return an empty string to replace the read time HTML
}
return $html; // Return the original HTML on other pages
}
add_filter('monawp_entry_read_time_html_filter', 'replace_read_time_on_single_posts', 10, 1);
Remove read time from blog page:
function remove_read_time_from_blog($html) {
if (is_home()) {
return ''; // Return an empty string to replace the read time HTML
}
return $html; // Return the original HTML on other pages
}
add_filter('monawp_entry_read_time_html_filter', 'remove_read_time_from_blog', 10, 1);
Show read time only on blog page:
function show_read_time_on_blog($html) {
if (!is_home()) {
return ''; // Return an empty string to replace the read time HTML
}
return $html; // Return the original HTML on other pages
}
add_filter('monawp_entry_read_time_html_filter', 'show_read_time_on_blog', 10, 1);
Remove author:
everywhere:
// Define filter function to replace the read time with nothing
function replace_author_with_nothing($html) {
return ''; // Return an empty string to replace the read time HTML
}
add_filter('monawp_entry_author_html_filter', 'replace_author_with_nothing', 10, 1);
in blog page:
// Define filter function to replace the author tag with nothing except on the blog page
function replace_author_with_nothing_except_on_blog($html) {
if (is_home()) { // If not on the blog page
return ''; // Return an empty string to replace the author tag
}
return $html; // Return the original HTML if on the blog page
}
add_filter('monawp_entry_author_html_filter', 'replace_author_with_nothing_except_on_blog', 10, 1);
everywhere but blog page:
// Define filter function to replace the author tag with nothing except on the blog page
function replace_author_with_nothing_except_on_blog($html) {
if (!is_home()) { // If not on the blog page
return ''; // Return an empty string to replace the author tag
}
return $html; // Return the original HTML if on the blog page
}
add_filter('monawp_entry_author_html_filter', 'replace_author_with_nothing_except_on_blog', 10, 1);
Replace with custom content:
// Define filter function to replace the read time with nothing
function replace_author_with_nothing($html) {
return 'custom content'; // Return an empty string to replace the read time HTML
}
add_filter('monawp_entry_author_html_filter', 'replace_author_with_nothing', 10, 1);