0

I'm writing a simple plugin for wordpress that changes a single word on a page or a post to make it bold.

For example: vlbs -> vlbs

It works fine for normal Wordpress pages and posts with this code:

defined('ABSPATH') or die('You can\'t enter this site');

class VLBS {

    function __construct() {

    }

    function activate() {
        flush_rewrite_rules();
    }

    function deactivate() {
        flush_rewrite_rules();
    }

    function unstinstall() {
    }

function new_content($content) {
                return $content = str_replace('vlbs','<strong style="color:#00a500">vlbs</strong>', $content);    
}

}

if(class_exists('VLBS')){
    $VLBS = new VLBS();

}

add_filter('the_content', array($VLBS, 'new_content'));

//activation
register_activation_hook(__FILE__, array($VLBS, 'activate'));

//deactivation
register_deactivation_hook(__FILE__, array($VLBS, 'deactivate'));

However, it does not work on a page built with Yootheme Pro Pagebuilder. Whatever is done within the function new_content() is processed after the content has already been loaded. Thus, I cannot manipulate it before it is displayed to the user.

So the question would be: How can I get the content of a page before it is displayed? Is there an equivalent to Wordpress' 'the_content'?

Any help is really appreciated! Thank you very much in advance.

Best regards Fabian

Yootheme: 1.22.5
Wordpress: 5.2.4
PHP: 7.3
Browser: Tested on Chrome, Firefox, Edge, Internet Explorer

FabianH
  • 3
  • 1

1 Answers1

0

In your code, do you are sure it's the good usage of add_filter content ?

In the doc, the 2nd parameter is string, not array:

add_filter( 'the_content', 'filter_the_content_in_the_main_loop' );

function filter_the_content_in_the_main_loop( $content ) {

    // Check if we're inside the main loop in a single post page.
    if ( is_single() && in_the_loop() && is_main_query() ) {
        return $content . esc_html__("I'm filtering the content inside the main loop", "my-textdomain");
    }

    return $content;
}

In wordpress, the_content function display the content. There is an other function for get_the_content

Go to your page file and get the content. You can use str_replace and echo the new content after.

Example single.php :

if ( $query->have_posts() ) :               
    while( $query->have_posts() ) : $query->the_post();

        $content = get_the_content();

        $new_content = str_replace(search, replace, $content);

        echo $new_content;

    endwhile;
endif;

If it is not possible for you, try to use the output buffer functions. If you need use this functions, I say it and I developpe more this part. But test the solution above before.

Oh, and it exist a special community for WP where your question will more pertinent : https://wordpress.stackexchange.com/

Xenofexs
  • 511
  • 4
  • 14
  • Hey, thanks for your help. It is really appreciated. I tested all your recomended solutions. I had to edit the themes page.php directly to make it work. Which might only be a temporary workaround since other installed plugins (eg. a membersarea plugin can't block access to pages built with yootheme pro since it is executed only after the content is already displayed) don't work as well. However, this was the right answer for this particular problem. So thank you very much :) – FabianH Nov 26 '19 at 15:44