Mario
Mario

Reputation: 177

Wordpress custom plugin development issue: Class function that outputs HTML, outputs duplicate HTML in the WP editor page

I'm developing a custom WP plugin with my own custom PHP Class with its function. I need it to output the html from the display_papers() class/function, by using a custom shortcode on my WP page.

As you can see below, I even thought perhaps that ob_start() had to be used (code commented out below).

The issue I have is, the echoing out of the HTML is displaying TWICE in the WP EDITOR (not on the front of page - which shows correctly) and even in the Elementor editor. The first (duplicate) echo almost seems as if it's echoing outside the html/shortcode container of the WP editor (when I refresh editor page, it renders first before the editor page is loaded.

I'm baffled with the following, what seems a simple function in a class:

The class:

class Pastpapers {
    public function __construct() {
        // nothing here yet
    }

    public function display_papers() {
        //ob_start();
    
        echo '<h2>hello biscuit!</h2>';

        //ob_get_contents();
        //ob_end_flush();
    }
}

I instantiate my custom class and call the display_papers() function within it (wrapped in my custom shortcode on a WP page):

// the paper list shortcode
function dbase_list_papers( $atts) {

    $pastpapers = new Pastpapers(); // instantiate class
    $pastpapers->display_papers();  // call class function to echo html onto shortcode page

}
add_shortcode ('list_papers', 'dbase_list_papers');

EDIT TO ADD: One thing that crossed my mind is: Is it a possible passing of html headers issue? Although I don't get any errors at all (ie. WP_DEBUG)

Upvotes: 0

Views: 53

Answers (0)

Related Questions