public
Last active

Original code for Multisite Categories. At WordPress Answers http://wordpress.stackexchange.com/a/50936/12615

  • Download Gist
plugin.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
<?php
 
/**
* Plugin Name: Multisite Site Category
* Plugin URI: https://gist.github.com/brasofilo/6715423
* Description: Add a custom meta option when registering new sites in WordPress Multisite. Two columns will be added to the Sites listing screen: ID and Category. Based on http://wordpress.stackexchange.com/a/50936/12615
* Network: true
* Author: Rodolfo Buaiz
* Author URI: http://brasofilo.com/
* Version: 1.1
* Stable Tag: 1.1
* License: GPLv2 or later
*
* This program is free software; you can redistribute it and/or modify it under the terms of the GNU
* General Public License version 2, as published by the Free Software Foundation. You may NOT assume
* that you can use any other version of the GPL.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
* even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*/
 
 
// BUSTED!
!defined( 'ABSPATH' ) AND exit(
"<pre>Hi there! I'm just part of a plugin, <h1>&iquest;what exactly are you looking for?"
);
 
// PREPARE
if( !class_exists( 'BL_Multisite_Categories' ) ):
 
class BL_Multisite_Categories
{
/**
* Plugin instance.
*
* @see get_instance()
* @type object
*/
protected static $instance = NULL;
 
/**
* URL to this plugin's directory.
*
* @type string
*/
public $plugin_url = '';
 
/**
* Path to this plugin's directory.
*
* @type string
*/
public $plugin_path = '';
 
/**
* Access this plugin’s working instance
*
* @wp-hook plugins_loaded
* @since 2012.09.13
* @return object of this class
*/
public static function get_instance()
{
NULL === self::$instance and self::$instance = new self;
 
return self::$instance;
}
 
 
/**
* Activation hook
*
* Checks if option exist, otherwise fill it up
*
* @global type $wpdb
*/
public function on_activation()
{
if( !is_multisite() )
wp_die( 'Cannot install in Single Site. Multisite only!' );
 
// Get the Sites
global $wpdb;
$blogs = $wpdb->get_results(
"SELECT blog_id
FROM {$wpdb->blogs}
WHERE site_id = '{$wpdb->siteid}'"
);
 
// Create the Category
foreach( $blogs as $blog )
{
switch_to_blog( $blog->blog_id );
$have_category = get_blog_option( $blog->blog_id, 'site_category' );
if( !$have_category )
update_blog_option( $blog->blog_id, 'site_category', 'Uncategorized' );
}
restore_current_blog();
}
 
 
/**
* Used for regular plugin work.
*
* @wp-hook plugins_loaded
* @since 2012.09.10
* @return void
*/
public function plugin_setup()
{
if( !is_admin() )
add_action(
'signup_blogform', array( $this, 'signup_blogform_extra_field' )
);
 
if( !is_network_admin() )
return;
 
$this->plugin_url = plugins_url( '/', __FILE__ );
$this->plugin_path = plugin_dir_path( __FILE__ );
 
add_action(
'admin_print_scripts-site-new.php', array( $this, 'admin_scripts' )
);
add_action(
'admin_footer-sites.php', array( $this, 'add_style' )
);
add_action(
'manage_blogs_custom_column', array( $this, 'add_columns' ), 10, 2
);
add_action(
'manage_sites_custom_column', array( $this, 'add_columns' ), 10, 2
);
add_action(
'wpmu_new_blog', array( $this, 'add_new_blog_field' )
);
add_filter(
'add_signup_meta', array( $this, 'append_extra_field_as_meta' )
);
add_filter(
'wpmu_blogs_columns', array( $this, 'print_columns' )
);
}
 
 
/**
* Constructor. Intentionally left empty and public.
*
* @see plugin_setup()
* @since 2012.09.12
*/
public function __construct()
{
}
 
 
/**
* Add new option when registering a site (back and front end)
*
* URI: http://stackoverflow.com/a/10372861/1287812
*/
public function add_new_blog_field( $blog_id, $user_id, $domain, $path, $site_id, $meta )
{
$new_field_value = 'default';
 
// Site added in the back end
if( !empty( $_POST['blog']['site_category'] ) )
{
switch_to_blog( $blog_id );
$new_field_value = $_POST['blog']['site_category'];
update_option( 'site_category', $new_field_value );
 
restore_current_blog();
}
// Site added in the front end
elseif( !empty( $meta['site_category'] ) )
{
$new_field_value = $meta['site_category'];
update_option( 'site_category', $new_field_value );
}
}
 
 
/**
* Add new field in /wp-admin/network/site-new.php
* has to be done with jQuery
*
* URI: http://stackoverflow.com/a/10372861/1287812
*/
public function admin_scripts()
{
wp_register_script( 'b_msc', $this->plugin_url . 'js/script.js' );
wp_enqueue_script( 'b_msc' );
}
 
 
/**
* Add new field in site signup form /wp-signup.php
*
* URI: http://wordpress.stackexchange.com/a/50550/12615
*/
public function signup_blogform_extra_field()
{
$txt = __( 'Category' );
echo "
<label>{$txt}</label>
<input type='text' name='site_category' value='' />
";
}
 
 
/**
* Append the submitted value of our custom input
* into the meta array that is stored while the user doesn't activate
*
* URI: http://wordpress.stackexchange.com/a/50550/12615
*/
public function append_extra_field_as_meta( $meta )
{
if( isset( $_REQUEST['site_category'] ) )
$meta['site_category'] = $_REQUEST['site_category'];
 
return $meta;
}
 
 
/**
* Add custom columns (ID and Site Category) in Sites listing
*
*/
public function add_columns( $column_name, $blog_id )
{
if( 'blog_id' === $column_name )
echo $blog_id;
 
elseif( 'site_category' === $column_name )
{
$sitecat = get_blog_option( $blog_id, 'site_category' );
echo $sitecat;
}
 
return $column_name;
}
 
 
/**
* Add Columns
*
*/
public function print_columns( $cols )
{
$in = array( "blog_id" => "ID" );
$cols = $this->arrayPushAfter( $cols, $in, 0 );
$cols['site_category'] = __( 'Category' );
return $cols;
}
 
 
/**
* Add column widths
*
*/
public function add_style()
{
echo '<style>#blog_id { width:7%; } #site_category { width:20%; }</style>';
}
 
 
/**
* Insert $in item in position $pos inside the $src array
*
*/
private function arrayPushAfter( $src, $in, $pos )
{
if( is_int( $pos ) )
$R = array_merge( array_slice( $src, 0, $pos + 1 ), $in, array_slice( $src, $pos + 1 ) );
else
{
foreach( $src as $k => $v )
{
$R[$k] = $v;
if( $k == $pos )
$R = array_merge( $R, $in );
}
}
return $R;
}
 
 
}
 
endif;
 
 
// ACTION!
if( function_exists( 'add_action' ) )
{
// Initial plugin hooks
register_activation_hook(
__FILE__, array( 'BL_Multisite_Categories', 'on_activation' )
);
 
 
add_action(
'plugins_loaded', array( BL_Multisite_Categories::get_instance(), 'plugin_setup' )
);
}
script.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
* Add new field in /wp-admin/network/site-new.php
* URI: http://stackoverflow.com/a/10372861/1287812
*/
 
(function($) {
$(document).ready(function() {
$('<tr class="form-field form-required"></tr>').append(
$('<th scope="row">Site category</th>')
).append(
$('<td></td>').append(
$('<input class="regular-text" type="text" title="Site Category" name="blog[site_category]">')
).append(
$('<p></p>')
)
).insertAfter('#wpbody-content table tr:eq(2)');
 
 
});
})(jQuery);

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.