Skip to content

Instantly share code, notes, and snippets.

@samikeijonen
Last active May 18, 2022 00:12
Show Gist options
  • Save samikeijonen/63d1bd862adc69786936 to your computer and use it in GitHub Desktop.
Save samikeijonen/63d1bd862adc69786936 to your computer and use it in GitHub Desktop.
Example plugin for using multisite user meta
<?php
/**
* Plugin Name: Multisite User Meta
* Plugin URI: https://foxland.fi/user-meta-in-wordpress-multisite/
* Description: Test multisite user meta with text value.
* Version: 1.0.0
* Author: Sami Keijonen
* Author URI: https://foxland.fi/
* Text Domain: multisite-user-meta
* Domain Path: /languages
*
* 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.
*
* @package Multisite User Meta
* @author Sami Keijonen <sami.keijonen@foxnet.fi>
* @copyright Copyright (c) Sami Keijonen
* @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
*/
// Exit if accessed directly
if( !defined( 'ABSPATH' ) ) {
exit;
}
/**
* Adds setting in user profile.
*
* @since 1.0.0
* @return void
*/
function multisite_user_meta_field( $user ) {
// Get setting.
$setting = multisite_user_meta_get_setting( $user->ID );
?>
<h3><?php esc_html_e( 'Multisite setting', 'multisite-user-meta' ); ?></h3>
<table class="form-table">
<tbody>
<tr>
<th scope="row"><label for="multisite_user_meta_setting"><?php esc_html_e( 'Test setting', 'multisite-user-meta' ); ?></label></th>
<td>
<input type="text" name="multisite_user_meta_setting" id="multisite_user_meta_setting" value="<?php if ( ! empty( $setting ) ) esc_attr_e( $setting ); ?>" class="multisite-user-meta medium-text multisite-user-meta-setting" />
<p class="description"><?php esc_html_e( 'Set example setting.', 'multisite-user-meta' ); ?></p>
</td>
</tr>
<tbody>
</table>
<?php }
add_action( 'show_user_profile', 'multisite_user_meta_field' );
add_action( 'edit_user_profile', 'multisite_user_meta_field' );
/**
* Save setting in user profile.
*
* @since 1.0.0
* @return void
*/
function multisite_user_meta_save_field( $user_id ) {
// Bail if we don't have permission to edit user.
if ( ! current_user_can( 'edit_user', $user_id ) ) {
return false;
}
// Get setting.
$setting = isset( $_POST['multisite_user_meta_setting'] ) ? sanitize_text_field( $_POST['multisite_user_meta_setting'] ) : null;
// Add user meta setting.
update_user_option( $user_id, 'multisite_user_meta_setting', $setting );
}
add_action( 'personal_options_update', 'multisite_user_meta_save_field' );
add_action( 'edit_user_profile_update', 'multisite_user_meta_save_field' );
/**
* Get user setting.
*
* @since 1.0.0
* @return void
*/
function multisite_user_meta_get_setting( $user_id = 0 ) {
// Get current user id if it's not set from function call
if ( empty( $user_id ) && is_user_logged_in() ) {
$user_id = get_current_user_id();
}
// Get setting
$setting = get_user_option( 'multisite_user_meta_setting', $user_id );
return $setting;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment