How to add an inline style to the <p> tag outputted in the_content() using PHP?

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.

Read More

Can anyone help me with this?

Related posts

Leave a Reply

4 comments

  1. 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;
    }
    
  3. 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.

  4. 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.