• Resolved tomcatwpdev

    (@tomcatwpdev)


    Hello guys, I have a shortcode create from my custom plugin.
    This is how I create that shortcode from plugin:

    class test_plugin{
    	function __construct()
            {
                register_activation_hook(__FILE__, array($this, 'plugin_activation'));
                register_deactivation_hook(__FILE__, array($this, 'plugin_deactivation'));
                register_uninstall_hook( __FILE__, 'plugin_uninstallation' );
            
           add_shortcode('my_custom_view_shortcode',array($this,'my_custom_view_handler'));
    
            }
            public static function my_custom_view_handler(){
                echo '<div>My view</div>';
            }
    }
    $test_plugin = new test_plugin();

    After activating plugin, I created new page and added shortcode to shortcode block like this [‘my_custom_view_shortcode’].
    And now, whenever I press publish or update button in any page/post which contains my custom shortcode [‘my_custom_view_shortcode’] will give me the error ‘Publishing/Updating failed. The response is not a valid JSON response.’
    I reloaded my page, all information was still saved in database and showed expected result ‘<div>My view</div>’.
    Can you help me how to fix that error? Thank you

Viewing 2 replies - 1 through 2 (of 2 total)
  • catacaustic

    (@catacaustic)

    First, shortcodes must return the HTML code, not output it.

    So your shortcode functions hould be this:

    return '<div>My view</div>';

    If you try to output things as yu are there, they will end up in the wrong place, and will normalyl break your admin area when you try to update pages because the output will be included when it shouldn’t be.

    Thread Starter tomcatwpdev

    (@tomcatwpdev)

    Thank you so much @catacaustic , I forgot this and I fixed it.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘shortcode from plugin causes The response is not a valid JSON response Error’ is closed to new replies.