Skip to content

Instantly share code, notes, and snippets.

@bjallen
Created December 9, 2009 20:00
Show Gist options
  • Save bjallen/252742 to your computer and use it in GitHub Desktop.
Save bjallen/252742 to your computer and use it in GitHub Desktop.
<?php
/*
Plugin Name: Manage Tags Capability
Description: This plugin adds a bit more functionality to the manage_categories capability. If a user cannot manage categories, then they will see a custom box for post tags that lists all existing post tags for selection but does not allow the user to add new post tags.
Version: 1.0
Author: B.J. Allen, Leia Scofield
Author URI: http://rd2inc.com
Requires: WordPress Version 2.6 or above
*/
class manage_tags_capability {
public function __construct() {
if ( !current_user_can( 'manage_categories' ) ) {
add_action( 'do_meta_boxes', array( $this, 'remove_default_tag_box' ) );
add_action( 'admin_menu', array( $this, 'add_restricted_tag_box' ) );
add_action( 'save_post', array( $this, 'save_post_tags' ) );
}
}
public function remove_default_tag_box() {
if ( !function_exists( 'remove_meta_box' )) return;
foreach( array( 'normal', 'advanced', 'side' ) as $context ) {
remove_meta_box( 'tagsdiv-post_tag', 'post', $context );
}
}
public function add_restricted_tag_box() {
if ( !function_exists( 'add_meta_box' )) return;
add_meta_box( 'restricted_tag_box',
__( 'Tags', 'manage_tags_capability' ),
array( $this, 'print_restricted_tag_box' ),
'post',
'side'
);
}
public function print_restricted_tag_box( $post ) {
$all_post_tags = get_terms( 'post_tag', array( 'hide_empty' => 0 ) );
$assigned_tags = wp_get_object_terms( $post->ID, 'post_tag' );
$rd2_mtc_nonce = wp_create_nonce( plugin_basename(__FILE__) );
$assigned_ids = array();
foreach ($assigned_tags as $assigned_tag) {
$assigned_ids[] = $assigned_tag->term_id;
}
require_once( WP_PLUGIN_DIR . '/manage_tags_capability/views/meta_box.php' );
}
public function save_post_tags( $post_id ) {
// bail conditions
if ( 'post' != $_POST['post_type'] ) return $post_id;
if ( !wp_verify_nonce( $_POST['rd2_mtc_nonce'], plugin_basename(__FILE__) ) ) return $post_id;
if ( ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) ) return $post_id;
if ( !current_user_can( 'edit_post', $post_id ) ) return $post_id;
$selected_tags = ( is_array( $_POST['rd2_mtc_tags'] ) ) ? implode( ",", $_POST['rd2_mtc_tags'] ) : array();
wp_set_post_tags( $post_id, $selected_tags );
}
}
add_action( 'set_current_user', 'rd2_mtc_init' );
function rd2_mtc_init()
{
$manage_tags_capability = new manage_tags_capability();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment