WordPress.org

Make WordPress Core

Opened 4 years ago

Closed 4 years ago

#17954 closed enhancement (wontfix)

add function wp_dropdown_posttypes();

Reported by: wpsmith Owned by: Travis Smith
Milestone: Priority: normal
Severity: normal Version: 3.2
Component: Posts, Post Types Keywords: close
Focuses: Cc:

Description

This function creates a dropdown select box, for all post types with the option to exclude custom post types. Features currently include ability to show option none, add an additional option (e.g., ALL), include builtin post types (defaults to public builtins, e.g. post,page,attachment), exclude custom post types, and echo and/or return.

<?php
/**
 * This function creates a dropdown select box,
 * for all custom post types with the option to include
 * pages and posts (but not the other builtins).
 *
 * Version: 1.0
 * Author: Travis Smith
 * URI: http://wpsmith.net
 **/
 
function wp_dropdown_posttypes($args) {
	$defaults = array(
		'pt_args'		=> array (
				'public'   => true,
				'_builtin' => false
			),
		'output'	=> 'objects', // names or objects, note names is the default
		'operator'	=> 'and', // 'and' or 'or'
		'selected' => 0,
		'echo' => 1,
		'show_option_none' => '',
		'show_option_no_change' => '',
		'option_none_value' => '',
		'name' => 'post_type',
		'id' => '',
		'include_builtins' => array('page','post','attachment'), //also mediapage, revision (not public), nav_menu_item (not public)
		'excludes' => '', //array of cpts to exclude
		'add_option' => ''
	); 
	
	$r = wp_parse_args( $args, $defaults );
	extract( $r, EXTR_SKIP );

	$post_types = get_post_types( $pt_args , $output , $operator ); 
	$output = '';
	
	$name = esc_attr($name);
	//print_r($args);
	if ( ! empty($post_types) ) {
		$output = "<select name=\"$name\" id=\"$name\" >\n";
		if ( $show_option_no_change )
			$output .= "<option value=\"-1\">$show_option_no_change</option>";
		if ( $show_option_none )
			$output .= "<option value=\"" . esc_attr($option_none_value) . "\">$show_option_none</option>\n";
		if ( $include_builtins ) {
			foreach ($include_builtins  as $builtin ) {
				$obj = get_post_type_object($builtin);
				if($selected == $obj->name) {	$sel = ' selected = "selected"'; } else { $sel = ''; }
				$output .= '<option class="post-type" value="'.$obj->name.'"'.$sel.'>'.$obj->label.'</option>';
			}
		}
		foreach ($post_types  as $post_type ) {
			if ($excludes) {
				$skip = false;
				foreach ($excludes  as $exclude ) {
					if ($exclude == $post_type->name) {
						$skip = true;
						break;
					}
				}
				if ($skip) break;
			}
			//print_r($post_type);
			if($selected == $post_type->name) {	$sel = ' selected = "selected"'; } else { $sel = ''; }
			$output .= '<option class="post-type" value="'.$post_type->name.'"'.$sel.'>'. $post_type->label. '</option>';
		}
		if ( $add_option ) {
			if($selected == $add_option['value']) {	$sel = ' selected = "selected"'; } else { $sel = ''; }
			$output .= "<option value='".$add_option['value']."'".$sel.">".$add_option['name']."</option>\n";
		}
		$output .= "</select>\n";
	}
	
	$output = apply_filters('wp_dropdown_posttypes', $output);

	//if ( $echo )
		echo $output;

	return $output;
}
?>

Attachments (1)

wp_dropdown_posttypes.php (2.5 KB) - added by wpsmith 4 years ago.
php file for wp_dropdown_posttypes

Download all attachments as: .zip

Change History (5)

comment:1 @wpsmith4 years ago

My apologies: replace

//if ( $echo )

with

if ( $echo )

See file.

comment:2 in reply to: ↑ description @wpsmith4 years ago

Correct code:

