Custom WordPress Network Site Info Field

Here’s a plugin that will allow you to add a custom field to your network site info page on a WordPress Multisite installation. I’ll talk you through how it works and you can find a download link at end.

How it works

The hook to insert content into this page is called network_site_info_form and is positioned before the closing tags of the form.

However, we will be adding our field as custom site meta-data (which is stored in a different place), so we will need to start off by closing the site-info form and starting a new one, optionally inserting a submit button at that point.

submit_button( 'Save Changes', 'primary', 'primary-save', true, "" ); 
echo "</form>"; // Close form in site-info.php

Next we use the three functions get_site_meta, add_site_meta, and update_site_meta to manage our new value, which we’ll call blog_custom_field. We check if there’s already a saved value, check if there is any newly submitted value (from our new form below)…

$saved_custom_field = get_site_meta( $blogid, $meta_key, true );
$posted_custom_field = $_POST[$meta_key];

… and add or update as appropriate.

if ( isset( $posted_custom_field ) && isset( $saved_custom_field ) ) {
    update_site_meta( $blogid, $meta_key, $posted_custom_field, false );
} else if ( isset( $_POST[$meta_key] ) ) {
    add_site_meta( $blogid, $meta_key, $posted_custom_field, false );
}

Then we refresh the page. We have to use JavaScript rather than wp_safe_redirect() because site-info.php has already output headers and content onto the page.

$url =	add_query_arg(
    array(
	'update' => 'updated',
	'id'     => $blogid,
    ),
    'site-info.php'
);
echo("<script>location.href = '".$url."'</script>");

Next we add our custom form, which will trigger an action that we’ll call update-site-custom. First we can wrap the above code for managing saving the new value in an if statement that checks to see if the form has been submitted:

if ( isset( $_REQUEST['action'] ) && 'update-site-custom' === $_REQUEST['action'] ) {
    // ...
}

Then we’ll add the form itself with its own submit button.

<form method="post" action="site-info.php?action=update-site-custom">
    <?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_custom_field"><?php _e( 'Custom Label' ); ?></label></th>
            <td><input name="blog_custom_field" type="text" id="blog_custom_field" value="<?php echo esc_attr( $blog_custom_field ); ?>"></td>
        </tr>
</table>
<?php submit_button( 'Save Changes', 'secondary', 'custom-save', true, "" ); ?>

Finally, for the sake of tidiness, we’ll hide the submit button inserted by site-info.php.

<style>
 .submit .button { display: none; }
 #custom-save, #primary-save { display: block; }
</style>

Putting it all together

The complete function, including the necessary hook, looks like this:

Download

You can download or fork this plugin on Github.

Leave a Reply