Home > Net >  I have create custom shortcode in wordpress but it show error
I have create custom shortcode in wordpress but it show error

Time:02-22

When I try to display a shortcode in pages it shows this 'Updating failed. The response is not a valid JSON response' error but the page gets updated and no error shows in the frontend. When I use the classic editor it works fine but issues with block editor.

Here its code

<?php
class EmailPluginfront {

    function __construct() {
        
        add_shortcode('email', array($this,'front_display'));  
        
    }  
function front_display() {
                   
echo '<h2 style="text-align:center; margin-bottom:30px">'.get_option('email_title_field').'</h2>';
}
}
$EmailPluginfront = new EmailPluginfront();

CodePudding user response:

you need to return data at the end of your function. try using this in your shortcode function

ob_start();
echo("Hello there!"); // all your code here
$output = ob_get_contents();
ob_end_clean();
return $output

CodePudding user response:

Can you please use 'return' instead of the 'echo'?

  • Related