Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't change <p> tag in the_content using string replace

Tags:

php

wordpress

I am struggling to get a simple string replace to work in wordpress the_content function.

<?php 

    $phrase = the_content();
    $find = '<p>';
    $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;">';

    $newphrase = str_replace($find, $replace, $phrase);

    echo $newphrase;

?>


It just seems to echoing <p> still.

Instead of <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;">

like image 397
Joshc Avatar asked Dec 26 '22 14:12

Joshc


1 Answers

You need to use apply_filters('the_content') to have Wordpress newlines with paragraphs.

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

?>

See the codex entry for the_content. Its in the alternative usage section.

like image 187
max Avatar answered Feb 12 '23 04:02

max