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.
post_class
andbody_class
the way it should you should rarely, if ever, need to do this.