0
Staff would like to know if you have how to force the user to mark at least one custom taxonomy in Wordpress? Ex: In a post I have custom taxonomies, I would like to oblige the user when entering the post to mark at least one category.
0
Staff would like to know if you have how to force the user to mark at least one custom taxonomy in Wordpress? Ex: In a post I have custom taxonomies, I would like to oblige the user when entering the post to mark at least one category.
1
This is a classic in WPSE: Force Category Choice before Creating new post?
It is necessary to adapt the post_type
, add custom taxonomy in wp_dropdown_categories
and use the function wp_set_object_terms
to update this taxonomy.
I adapted using portfolio
as Post Type and location
as Custom Taxonomy. When we click on Add new, appears first a screen asking for the selection of a category, when selecting and submitting the new post is created with the custom category selected. The consideration is that this is only necessary when creating a new post, if it is to control the selections of existing posts it is necessary to inject jQuery for this.
# Adapted from https://wordpress.stackexchange.com/questions/14403/force-category-choice-before-creating-new-post
add_filter( 'load-post-new.php', 'wpse14403_load_post_new' );
function wpse14403_load_post_new()
{
$post_type = 'portfolio';
if ( isset( $_REQUEST['post_type'] ) ) {
$post_type = $_REQUEST['post_type'];
}
else
return; // Se o post_type for 'post', o REQUEST vem vazio, precisa disto para não disparar nos posts
// Only do this for posts
if ( 'portfolio' != $post_type ) {
return;
}
if ( array_key_exists( 'category_id', $_REQUEST ) ) {
add_action( 'wp_insert_post', 'wpse14403_wp_insert_post' );
return;
}
// Show intermediate screen
extract( $GLOBALS );
$post_type_object = get_post_type_object( $post_type );
$title = $post_type_object->labels->add_new_item;
include( ABSPATH . 'wp-admin/admin-header.php' );
$dropdown = wp_dropdown_categories( array(
'name' => 'category_id',
'hide_empty' => false,
'taxonomy' => 'location',
'echo' => false,
) );
$category_label = __( 'Category:' );
$continue_label = __( 'Continue' );
echo <<<HTML
<div class="wrap">
<h2>{$title}</h2>
<form method="get">
<table class="form-table">
<tbody>
<tr valign="top">
<th scope="row">{$category_label}</th>
<td>{$dropdown}</td>
</tr>
<tr>
<td></td>
<th><input name="continue" type="submit" class="button-primary" value="{$continue_label}" /></th>
</tbody>
</table>
<input type="hidden" name="post_type" value="{$post_type}" />
</form>
</div>
HTML;
include( ABSPATH . 'wp-admin/admin-footer.php' );
exit();
}
// This function will only be called when creating an empty post,
// via `get_default_post_to_edit()`, called in post-new.php
function wpse14403_wp_insert_post( $post_id )
{
wp_set_object_terms( $post_id, intval($_REQUEST['category_id']), 'location' );
}
Browser other questions tagged wordpress
You are not signed in. Login or sign up in order to post.
Matthew, fix a bug in the code, are only two lines.
– brasofilo