WordPress Code Standards and Working $link Parameters In Shortcode

I Have this snippet code,

add_shortcode( 'parent-child', 'taxonomy_hierarchy' );
function taxonomy_hierarchy( $atts ){
    extract( shortcode_atts( array(
        'link' => true,
        'taxonomy' => 'property_city'
    ), $atts, 'parent-child' ) );

    global $post;
    $terms = wp_get_post_terms( $post->ID, $taxonomy );

    /* You can pass conditions here to override
     * $link based on certain conditions. If it's
     * a single post, current user is editor, etc.
     */

    ob_start();
    foreach( $terms as $term ){
        if( $term->parent != 0 ){
            $parent_term = get_term( $term->parent, $taxonomy );
            echo ($link != false) ? sprintf( '<a href="%s">%s</a>, ', esc_url( get_term_link($parent_term) ), $parent_term->name ) : "{$parent_term->name}, " ;
        }

        echo ($link != false) ? sprintf( '<a href="%s">%s</a>', esc_url( get_term_link($term) ), $term->name ) : $term->name ;
    }   

    return ob_get_clean();
}

This will allow you to get the following results:
usage

**[parent-child]**
• <a href="#">New York</a>
• <a href="#">New York</a>, <a href="#">Manhattan</a>

**[parent-child link="true"]**
• <a href="#">New York</a>
• <a href="#">New York</a>, <a href="#">Manhattan</a>

**[parent-child link="false"]**
• New York
• New York, Manhattan

[parent-child link="false" taxonomy="some_other_taxonomy"]
• Top Level Term
• Top Level Term, Child Level Term

Everything works fine and displays as expected! just…my parameters do not work.

I failed to get [parent-child link=”false”] work..
During my research on how shortcodes work, I came across these posts,

link1
link2
link3

they all talk about never using extract and echo in shortcodes this is so bad practice in wordpress code and using $atts and $return instead.

which casts doubt on the effectiveness of this code…

My question now is : how to make this shortcode [parent-child] working with link=false , and no extract and echo in shortcode.

thank you if you understood what i’m talking about and knows how to make it more respectful of wordpress standards

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

how to make this shortcode [parent-child] working with link=false , and no extract in shortcode.

You can never pass boolean value as shortcode parameter, rather it should be treated as string. Your comparison over the param link value false should be used as 'false' (string).

add_shortcode( 'parent-child', 'taxonomy_hierarchy' );
function taxonomy_hierarchy( $atts ){
    // Don't extract, rather parse shortcode params with defaults.
    $atts = shortcode_atts( array(
        'link' => 'true',
        'taxonomy' => 'property_city'
    ), $atts, 'parent-child' );

    // Check the $atts parameters and their typecasting.
    var_dump( $atts );

    global $post;
    $terms = wp_get_post_terms( $post->ID, $atts['taxonomy'] );

    /* You can pass conditions here to override
     * $link based on certain conditions. If it's
     * a single post, current user is editor, etc.
     */

    ob_start();
    foreach( $terms as $term ){
        if( $term->parent != 0 ){
            $parent_term = get_term( $term->parent, $taxonomy );
            if ($atts['link'] !== 'false') {
                printf( '<a href="%s">%s</a>, ', esc_url( get_term_link($parent_term) ), $parent_term->name );
            } else {
                echo $parent_term->name . ', ';
            }
        }

        if ($atts['link'] !== 'false') {
            printf( '<a href="%s">%s</a>', esc_url( get_term_link($term) ), $term->name );
        } else {
            echo $term->name;
        }
    }

    return ob_get_clean();
}

no echo in shortcode

It’s not about using echo, you are asked to not echo output within shortcode. Rather, you should return it as WordPress will replace shortcode with your output value. Here, you are buffering your output using ob_start() and ob_get_clean() functions and returning it. Which is just fine, and commonly used techniques.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x