Advertisement
Guest User

AD INSERTER

a guest
Dec 11th, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.81 KB | None | 0 0
  1. /*
  2. * Insert ads code into post content
  3. * $adsCode: Specify code that wants to add
  4. * $insertAfter: Specify paragraph number
  5. */
  6. add_filter('the_content', 'cw_insert_post_ads');
  7. function cw_insert_post_ads($content){
  8. if(is_single()){
  9. //ads code
  10. $adsCode = '<div>Insert your ads code here</div>';
  11.  
  12. //insert after
  13. $insertAfter = 2;
  14.  
  15. $closingP = '</p>';
  16. $contentBlock = explode($closingP, $content);
  17. foreach($contentBlock as $key => $con){
  18. if(trim($con)) {
  19. $contentBlock[$key] .= $closingP;
  20. }
  21. if(($key + 1) == $insertAfter){
  22. $contentBlock[$key] .= $adsCode;
  23. }
  24. }
  25. $content = implode('', $contentBlock);
  26. }
  27. return $content;
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement