Skip to content

Instantly share code, notes, and snippets.

@gbyat
Last active June 2, 2016 10:13
Show Gist options
  • Save gbyat/ac1def815c599372ed605ce4f9157223 to your computer and use it in GitHub Desktop.
Save gbyat/ac1def815c599372ed605ce4f9157223 to your computer and use it in GitHub Desktop.
WordPress Plugin: Authors may only assign existing post_tags not add new
<?php
/*
* Plugin Name: PPP Posttags Select Only
* Plugin URI: http://gby.at
* Description: Authors may only assign existing post_tags not add new
* Author: Gabriele Laesser
* Author URI: http://www.xisign.com
* License: GPLv2 or later
* License URI: http://www.gnu.org/licenses/gpl-2.0.html
* Version: 1.0
* Text Domain: ppp-posttags-select-only
* Domain Path: /languages
*/
function pppf_post_tags_select_init() {
load_plugin_textdomain( 'ppp-posttags-select-only', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
}
add_action('plugins_loaded', 'pppf_post_tags_select_init');
function pppf_remove_tagsdiv() {
if ( current_user_can('edit_pages') ) return;
remove_meta_box( 'tagsdiv-post_tag', 'post', 'side' );
}
add_action( 'admin_menu', 'pppf_remove_tagsdiv' );
/**
* Retrieve the custom metabox post_tags array
* must be the slugs otherwise wordpress will set new post_tags from ids
*
*/
function pppf_get_the_tags() {
$terms = get_terms( array(
'taxonomy' => 'post_tag',
'hide_empty' => false,
) );
if ( !$terms ) return array();
foreach ( $terms as $term ) {
$post_tags[$term->slug] = $term->name;
}
return $post_tags;
}
/**
* Retrieve all post_tags set in post-ID
* must be the slugs
*
*/
function pppf_get_post_tags( $post_id ) {
$tags = wp_get_post_tags( $post_id );
if ( !count( $tags ) ) {
return array();
}
foreach ( $tags as $tag ) {
$post_tags[] = $tag->slug;
}
return $post_tags;
}
/**
* the metabox to select post_tags for a post
*
*/
function pppf_custom_tag_meta_box( $post, $box ) {
if ( current_user_can('edit_pages') ) return;
$post_tags = pppf_get_the_tags();
$selected_tags = pppf_get_post_tags( $post->ID );
?>
<div id="custom-post_tags-select">
<fieldset>
<legend class="screen-reader-text"><?php esc_html_e( 'Tags', 'ppp-posttags-select-only' ); ?></legend>
<?php foreach ( $post_tags as $tag => $label ) : ?>
<br /><input type="checkbox" name="tax_input[post_tag][]" id="post_tag-<?php echo esc_attr( $tag ); ?>" value="<?php echo esc_attr( $tag ); ?>" <?php echo ( in_array( $tag, $selected_tags ) ) ? 'checked="checked"' : ''; ?> /> <label for="post_tag-<?php echo esc_attr( $tag ); ?>"><?php echo esc_html( $label ); ?></label>
<?php endforeach; ?>
</fieldset>
</div>
<?php
}
/**
* register the metabox with WordPress
*
*/
function pppf_register_custom_tag_metabox() {
if ( current_user_can('edit_pages') ) return;
add_meta_box( 'posttag-custom', _x( 'Tags', 'ppp-posttags-select-only' ), 'pppf_custom_tag_meta_box', null, 'side', '' );
}
add_action( 'add_meta_boxes', 'pppf_register_custom_tag_metabox' );
function pppf_save_custom_tag( $post_id ) {
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return $post_id;
if ( current_user_can( 'edit_pages', $post_id ) ) return $post_id;
if ( !current_user_can( 'edit_post', $post_id ) ) return $post_id;
if ( !isset( $_POST['tax_input']['post_tag'] ) )
wp_delete_object_term_relationships( $post_id, 'post_tag' );
}
add_action( 'save_post', 'pppf_save_custom_tag' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment