I am trying to add an inline style to my paragraph tags which are ouputted using the_content();
I’ve tried string replace, see question I did earlier. But it won’t because the_content echo’s it and does not return it. If I return the content using get_the_content();
, it does not output in paragraph tags.
Can anyone help me with this?
Thanks to @papirtiger
Came up with this solution just to apply it to a specific content function.
I did not explain in my question that I only needed to work on a specific the_content, instead I think the above solutions are a global solutions, and both are great solutions from that point of view.
You can use your custom string replace in a custom “the_content” function.
Use filter and actions:
Update
If you want to use a filter just for a specialjob instead of a global filtering, add the filter just in the place where you need it and than remove it again.
First define your filter-callback:
function insert_inline_style( $content = null ){ ... }
Place this function anywhere you want. Rule of thumb: if you want to reuse the callback, place it in an central file like functions.php. If the callback is just for a (very) special job, place it in the same file which do the job.
Now we have to add the filter, so the output of
the_content
will be filtered:So you don’t have to fiddle around with
get_the_content()
and can easily reuse the callback if needed.I might be late for this one, but use this :
Then call $content wherever you want.
Basically, the_content is kept in memory (another time dimension) and no data is fetch until
ob_get_clean()
is called. This said,the_content()
keeps the format asget_the_content()
does not. This is why many answers would try to filterget_the_content()
, which is quite troublesome.