Search code examples
phpwordpresseditorshortcode

Invalid JSON response block editor


When I add this shortcode that I created, on a page, the code works fine and as expected. But when I want to edit the page in Block editor, there is a message that states "Invalid JSON response block editor".

I'm not extremely concerned, but will someone please have a look at my code and let me know if the problem lies with my code, or if it is just a glitch with the block editor?

Am I perhaps missing brackets somewhere {}

Thank you

function gk_username( $current_user ) { 
    $current_user = wp_get_current_user();
    echo 'Username: ' . $current_user->user_login . '<br />';
    echo 'User email: ' . $current_user->user_email . '<br />';
}

add_shortcode( 'gk_user', 'gk_username');

/*Use this shortcode: [gk_user]*/```

Solution

  • @ Aida

    Thank you, you helped me to figure it out. Instead of echoing the result, it must be returned because a shortcode is being created.

    So I changed the code to the following and there are no more errors and it is working. Thank you.

        $current_user = wp_get_current_user();
        return 'Username: ' . $current_user->user_login . '<br />';
    }
    
    add_shortcode( 'gk_user', 'gk_username');
    
    /*Use this shortcode: [gk_user]*/ ```