bbp_get_dropdown( array $args = array() )

Return a select box allowing to pick which forum/topic a new topic/reply belongs in.


Description Description


Parameters Parameters

$args

(Optional) The function supports these args: - post_type: Post type, defaults to bbp_get_forum_post_type() (bbp_forum) - selected: Selected ID, to not have any value as selected, pass anything smaller than 0 (due to the nature of select box, the first value would of course be selected - though you can have that as none (pass 'show_none' arg)) - orderby: Defaults to 'menu_order title' - post_parent: Post parent. Defaults to 0 - post_status: Which all post_statuses to find in? Can be an array or CSV of publish, category, closed, private, spam, trash (based on post type) - if not set, these are automatically determined based on the post_type - posts_per_page: Retrieve all forums/topics. Defaults to -1 to get all posts - walker: Which walker to use? Defaults to BBP_Walker_Dropdown - select_id: ID of the select box. Defaults to 'bbp_forum_id' - tab: Deprecated. Tabindex value. False or integer - options_only: Show only <options>? No <select>? - show_none: Boolean or String __( '— No Forum —', 'bbpress' ) - disable_categories: Disable forum categories and closed forums? Defaults to true. Only for forums and when

Default value: array()


Top ↑

Return Return

(string) The dropdown


Top ↑

Source Source

File: includes/common/template.php

	function bbp_get_dropdown( $args = array() ) {

		// Setup return value
		$retval = '';

		/** Arguments *********************************************************/

		// Parse arguments against default values
		$r = bbp_parse_args( $args, array(

			// Support for get_posts()
			'post_type'          => bbp_get_forum_post_type(),
			'post_parent'        => null,
			'post_status'        => null,
			'selected'           => 0,
			'include'            => array(),
			'exclude'            => array(),
			'numberposts'        => -1,
			'orderby'            => 'menu_order title',
			'order'              => 'ASC',

			// Preloaded content
			'posts'              => array(),

			// Custom hierarchy walkers
			'walker'             => '',

			// Output-related
			'select_id'          => 'bbp_forum_id',
			'select_class'       => 'bbp_dropdown',
			'tab'                => false,
			'options_only'       => false,
			'show_none'          => false,
			'disable_categories' => true,
			'disabled'           => ''
		), 'get_dropdown' );

		// Fallback to our walker
		if ( empty( $r['walker'] ) ) {
			$r['walker']            = new BBP_Walker_Dropdown();
			$r['walker']->tree_type = $r['post_type'];
		}

		// Force 0
		if ( is_numeric( $r['selected'] ) && ( $r['selected'] < 0 ) ) {
			$r['selected'] = 0;
		}

		// Force array
		if ( ! empty( $r['include'] ) && ! is_array( $r['include'] ) ) {
			$r['include'] = explode( ',', $r['include'] );
		}

		// Force array
		if ( ! empty( $r['exclude'] ) && ! is_array( $r['exclude'] ) ) {
			$r['exclude'] = explode( ',', $r['exclude'] );
		}

		/** Setup Posts *******************************************************/

		/**
		 * Allow passing of custom posts data
		 *
		 * @see bbp_get_reply_to_dropdown() as an example
		 */
		if ( empty( $r['posts'] ) ) {
			$r['posts'] = get_posts( array(
				'post_type'   => $r['post_type'],
				'post_status' => $r['post_status'],
				'post_parent' => $r['post_parent'],
				'include'     => $r['include'],
				'exclude'     => $r['exclude'],
				'numberposts' => $r['numberposts'],
				'orderby'     => $r['orderby'],
				'order'       => $r['order'],
			) );
		}

		/** Drop Down *********************************************************/

		// Build the opening tag for the select element
		if ( empty( $r['options_only'] ) ) {

			// Should this select appear disabled?
			$disabled  = disabled( isset( bbpress()->options[ $r['disabled'] ] ), true, false );

			// Setup the tab index attribute
			$tab       = ! empty( $r['tab'] ) ? ' tabindex="' . intval( $r['tab'] ) . '"' : '';

			// Open the select tag
			$retval   .= '<select name="' . esc_attr( $r['select_id'] ) . '" id="' . esc_attr( $r['select_id'] ) . '" class="' . esc_attr( $r['select_class'] ) . '"' . $disabled . $tab . '>' . "\n";
		}

		// Display a leading 'no-value' option, with or without custom text
		if ( ! empty( $r['show_none'] ) || ! empty( $r['none_found'] ) ) {

			// Open the 'no-value' option tag
			$retval .= "\t<option value=\"\" class=\"level-0\">";

			// Use deprecated 'none_found' first for backpat
			if ( ! empty( $r['none_found'] ) && is_string( $r['none_found'] ) ) {
				$retval .= esc_html( $r['none_found'] );

			// Use 'show_none' second
			} elseif ( ! empty( $r['show_none'] ) && is_string( $r['show_none'] ) ) {
				$retval .= esc_html( $r['show_none'] );

			// Otherwise, make some educated guesses
			} else {

				// Switch the response based on post type
				switch ( $r['post_type'] ) {

					// Topics
					case bbp_get_topic_post_type() :
						$retval .= esc_html__( 'No topics available', 'bbpress' );
						break;

					// Forums
					case bbp_get_forum_post_type() :
						$retval .= esc_html__( 'No forums available', 'bbpress' );
						break;

					// Any other
					default :
						$retval .= esc_html__( 'None available', 'bbpress' );
						break;
				}
			}

			// Close the 'no-value' option tag
			$retval .= '</option>';
		}

		// Items found so walk the tree
		if ( ! empty( $r['posts'] ) ) {
			$retval .= walk_page_dropdown_tree( $r['posts'], 0, $r );
		}

		// Close the selecet tag
		if ( empty( $r['options_only'] ) ) {
			$retval .= '</select>';
		}

		// Filter & return
		return apply_filters( 'bbp_get_dropdown', $retval, $r, $args );
	}

Top ↑

Changelog Changelog

Changelog
Version Description
2.0.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

You must log in before being able to contribute a note or feedback.