Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Add blog meta on WordPress multisite
<?php
class WeAreWP_Add_Blog_Order {
public static function init() {
$class = __CLASS__ ;
if ( empty( $GLOBALS[ $class ] ) )
$GLOBALS[ $class ] = new $class;
}
public function __construct() {
add_filter( 'wpmu_blogs_columns', array( $this, 'get_id' ) );
add_action( 'manage_sites_custom_column', array( $this, 'add_columns' ), 10, 2 );
add_action( 'manage_blogs_custom_column', array( $this, 'add_columns' ), 10, 2 );
add_action( 'admin_footer', array( $this, 'add_style' ) );
}
public function add_columns( $column_name, $blog_id ) {
if ( 'blog_id' === $column_name ){
echo $blog_id;
//render column value
}elseif( 'order' === $column_name ){
echo get_site_meta($blog_id, 'blog_order', true );
}
return $column_name;
}
// Add in a column header
public function get_id( $columns ) {
$columns['blog_id'] = __('ID');
//add extra header to table
$columns['order'] = __('Order');
return $columns;
}
public function add_style() {
echo '<style>#blog_id { width:7%; }</style>';
}
}
add_action( 'init', array( 'WeAreWP_Add_Blog_Order', 'init' ) );
function wearewp_custom_network_site_info_field() {
submit_button( 'Save Changes', 'primary', 'primary-save', true, "" );
echo "</form>"; // Close form in site-info.php
$blogid = isset( $_REQUEST['id'] ) ? (int) $_REQUEST['id'] : 0;
$meta_key = 'blog_order';
$saved_custom_field = get_site_meta( $blogid, $meta_key, true );
if ( isset( $_REQUEST['action'] ) && 'update-site-custom' === $_REQUEST['action'] ) {
if ( isset( $_POST[$meta_key] ) && isset( $saved_custom_field ) ) {
update_site_meta( $blogid, $meta_key, $_POST[$meta_key], false );
} else if ( isset( $_POST[$meta_key] ) ) {
add_site_meta( $blogid, $meta_key, $_POST[$meta_key], false );
}
$url = add_query_arg(
array(
'update' => 'updated',
'id' => $blogid,
),
'site-info.php'
);
echo("<script>location.href = '".$url."'</script>");
}
$blog_order = get_site_meta( $blogid, $meta_key, true );
?>
<hr><h3>Custom Fields</h3>
<form method="post" action="site-info.php?action=update-site-custom">
<table>
<tr>
<td>
<?php wp_nonce_field( 'edit-site' ); ?>
<input type="hidden" name="id" value="<?php echo esc_attr( $blogid ); ?>" />
<table class="form-table" role="presentation"><tr class="form-field">
<th scope="row"><label for="blog_order"><?php _e( 'Order' ); ?></label></th>
<td><input name="blog_order" type="text" id="blog_order" value="<?php echo esc_attr( $blog_order ); ?>"></td>
</tr></table>
</td>
<td><?php submit_button( 'Save Changes', 'secondary', 'custom-save', true, "" ); ?></td>
</tr>
</table>
<style>
.submit .button { display: none; }
#custom-save, #primary-save { display: block; }
</style>
<?php
}
add_action( 'network_site_info_form', 'wearewp_custom_network_site_info_field', 10, 0 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment