3

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?

3
  • The answers below should do what you are asking but if you theme uses post_class and body_class the way it should you should rarely, if ever, need to do this.
    – s_ha_dum
    Nov 15, 2012 at 14:51
  • I'm trying to create a mailer template, so I need inline styles just on one the_content function. See what mean here... gist.github.com/0e85ea3b81eddf48376c
    – Joshc
    Nov 15, 2012 at 15:15
  • That makes sense. You found a rare case.
    – s_ha_dum
    Nov 15, 2012 at 15:33

4 Answers 4

4

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.

<?php 

    $phrase = get_the_content();
    // This is where wordpress filters the content text and adds paragraphs
    $phrase = apply_filters('the_content', $phrase);
    $replace = '<p style="text-align: left; font-family: Georgia, Times, serif; font-size: 14px; line-height: 22px; color: #1b3d52; font-weight: normal; margin: 15px 0px; font-style: italic;">';

    echo str_replace('<p>', $replace, $phrase);

?>
2

You can use your custom string replace in a custom "the_content" function.

function custom_the_content($more_link_text = null, $stripteaser = false) {

    $content = get_the_content($more_link_text, $stripteaser);
    $content = apply_filters('the_content', $content);
    $content = str_replace(']]>', ']]&gt;', $content);
    // apply you own string replace here
    echo $content;
}
5
  • Hi Thanks for your answer. Does this work globally? I'm trying to achieve this outside of wordpress. It's a mailer template loading content from wordpress via wp-load. So I need php to work in my document. I can still use the functions.php, but how do I call it so it just works on my specific the_content?
    – Joshc
    Nov 15, 2012 at 15:09
  • See here how I'm trying to apply it... gist.github.com/0e85ea3b81eddf48376c
    – Joshc
    Nov 15, 2012 at 15:15
  • Thanks for your help, but came up with a individual solution below, sorry for not explaining my question properly first time.
    – Joshc
    Nov 15, 2012 at 15:26
  • Putting the answer anyway for future visitors. Yes, it will work if the_content() works. just one small difference <?php the_content(); ?> becomes <?php custom_the_content(); ?>. This function is meant to be a replacement Nov 15, 2012 at 15:28
  • Ahhhh I see now, I was not sure how. That's is good. Again thanks for your time.
    – Joshc
    Nov 15, 2012 at 15:31
1

Use filter and actions:

/**
 * Plugin Name: Your_awsome_inlinestyle
 * Plugin URI:  http://wordpress.stackexchange.com/questions/72681/how-to-add-an-inline-style-to-the-p-tag-outputted-in-the-content-using-php
 * Description: See link to plugin
 * Version:     0.1
 * Author:      Ralf Albert
 * Author URI:  http://yoda.neun12.de
 * Text Domain:
 * Domain Path:
 * Network:
 * License:     GPLv3
 */
    add_action( 'plugins_loaded', 'init_inlinestyler', 10, 0 );

    function init_inlinestyler(){

        add_filter( 'the_content', 'add_inlinestyle_to_p_tag', 10, 1 );

    }

    function add_inlinestyle_to_p_tag( $content = null ){

        if( null === $content )
            return $content;

        return str_replace( '<p>', '<p style="color:red">', $content );

    }

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:

<?php
// add the filter
    add_filter( 'the_content', 'insert_inline_style', 10, 1 );

// output the post content
    the_content();

// remove the filter if it is not longer needed
    remove_filter( 'the_content', 'insert_inline_style' );

So you don't have to fiddle around with get_the_content() and can easily reuse the callback if needed.

3
  • Hi Thanks for your answer. Does this work globally? I'm trying to achieve this outside of wordpress. It's a mailer template loading content from wordpress via wp-load. So I need php to work in my document. I can still use the functions.php, but how do I call it so it just works on my specific the_content?
    – Joshc
    Nov 15, 2012 at 15:09
  • See here how I'm trying to apply it... gist.github.com/0e85ea3b81eddf48376c
    – Joshc
    Nov 15, 2012 at 15:15
  • Thanks for your help, but came up with a individual solution below, sorry for not explaining my question properly first time.
    – Joshc
    Nov 15, 2012 at 15:27
1

I might be late for this one, but use this :

ob_start();
the_content();
$content = ob_get_clean();

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 as get_the_content() does not. This is why many answers would try to filter get_the_content(), which is quite troublesome.

4
  • 2
    Please explain why this will work Jul 11, 2014 at 19:08
  • @PieterGoosen Would be easier for me to explain in french. Look up buffering. 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 as get_the_content() does not. This why many answer would try to filter get_the_content(), which is quite troublesome.
    – Pobe
    Jul 15, 2014 at 14:55
  • Thanks, but you should file an edit and add that directly to your answer Jul 15, 2014 at 15:02
  • 1
    well it is done @PieterGoosen :)
    – Pobe
    Jul 15, 2014 at 15:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.