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
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
Share Improve this question asked Jan 7, 2021 at 12:02 neosanneosan 1175 bronze badges1 Answer
Reset to default 2how 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.