Home > Enterprise >  Elementor page builder shortcode issue - unable to include external PHP file when using ob_start and
Elementor page builder shortcode issue - unable to include external PHP file when using ob_start and

Time:03-11

I have a weird problem when I try to include my custom wordpress plugin shortcode within elementor. Whenever I use an external php file there is no output.

This code is working fine:

// Shortcode Output function
function vergleichsplugin_output_frontend() 
{

        ob_start();

        echo '<div ></div>';

        return ob_get_clean();

}


/* Shortcodes */ 
add_shortcode('vergleichsplugin','vergleichsplugin_output_frontend'); 

But this doesn't produce any output at all (file path is correct):

// Shortcode Output function
function vergleichsplugin_output_frontend() 
{

    ob_start();


    $html = require_once(ABSPATH.'/wp-content/plugins/vergleichsplugin/views/frontend/frontend.php'); 


    $html = $html.ob_get_clean();
    return $html;
}



/* Shortcodes */ 
add_shortcode('vergleichsplugin','vergleichsplugin_output_frontend'); 

content of frontend.php is the same:

echo '<p>Output</p>'; 

CodePudding user response:

Have you tried something like this?

function vergleichsplugin_output_frontend() {
    ob_start();

    include(ABSPATH.'/wp-content/plugins/vergleichsplugin/views/frontend/frontend.php'); 

    return ob_get_clean();
}
add_shortcode('vergleichsplugin','vergleichsplugin_output_frontend'); 

Why wasnt your solution working?

in order to save the contents of you file to a variable with require_once or include you would need to return all your html in that file. something like:

// frontend.php

<?php

$html = "<h1>Some sample html</h1>";

return $html;

?>

Also this section is not valid:

    $html = $html.ob_get_clean();
    return $html;

Should be:

    $html = ob_get_clean();
    return $html;

Since using a buffer you dont need to save the require to a variable as the html will be returned by ob_get_clean;

  • Related