Am Einfachsten wäre es, jenen Teil des Posttag-Divs auszublenden in dem neue Tags angelegt werden. Allerdings ist die nachfolgende Auswahl auf die häufigsten Tags beschränkt. Ein Autor hätte ggf. also nicht Zugang zu allen Tags.
Der Trick bestand darin, die Posttag-Metabox für Autoren zu entfernen, und eine neue Box zu bauen, deren Feldnamen dem Original entsprach und Tags über Checkboxen zur Auswahl anbietet. Im Gegensatz zu den Metaboxen mit hierarchischen Taxonomien werden bei Tags die Namen gespeichert, nicht die IDs.
function flxo_taxonomy() {
return 'post_tag';
}
function flxo_post_type() {
return 'post';
}
/*
* Remove Tags Metabox
*/
function flxo_tags_meta_box_remove() {
if ( current_user_can('edit_pages') ) return;
$id = 'tagsdiv-' . flxo_taxonomy(); // you can find it in a page source code (Ctrl+U)
$post_type = flxo_post_type(); // remove only from post edit screen
$position = 'side';
remove_meta_box( $id, $post_type, $position );
}
add_action( 'admin_menu', 'flxo_tags_meta_box_remove');
/*
* Add new Tags Metabox
*/
function flxo_add_new_tags_metabox(){
if ( current_user_can('edit_pages') ) return;
$id = 'tagsdiv-' . flxo_taxonomy(); // it should be unique
$heading = sprintf(__( '%1$s Tags', 'ppp-posttags-select-only' ), ucwords( flxo_post_type() )); // meta box heading
$callback = 'flxo_tags_metabox_content'; // the name of the callback function
$post_type = flxo_post_type();
$position = 'side';
$pri = 'default'; // priority, 'default' is good for us
add_meta_box( $id, $heading, $callback, $post_type, $position, $pri );
}
add_action( 'admin_menu', 'flxo_add_new_tags_metabox');
/*
* New Tags Metabox Content
*/
function flxo_tags_metabox_content($post) {
// get all blog post tags as an array of objects
$all_tags = get_terms( array('taxonomy' => flxo_taxonomy(), 'hide_empty' => 0) );
// get all tags assigned to a post
$checked_tags = get_the_terms( $post->ID, flxo_taxonomy() );
// create an array of post tags ids
$ids = array();
if ( $checked_tags ) {
foreach ($checked_tags as $tag ) {
$ids[] = $tag->term_id;
}
}
// HTML
echo '<div id="taxonomy-'.flxo_taxonomy().'" class="categorydiv">';
echo '<input type="hidden" name="tax_input['.flxo_taxonomy().'][]" value="0" />';
echo '<ul>';
$tax = flxo_taxonomy();
foreach( $all_tags as $tag ){
$checked = in_array( $tag->term_id, $ids ) ? " checked='checked'" : "";
$id = flxo_post_type() . '_tag-' . $tag->term_id;
echo "<li id='{$id}'>";
echo "<label><input type='checkbox' name='tax_input[$tax][]' id='in-$id'". $checked ." value='$tag->slug' /> $tag->name</label><br />";
echo "</li>";
}
echo '</ul></div>'; // end HTML
}
Schreibe einen Kommentar