If you are using WordPress to manage a blog or website, you might have sometime wanted to insert a content block or advertisement programatically. In this WordPress tutorial, I am sharing the right way to insert content or ad automatically in WordPress.

Disclosure:We receive compensation from companies whose products and services we feature. All links, coupons & recommendations on this website should be treated as paid advertisements.

Wrong Way of Inserting Content in WP

As a novice, I relied on the WordPress content filter to add the custom content block before or after the content:

add_filter( 'the_content', 'my_custom_function');
function my_custom_function( $content ) {
return 'Custom Content HTML Block Before Content' . $content . 'Custom Content HTML Block After Content';
}

This method works fine when you use it within the file specific to the post or page content display. It might start repeating the content in non-desired locations when used in the functions.php file of your theme or in any plugin.

This function is not truly fit as it will alter all the posts and pages, including any post appearing in widgets, or custom blocks on individual post/pages. The content filter gets applies every time ‘the_content’ function is used, irrespective of the post type, or if the target post is part of main query (the actual page content) or not.

Right Method for Inserting Content in WP

If you want to insert content automatically in WordPress with better control and proper functionality, you need to skip the filter and hook into the WordPress query, check if it is the main query (for the actual content of the page/post) and not a secondary content such as that showing in widgets.

For inserting content or ads in WordPress automatically, without the side-effect of having the same content repeated in post content other than the main query. This way your custom content is inserted only within the content of the page being visited,  and not on other posts or pages that might be also displayed in lists, widgets, etc.

For more control in inserting content automatically in WordPress, you should use loop_start / loop_end actions to add and remove the content filter. This ensures that the content filter is applied only to the desired content and not elsewhere.

// Modify the post content
function speckygeek_modify_content( $content ) {

global $post;
// Only edit specific post types
$types = array( 'post', 'page' );

if ( $post && in_array( $post->post_type, $types, true ) ) {
$content = 'Custom Content HTML Block Before Content' . $content . 'Custom Content HTML Block After Content';
}

return $content;
}

// Add the filter when the main loop starts
add_action( 'loop_start', function( WP_Query $query ) {
if ( $query->is_main_query() ) {
add_filter( 'the_content', 'speckygeek_modify_content', -10 );
}
} );

// Remove the filter when main loop ends
// Ensures that the modification not applied at any undesired location
add_action( 'loop_end', function( WP_Query $query ) {
if ( has_filter( 'the_content', 'speckygeek_modify_content' ) ) {
remove_filter( 'the_content', 'speckygeek_modify_content' );
}
} );

 

What we did above is that instead of using the content filter directly, which would have modified the content on every content instance being shown on the page, we restricted the scope of the file to the main query by using the loop_start action. Once our content was modified, we removed the action to limit the modification to the content of the page being displayed.

That’s all that you need to properly insert content automatically in WordPress.