WP 5.2.2
Is it possible to render a shortcode outside of the gutenberg shortcode block?
I created a shortcode and have been using it on a couple of pages for years. It works great on those pages... I am afraid to edit them in fear of them breaking :/
I created a new page that I would like use the shortcode on. However, the shortcode is not being processed. I have tried inserting it via the code editor and using it in an html block. Either way it fails to render.
If I create a shortcode block and put my shortcode in it, it works. However, since I use the short code to fill in a hidden form field, I do not believe that option will work for me. It hasn't in my testing.
Example:
<form action=".php" method="post">
<input name="name" value="[current_user]" type="hidden">
<input name="currency" value="USD" type="hidden">
<input name="tax" value="0" type="hidden">
<input name="btn" value="mybutton" type="hidden">
</form>
My shortcode:
function custom_shortcode_func() {
ob_start();
$current_user = wp_get_current_user();
echo $current_user->user_login;
$output = ob_get_clean();
return $output;
}
add_shortcode('current_user', 'custom_shortcode_func');
WP 5.2.2
Is it possible to render a shortcode outside of the gutenberg shortcode block?
I created a shortcode and have been using it on a couple of pages for years. It works great on those pages... I am afraid to edit them in fear of them breaking :/
I created a new page that I would like use the shortcode on. However, the shortcode is not being processed. I have tried inserting it via the code editor and using it in an html block. Either way it fails to render.
If I create a shortcode block and put my shortcode in it, it works. However, since I use the short code to fill in a hidden form field, I do not believe that option will work for me. It hasn't in my testing.
Example:
<form action="https://www.example/process.php" method="post">
<input name="name" value="[current_user]" type="hidden">
<input name="currency" value="USD" type="hidden">
<input name="tax" value="0" type="hidden">
<input name="btn" value="mybutton" type="hidden">
</form>
My shortcode:
function custom_shortcode_func() {
ob_start();
$current_user = wp_get_current_user();
echo $current_user->user_login;
$output = ob_get_clean();
return $output;
}
add_shortcode('current_user', 'custom_shortcode_func');
Share
Improve this question
edited Jul 18, 2019 at 20:53
lost.in.userspace
asked Jul 18, 2019 at 20:13
lost.in.userspacelost.in.userspace
651 silver badge5 bronze badges
1
- Will I have to create a custom template in order to get the shortcode to render on a page? – lost.in.userspace Commented Jul 18, 2019 at 20:15
1 Answer
Reset to default 3The 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
.. :)