Prepend content on WordPress sites

  |   By  |   1 min 55 secs |  0 Comments

With the advent of block and patterns in themes, the ability for plugins to prepend some content on posts and pages has got a little more complicated.

The old method of filtering the content, doesn’t look good as a quirk is that filter hook is fired at the top of the page or post. Ouch.

add_filter( ‘the_content’, ‘my_prepend_to_content’ );

So now you will need to detect whether it is a block theme or not and act accordingly…

function my_prepend_setup() {    
  	if ( wp_is_block_theme() ) {         
      	// For block themes: hook into Post Content block rendering        
      	add_filter( 'render_block', 'my_prepend_inside_post_content', 10, 2 );     
    } else {         
      // For classic themes: hook into the_content         
      add_filter( 'the_content', 'my_prepend_to_content' );     
    }
} 
add_action( 'after_setup_theme', 'my_prepend_setup' );  

function my_prepend_inside_post_content($block_content, $block){ 
    // the is_singular('post'), renders the prepend only on a single post.  
    if ( $block['blockName'] === 'core/post-content' && is_singular('post') ) {         
          $prepend = '<div class="custom-prepend">Hello from inside Post Content</div>';         
      return $prepend . $block_content;     
    }     
  return $block_content; 
} 

function my_prepend_to_content($content){ 
  //for non block themes 
  $prepend = '<div class="custom-prepend">Hello from inside Post Content</div>';  
  return $prepend.$content 
  
 }

But even then some older themes don’t play nicely, so the function becomes…

function church_admin_premium_prayer_request_form_prepend_setup() {     
  if ( wp_is_block_theme() ) {         
    // For block themes: hook into Post Content block rendering         
    add_filter( 'render_block', 'my_prepend_inside_post_content', 10, 2 );      
  } else {         
    // For classic themes: hook into the_content         
    $theme = wp_get_theme(); // gets the current theme         
    if ( 'Avada' == $theme->name || 'Avada' == $theme->parent_theme ) 
    	{             add_action('avada_before_main_container', 'my_prepend_to_content');      
     } elseif ( 'The7' == $theme->name || 'The7' == $theme->parent_theme ) {            
      	add_action('presscore_before_loop', 'my_prepend_to_content');         
    }  elseif( 'Omega' == $theme->name || 'Omega' == $theme->parent_theme )  {                          
      	add_action('omega_before_content', 'my_prepend_to_content');         
    }elseif(has_action('fusion_blog_shortcode_before_loop') )         {             
      add_action('fusion_blog_shortcode_before_loop', 'my_prepend_to_content');         
    }        
  else{         
    add_action('loop_start', 'my_prepend_to_content');        
  }     
  } 
}

Let me know of any other themes that have strange content action/filter behaviour.

Hope the code helps

name

ABOUT THE AUTHOR - ANDY MOYLE

Andy Moyle is a church leader and web developer. His biggest project is the Church Admin WordPress plugin and app. He also runs, mainly so he can eat pizza.