The shortcode doesn’t work because it’s wrapped inside quotes — "[current_user]"
. But a better explanation is, because it’s in a HTML attribute.
And it’s not a Gutenberg or the block editor problem; even with <?php echo do_shortcode( '<input name="name" value="[current_user]" type="hidden">' ); ?>
, the shortcode would still remain as-is. Because shortcodes in HTML attributes are not allowed — see below excerpt from the Codex:
Starting with version 4.2.3, similar limitations were placed on use of
shortcodes inside HTML. For example, this shortcode will not work
correctly because it is nested inside a scripting attribute:
<a onclick="[tag]">
There’s a quick (and dirty) fix for that, though; don’t wrap it in quotes:
<input name="name" value=[current_user] type="hidden">
But that results in an invalid HTML (unwrapped attribute value), so I’d just create a shortcode which outputs the entire form
:
function custom_shortcode_func2() {
$current_user = wp_get_current_user();
$user_login = isset( $current_user->user_login ) ?
$current_user->user_login : '';
ob_start();
?>
<form action="" method="post">
<input name="name" value="<?php echo esc_attr( $user_login ); ?>" type="hidden">
<input name="currency" value="USD" type="hidden">
<input name="tax" value="0" type="hidden">
<input name="btn" value="mybutton" type="hidden">
</form>
<?php
return ob_get_clean();
}
add_shortcode( 'my_form', 'custom_shortcode_func2' );
Or just the <input>
tag:
function custom_shortcode_func3() {
$current_user = wp_get_current_user();
$user_login = isset( $current_user->user_login ) ?
$current_user->user_login : '';
return sprintf( '<input name="name" value="%s" type="hidden">',
esc_attr( $user_login ) );
}
add_shortcode( 'input_current_user', 'custom_shortcode_func3' );
Btw, referring to your original shortcode function, there’s no need to use output buffering (those ob_
functions). Just return the $current_user->user_login
.. 🙂