Adding a div to the_content Wordpress

Hi all,

As the title says I am trying to add a div to the_content on a Wordpress site.

The reason I am trying to do this is because there is a plugin on the site that adds content to the header of a post instead of the content and another plugin that adds social media icons to the top of the content, so the social media icons look like they are in the center of the text.

I need to add the div in the header to the content.

I have something like this (added to the bottom of the functions.php file)

function custom_fields($content){
    return preg_replace('<div class="offer_intro"><?= get_post_meta($post->ID, "offer_intro", true); ?></div>', $content);
add_filter('the_content', 'custom_fields');

But I don’t know enough about adding this sort of thing or PHP, so I need to get this written properly.

As far as I know this will add the div (offer_intro) to the content.


i really can’t understand what you want to do !? if you use this Function so you can see a Custom field just under Your Editor… so what i suggest :

1- Use this Plugin : Advanced Custom Fields

2- Check and edit the CSS Code of this class : offer_intro


@allanlud - preg_replace needs 3 parameters to function

Your code

    return preg_replace('<div class="offer_intro"><?= get_post_meta($post->ID, "offer_intro", true); ?></div>', $content);

is missing the first parameter what you want to replace.

For example in your content if you have a content replacethistextwithothertext. Then you can say

    return preg_replace('replacethistextwithothertext','<div class="offer_intro"><?php get_post_meta($post->ID, "offer_intro", true); ?></div>', $content);

If you want to simply add text before your content output begins other option is this

function custom_fields($content){
global $post;
$offerintro = get_post_meta($post->ID, "offer_intro", true);
$outputdata = '<div class="offer_intro">' .  $offerintro  . '</div>';
return $outputdata . $content;
add_filter('the_content', 'custom_fields');

Hence before the content is output the offer_intro is displayed.

Thanks for the replies guys, sorry if I didn’t explain myself clearly, I thought it would be a simple enough problem to solve.

@deadmix the pluign that is adding text to the title is Advanced Custom Fields (in the single.php file it was placed in the header / title section). So I needed to push it down into the_content. Moving the line of code below the_content in the single.php file only pushed that paragraph of text to the bottom of the_content and I needed it in the_content at the start (just after the Social Media icons).

I just found some code this morning that seems to work:

function add_postdata_to_content($text) {
global $post;
  $postdata .= '<div class="offer_intro">' . get_post_meta( $post->ID, 'offer_intro', true ) . '</div> ';
return $postdata . $text;

@ jaagare I was wondering why the code was like this - <?=

@allanlud. Yes I had simply copy/pasted the code you had, so found that error and fixed it in my code.

Yes the code you have posted now and that works is exactly similar to what I have mentioned so I think it should work without issues.

That’s “short cut” for
<?php echo

I don’t use it because it requires php.ini to be configured to use it and a few extra characters here and there doesn’t bother me any. But I have seen it used by others often.

Thanks for explaining that Mittineague