Media Library Categories - Version 1.3

Version Description

  • Add support for bulk actions (to change category from multiple media items at once)
  • support for WordPress 3.8
Download this release

Release Info

Developer jeffrey-wp
Plugin Icon 128x128 Media Library Categories
Version 1.3
Comparing to
See all releases

Code changes from version 1.2 to 1.3

Files changed (2) hide show
  1. index.php +90 -1
  2. readme.txt +12 -3
index.php CHANGED
@@ -3,7 +3,7 @@
3
  * Plugin Name: Media Library Categories
4
  * Plugin URI: http://wordpress.org/plugins/wp-media-library-categories/
5
  * Description: Adds the ability to use categories in the media library.
6
- * Version: 1.2
7
  * Author: Jeffrey-WP
8
  */
9
 
@@ -13,6 +13,7 @@ function wpmediacategory_init() {
13
  }
14
  add_action( 'init', 'wpmediacategory_init' );
15
 
 
16
  /** Add a category filter */
17
  function wpmediacategory_add_category_filter() {
18
  $screen = get_current_screen();
@@ -23,6 +24,94 @@ function wpmediacategory_add_category_filter() {
23
  }
24
  add_action( 'restrict_manage_posts', 'wpmediacategory_add_category_filter' );
25
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26
  /** Add a link to media categories on the plugin page */
27
  function wpmediacategory_add_plugin_action_links( $links ) {
28
  return array_merge(
3
  * Plugin Name: Media Library Categories
4
  * Plugin URI: http://wordpress.org/plugins/wp-media-library-categories/
5
  * Description: Adds the ability to use categories in the media library.
6
+ * Version: 1.3
7
  * Author: Jeffrey-WP
8
  */
9
 
13
  }
14
  add_action( 'init', 'wpmediacategory_init' );
15
 
16
+
17
  /** Add a category filter */
18
  function wpmediacategory_add_category_filter() {
19
  $screen = get_current_screen();
24
  }
25
  add_action( 'restrict_manage_posts', 'wpmediacategory_add_category_filter' );
26
 
27
+
28
+ /** Add custom Bulk Action to the select menus */
29
+ function wpmediacategory_custom_bulk_admin_footer() {
30
+ //$terms = get_the_terms( 0, 'category' );
31
+ $terms = get_terms( 'category', 'hide_empty=0' );
32
+ if ( $terms && ! is_wp_error( $terms ) ) :
33
+ ?>
34
+ <script type="text/javascript">
35
+ jQuery(document).ready(function() {
36
+ <?php
37
+ foreach ( $terms as $term ) {
38
+ echo 'jQuery(\'<option>\').val(\'cat_' . $term->term_taxonomy_id . '\').text(\''. __( 'Category' ) . ': ' . $term->name . '\').appendTo("select[name=\'action\']");';
39
+ echo 'jQuery(\'<option>\').val(\'cat_' . $term->term_taxonomy_id . '\').text(\''. __( 'Category' ) . ': ' . $term->name . '\').appendTo("select[name=\'action2\']");';
40
+ }
41
+ ?>
42
+ jQuery('<option>').val('cat_0').text('<?php _e( 'Category' ); ?> <?php echo strtolower(__( 'Remove' )); ?>').appendTo("select[name='action']");
43
+ jQuery('<option>').val('cat_0').text('<?php _e( 'Category' ); ?> <?php echo strtolower(__( 'Remove' )); ?>').appendTo("select[name='action2']");
44
+ });
45
+ </script>
46
+ <?php
47
+ endif;
48
+ }
49
+ add_action( 'admin_footer-upload.php', 'wpmediacategory_custom_bulk_admin_footer' );
50
+
51
+ /** Handle the custom Bulk Action */
52
+ function wpmediacategory_custom_bulk_action() {
53
+ global $wpdb;
54
+
55
+ if ( ! isset( $_REQUEST['action'] ) )
56
+ return;
57
+
58
+ // is it a category?
59
+ $sAction = ($_REQUEST['action'] != -1) ? $_REQUEST['action'] : $_REQUEST['action2'];
60
+ if ( substr( $sAction, 0, 4 ) != 'cat_' )
61
+ return;
62
+
63
+ // security check
64
+ check_admin_referer('bulk-media');
65
+
66
+ // make sure ids are submitted. depending on the resource type, this may be 'media' or 'post'
67
+ if(isset($_REQUEST['media'])) {
68
+ $post_ids = array_map('intval', $_REQUEST['media']);
69
+ }
70
+
71
+ if(empty($post_ids)) return;
72
+
73
+ $sendback = admin_url( "upload.php?editCategory=1" );
74
+
75
+ //$pagenum = $wp_list_table->get_pagenum();
76
+ //$sendback = add_query_arg( 'paged', $pagenum, $sendback );
77
+
78
+ $newCategory = str_replace('cat_', '', $sAction);
79
+
80
+ foreach( $post_ids as $post_id ) {
81
+ if ($newCategory == 0) {
82
+ // remove category
83
+ $wpdb->delete( $wpdb->term_relationships, array( 'object_id' => $post_id ) );
84
+ } else {
85
+ // update or insert category
86
+ $wpdb->replace( $wpdb->term_relationships,
87
+ array(
88
+ 'object_id' => $post_id,
89
+ 'term_taxonomy_id' => $newCategory
90
+ ),
91
+ array(
92
+ '%d',
93
+ '%d'
94
+ )
95
+ );
96
+ }
97
+ }
98
+
99
+ wp_redirect( $sendback );
100
+ exit();
101
+ }
102
+ add_action( 'load-upload.php', 'wpmediacategory_custom_bulk_action' );
103
+
104
+ /** Display an admin notice on the page after changing category */
105
+ function wpmediacategory_custom_bulk_admin_notices() {
106
+ global $post_type, $pagenow;
107
+
108
+ if($pagenow == 'upload.php' && $post_type == 'attachment' && isset($_GET['editCategory'])) {
109
+ echo '<div class="updated"><p>' . __('Settings saved.') . '</p></div>';
110
+ }
111
+ }
112
+ add_action( 'admin_notices', 'wpmediacategory_custom_bulk_admin_notices' );
113
+
114
+
115
  /** Add a link to media categories on the plugin page */
116
  function wpmediacategory_add_plugin_action_links( $links ) {
117
  return array_merge(
readme.txt CHANGED
@@ -2,8 +2,8 @@
2
  Contributors: jeffrey-wp
3
  Tags: category, categories, media, library, medialibrary
4
  Requires at least: 3.1
5
- Tested up to: 3.7.1
6
- Stable tag: 1.2
7
  License: GPLv2 or later
8
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
9
 
@@ -35,14 +35,23 @@ To upload the plugin through WordPress, instead of FTP:
35
  1. Upload the downloaded zip file on the 'Add New' plugins screen (see the 'Upload' tab) in your WordPress admin area and activate.
36
  2. A dropdown of categories will show up in the media library
37
 
 
 
 
 
 
 
38
  == Screenshots ==
39
 
40
  1. Filter by category in the media library
41
  2. Manage categories in the media library
42
 
43
-
44
  == Changelog ==
45
 
 
 
 
 
46
  = 1.2 =
47
  * Better internationalisation
48
 
2
  Contributors: jeffrey-wp
3
  Tags: category, categories, media, library, medialibrary
4
  Requires at least: 3.1
5
+ Tested up to: 3.8
6
+ Stable tag: 1.3
7
  License: GPLv2 or later
8
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
9
 
35
  1. Upload the downloaded zip file on the 'Add New' plugins screen (see the 'Upload' tab) in your WordPress admin area and activate.
36
  2. A dropdown of categories will show up in the media library
37
 
38
+ == Frequently Asked Questions ==
39
+
40
+ = Where can I request new features? =
41
+
42
+ You can request new features on the [support page](http://wordpress.org/support/plugin/wp-media-library-categories)
43
+
44
  == Screenshots ==
45
 
46
  1. Filter by category in the media library
47
  2. Manage categories in the media library
48
 
 
49
  == Changelog ==
50
 
51
+ = 1.3 =
52
+ * Add support for bulk actions (to change category from multiple media items at once)
53
+ * support for WordPress 3.8
54
+
55
  = 1.2 =
56
  * Better internationalisation
57