There have been numerous times when I needed the filter capabilities of the_content, but I didn’t want other plugin and/or theme authors to be able to hook into it and add in their own stuff.

Here’s a code snippet I use quite regularly (please add in your own custom prefix).

<?php
/* Filter for the content - Custom */
add_action( 'init', 'city_the_content', 1 );
function city_the_content() {
	//Create our own version of the_content so that others can't accidentally loop into our output - Taken from default-filters.php, shortcodes.php, and media.php
	if ( !has_filter( 'city_the_content', 'wptexturize' ) ) {
		add_filter( 'city_the_content', 'wptexturize'        );
		add_filter( 'city_the_content', 'convert_smilies'    );
		add_filter( 'city_the_content', 'convert_chars'      );
		add_filter( 'city_the_content', 'wpautop'            );
		add_filter( 'city_the_content', 'shortcode_unautop'  );
		add_filter( 'city_the_content', 'prepend_attachment' );
		$vidembed = new WP_Embed();
		add_filter( 'city_the_content', array( &$vidembed, 'run_shortcode'), 8 );
		add_filter( 'city_the_content', array( &$vidembed, 'autoembed'), 8 );
		add_filter( 'city_the_content', 'do_shortcode', 11);
	} //end has_filter
} //end city_the_content
?>

Some sample use-cases:

  • Run on excerpts
  • Run on Widget text output
  • Run on just about any text, really

It applies the same formatting rules, and even includes the fancy oEmbed capabilities.

Enjoy.

Tags:

Comments are closed.