Drupal 7 - Inserting A Block Inside A Node Body

To inject a drupal block into the body of a specific node type after a set number of paragraphs, you just need to add a small amount of code to your template.php file.
In example code below, the custom block contains Adsense code (using PHP filter), and the ad is getting inserted after the 3rd paragraph of every node of a specific node type.


1. function YOURTHEME_preprocess_node(&$vars) { 2. $node = $vars['node']; 3. if ($node->type === 'YOURNODETYPE') { 4. $whichBlock = block_load('block',YOURBLOCKNAMENUMBER); 5. $renderTheBlock = drupal_render(_block_get_renderable_array(_block_render_blocks(array($whichBlock)))); 6. $findTheThirdParagraph = "/(?s)((?:<p>.*?<\\/p>\\s*){3})/"; 7. $theNodeBody = $vars['content']['body'][0]['#markup']; 8. $insertThis = "$1 $renderTheBlock\n"; 9. $newNodeBody = preg_replace($findTheThirdParagraph, $insertThis, $theNodeBody, 1); 10. $vars['content']['body'][0]['#markup'] = $newNodeBody; } }
  1. Replace YOURTHEME with your theme name
  2. Here we're loading node variables
  3. Replace YOURNODETYPE with the node type you want to target
  4. Replace YOURBLOCKNAMENUMBER with the number or name of your block
  5. Here we're loading the block into a variable
  6. We want to search for the 3rd paragraph
  7. We are loading the node body into a variable
  8. We are going to display our block after what we've targeted in #6
  9. Now we'll load all three arguments
  10. This is our new node body with the block displayed after the 3rd paragraph

Sticky Side Column

If you're using our PHP Website Template, this is the optional side bar column you can add to some or all of your pages. It can be useful for adding advertisements such as Adsense ads to be displayed on multiple pages.
When you include the side column, your main content will automatically adjust its width to accommodate it.
The side bar is 'sticky'; it will scroll down and remain in view.
You can turn on/off the side bar column per page by defining the $showSide option located at the top of each file.