Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

share|improve this question
    
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 '12 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 '12 at 15:15
    
Solved it...... –  Joshc Nov 15 '12 at 15:27
    
That makes sense. You found a rare case. –  s_ha_dum Nov 15 '12 at 15:33
add comment

3 Answers

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(']]>', ']]>', $content);
    // apply you own string replace here
    echo $content;
}
share|improve this answer
    
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 '12 at 15:09
    
See here how I'm trying to apply it... gist.github.com/0e85ea3b81eddf48376c –  Joshc Nov 15 '12 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 '12 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 –  Mridul Aggarwal Nov 15 '12 at 15:28
    
Ahhhh I see now, I was not sure how. That's is good. Again thanks for your time. –  Joshc Nov 15 '12 at 15:31
add comment

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.

share|improve this answer
    
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 '12 at 15:09
    
See here how I'm trying to apply it... gist.github.com/0e85ea3b81eddf48376c –  Joshc Nov 15 '12 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 '12 at 15:27
add comment
up vote 0 down vote accepted

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);

?>
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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