最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

block editor - Gutenberg Shortcode Fail

programmeradmin0浏览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=".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
Add a comment  | 

1 Answer 1

Reset to default 3

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

发布评论

评论列表(0)

  1. 暂无评论