Wednesday, November 6, 2013

Change or modify elements in the content in Wordpress

Have you ever wanted to change the output of a Wordpress page or a post? Maybe modify the H1-elements in the content or something else. The solution is quite simple to achieve this.

Lets say we wanted to add something, in this case a header-element before the H1-element and a closing one right after. This is how you do it:

Put this code below in your functions.php-file:
function filter_h1_pages( $content ) {
$content = str_ireplace( '<h1>', '<header><h1>', $content ); return str_ireplace( '</h1>', '</header></h1>', $content );}add_filter( 'the_content', 'filter_h1_pages' );

What the code does is replacing a string with something else, twice and then return it. So we have filtered the main function the_content in Wordpress.

No comments:

Post a Comment