admin管理员组

文章数量:1299997

Does the id in a shortcode such as [reusable id='7'] or [reusable id=7] always get interpreted by shortcode_atts()as a string regardless of whether the integer is in single quotes or not?

I'm trying to make a shortcode that will let me use gutenberg reusable blocks outside of the editor. I've found a code snippet to do this, but being new to wordpress development have had some trouble understanding why it won't work. So far my search for a reason why has come up short, so hello StackExchange!

This is the snippet i'm using. The original source is here

function reusable_block_shortcode( $atts ) {

    function get_reusable_block( $block_id = '' ) {
        if ( empty( $block_id ) || (int) $block_id !== $block_id ) {
            return;
        }

        $content = get_post_field( 'post_content', $block_id );
        return apply_filters( 'the_content', $content );
    } 


    $args = ( shortcode_atts( array('id' => ''), $atts ) );
    $id = $args['id'];


    if ( empty( $id ) || (int) $id !== $id ) {
         return;
    }

    $content = get_reusable_block( $id );
    return $content;

}
add_shortcode( 'reusable', 'reusable_block_shortcode' );

Having played around with the snippet a bit I've found the problem to be with both if statements, in particular

 if( (int) $id !== $id ) {
     return;
}

To my understanding the if statement is returning false, because $id is a string? I'm inferring this because if I use a non-strict equality operation (!=) the if statement returns true and the shortcode works. Am I correct?

If so, does the id in a shortcode such as [reusable id='7'] or [reusable id=7] always get interpreted by shortcode_atts()as a string regardless of whether the integer is in single quotes or not? I've tried the shortcode with and without single quotes but it only works if a I remove the strict equality operation from the if statement in the code snippet.

This makes me think that shortcode_atts() is always interpreting the shortcode attribute as a string, is this true?

If so, is the equality portion of the if statement even necessary from a coding standards perspective.

I expect the answer will have implications for booleans and other data types too. Thanks in advance for helping me understand this better!

Does the id in a shortcode such as [reusable id='7'] or [reusable id=7] always get interpreted by shortcode_atts()as a string regardless of whether the integer is in single quotes or not?

I'm trying to make a shortcode that will let me use gutenberg reusable blocks outside of the editor. I've found a code snippet to do this, but being new to wordpress development have had some trouble understanding why it won't work. So far my search for a reason why has come up short, so hello StackExchange!

This is the snippet i'm using. The original source is here

function reusable_block_shortcode( $atts ) {

    function get_reusable_block( $block_id = '' ) {
        if ( empty( $block_id ) || (int) $block_id !== $block_id ) {
            return;
        }

        $content = get_post_field( 'post_content', $block_id );
        return apply_filters( 'the_content', $content );
    } 


    $args = ( shortcode_atts( array('id' => ''), $atts ) );
    $id = $args['id'];


    if ( empty( $id ) || (int) $id !== $id ) {
         return;
    }

    $content = get_reusable_block( $id );
    return $content;

}
add_shortcode( 'reusable', 'reusable_block_shortcode' );

Having played around with the snippet a bit I've found the problem to be with both if statements, in particular

 if( (int) $id !== $id ) {
     return;
}

To my understanding the if statement is returning false, because $id is a string? I'm inferring this because if I use a non-strict equality operation (!=) the if statement returns true and the shortcode works. Am I correct?

If so, does the id in a shortcode such as [reusable id='7'] or [reusable id=7] always get interpreted by shortcode_atts()as a string regardless of whether the integer is in single quotes or not? I've tried the shortcode with and without single quotes but it only works if a I remove the strict equality operation from the if statement in the code snippet.

This makes me think that shortcode_atts() is always interpreting the shortcode attribute as a string, is this true?

If so, is the equality portion of the if statement even necessary from a coding standards perspective.

I expect the answer will have implications for booleans and other data types too. Thanks in advance for helping me understand this better!

Share Improve this question asked Apr 7, 2021 at 11:34 mrfisch93mrfisch93 31 bronze badge
Add a comment  | 

1 Answer 1

Reset to default 1

Yes, shortcode attributes are always strings. WordPress has no way of knowing the intended type of each attribute, and the values are ultimately pulled from an HTML string, after all.

If you need $id to be an integer, just do this:

$args = ( shortcode_atts( array('id' => ''), $atts ) );
$id   = (int) $args['id'];


if ( ! $id ) {
     return;
}

$content = get_reusable_block( $id );

本文标签: Are Shortcode Attributes Always Passed As Strings