<?php
/**
 * This function creates a dropdown select box,
 * for all custom post types with the option to include
 * pages and posts (but not the other builtins).
 *
 * Version: 1.0
 * Author: Travis Smith
 * URI: http://wpsmith.net
 **/
 
function wp_dropdown_posttypes($args) {
	$defaults = array(
		'pt_args'		=> array (
				'public'   => true,
				'_builtin' => false
			),
		'output'	=> 'objects', // names or objects
		'operator'	=> 'and', // 'and' or 'or'
		'selected' => 0,
		'echo' => 1,
		'show_option_none' => '',
		'show_option_no_change' => '',
		'option_none_value' => '',
		'name' => 'post_type',
		'id' => '',
		'include_builtins' => array('page','post','attachment'), //also revision (not public), nav_menu_item (not public)
		'excludes' => '', //array of cpts to exclude
		'add_option' => ''
	); 
	
	$r = wp_parse_args( $args, $defaults );
	extract( $r, EXTR_SKIP );

	$post_types = get_post_types( $pt_args , $output , $operator ); 
	$output = '';
	
	$name = esc_attr($name);
	
	if ( ! empty($post_types) ) {
		$output = "<select name=\"$name\" id=\"$name\" >\n";
		if ( $show_option_no_change )
			$output .= "<option value=\"-1\">$show_option_no_change</option>";
		if ( $show_option_none )
			$output .= "<option value=\"" . esc_attr($option_none_value) . "\">$show_option_none</option>\n";
		if ( $include_builtins ) {
			foreach ($include_builtins  as $builtin ) {
				$obj = get_post_type_object($builtin);
				if($selected == $obj->name) {	$sel = ' selected = "selected"'; } else { $sel = ''; }
				$output .= '<option class="post-type" value="'.$obj->name.'"'.$sel.'>'.$obj->label.'</option>';
			}
		}
		foreach ($post_types  as $post_type ) {
			if ($excludes) {
				$skip = false;
				foreach ($excludes  as $exclude ) {
					if ($exclude == $post_type->name) {
						$skip = true;
						break;
					}
				}
				if ($skip) break;
			}
			
			if($selected == $post_type->name) {	$sel = ' selected = "selected"'; } else { $sel = ''; }
			$output .= '<option class="post-type" value="'.$post_type->name.'"'.$sel.'>'. $post_type->label. '</option>';
		}
		if ( $add_option ) {
			if($selected == $add_option['value']) {	$sel = ' selected = "selected"'; } else { $sel = ''; }
			$output .= "<option value='".$add_option['value']."'".$sel.">".$add_option['name']."</option>\n";
		}
		$output .= "</select>\n";
	}
	
	$output = apply_filters('wp_dropdown_posttypes', $output);

	if ( $echo == 1 )
		echo $output;

	return $output;
}
?>

@wpsmith4 years ago

php file for wp_dropdown_posttypes

comment:3 @nacin4 years ago

  • Keywords close added; dev-feedback 2nd-opinion needs-testing removed

Until core is going to use this, I don't see why we need this.

For one, it tries to do too much. You'd be better off just doing it on a case-by-case basis. It's a pretty simple piece of code when you get down to it:

<select name="post_type">
<?php
$_selected = 'post';
$pts = get_post_types();
foreach ( get_post_types( array(), 'objects' ) as $pt ) :
?>
    <option value="<?php echo $pt->name; ?>"
        <?php selected( $pt->name, $_selected ); ?>>
        <?php echo $pt->labels->name; ?>
    </option>
<?php
endforeach;
?>
</select>

Two, it never will be used, because post types are distinct objects that are not to be mixed in the user interface.

P.S. your gravatar is a fauxgo: http://wordpress.org/about/logos/.

comment:4 @scribu4 years ago

  • Milestone Awaiting Review deleted
  • Resolution set to wontfix
  • Status changed from new to closed

Agree that this doesn't need to be in Core.

Note: See TracTickets for help on using tickets.