WP shortcode to change bbPress tag-cloud font size

This post shows:
– how to write your own WordPress [shortcode] with multiple parameters; – how to change font size and number of tags displayed on bbPress tag cloud.

 

Power to the People

If you can easily write your own WP shortcode and place them in a desired spot on a page or post – you increase control of your site and you power increase dramatically.  Let’s do it.

 

Problem

bbPress tag cloud shortcode [bbp-topic-tags] implemented without parameters. Related code located inside file plugins/bbpress/includes/common/shortcodes.php at line 522.  There are 3 hard-coded parameters here: smallest font size (9), largest font size (38), and number of tags to display (80).

You can stop reading right here, and simply change these default parameters right here, but you will lose your changes upon next bbPress upgrade. If that is not acceptable – keep reading.

 

Simple Solution – no parameters

 

Usage on the page/post:

[bbp-tag-cloud]

 

Code you need to add to your child theme functions.php file:

function my_bbp_tag_cloud() {
ob_start();
wp_tag_cloud( array(
‘smallest’ => 10,   ‘largest’  => 18,   ‘number’   => 27,
‘taxonomy’ => bbp_get_topic_tag_tax_id()  ) );
return ob_get_clean();
}
add_shortcode(‘bbp-tag-cloud’, ‘my_bbp_tag_cloud’);

 

Note: you need to use PHP ob_start() and ob_get_clean() functions to force output exactly at the point where you place your shortcode, and not at the top of the post.

 

Harder Solution – multiple parameters

Usage on the page/post:

[bbPress-tag-cloud smallest="12" largest="20" number="22"]

Code in your child theme function.php file:

function param_bbp_tag_cloud( $atts) {
extract(shortcode_atts(array(
‘smallest’ => 10, ‘largest’  => 18, ‘number’   => 27 ), $atts));
ob_start();
wp_tag_cloud( array(
‘smallest’ => $atts[‘smallest’],
‘largest’ => $atts[‘largest’],
‘number’ => $atts[‘number’],
‘taxonomy’ => bbp_get_topic_tag_tax_id() ) );
return ob_get_clean();
}
add_shortcode(‘bbPress-tag-cloud’, ‘param_bbp_tag_cloud’);

 

 

Summary

In this article we solved several problems:
– stopped using bbPress tag cloud shortcode that is not flexible;
– moved the control over font size and number of tags in the cloud from core file to the page;
– learned how to pass parameters from shortcode to the function.

The only thing left to do is to shorten the code, make it easier to read and to maintain.

(Visited 372 times, 1 visits today)

Be the first to comment

Your question, correction or clarification Ваш вопрос, поправка или уточнение