0

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.com/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');
1
  • Will I have to create a custom template in order to get the shortcode to render on a page? Jul 18, 2019 at 20:15

1 Answer 1

2

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 .. :)

5
  • Thank you for your reply. I will look deeper into it shortly. The codex quote is enlightening. A couple of notes off of the top of my head: It is a gutenberg/block issue in the way that it reformats my code (adds quotes) even in a classic(or html) block and using the code editor. It is not possible to remove the quotes as gutenberg just adds them after I press save. The pages that have not been edited since before gutenberg do have the "s around the shortcode and still function as expected. Jul 19, 2019 at 16:42
  • I do appreciate your perspective and thoughts on the matter. I had not tried to create a short code in the way that you did above. I did create a template. It also failed to output the username, possible due to the quotes. I think that you are correct in suggesting that I create a short code with the entire form in it. I will explore that option. Thank you. Jul 19, 2019 at 16:42
  • 1
    The point is, don't use shortcodes in HTML attributes. =) And do try my suggestions other than the dirty trick.. that trick is in the answer just so you know it worked for me. Also, if the answer helped you, you can vote it up..
    – Sally CJ
    Jul 19, 2019 at 19:20
  • So did my other suggestions help? What's your solution? Let us know about it.
    – Sally CJ
    Jul 24, 2019 at 6:01
  • 1
    Thank you, Sally. I just got back from traveling and had time to test your solution. Putting the entire form in a shortcode works well. Thank you for your time and input. Jul 24, 2019 at 20:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.