Last active
September 5, 2024 01:48
-
-
Save westonruter/69cfa4538861ec815904 to your computer and use it in GitHub Desktop.
Customize Readonly WordPress Plugin: Only let superadmins make changes in the Customizer. All other admins can just preview changes. For them, the save UI is removed/disabled, and the Ajax request to save settings is blocked. The customize_readonly filter can be used to fine-tune which users are read-only.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*global wp, jQuery */ | |
wp.customize.ReadOnly = ( function ( $, api ) { | |
var self; | |
self = {}; | |
self.init = function () { | |
// Hide the save button | |
$( '#save' ).hide(); | |
// Clean the saved state whenever it is dirty | |
api.state( 'saved' ).bind( function( saved ) { | |
if ( ! saved ) { | |
this( true ); | |
} | |
} ); | |
}; | |
api.bind( 'ready', function () { | |
self.init(); | |
} ); | |
return self; | |
}( jQuery, wp.customize ) ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Plugin Name: Customize Readonly | |
* Description: Only let superadmins make changes in the Customizer. All other admins can just preview changes. For them, the save UI is removed/disabled, and the Ajax request to save settings is blocked. The <code>customize_readonly</code> filter can be used to fine-tune which users are read-only. | |
* Author: Weston Ruter, X-Team WP | |
* Version: 0.1 | |
* Author URI: http://x-team.com/wordpress/ | |
* License: GPLv2+ | |
*/ | |
/** | |
* Copyright (c) 2014 X-Team (http://x-team.com/) | |
* | |
* This program is free software; you can redistribute it and/or modify | |
* it under the terms of the GNU General Public License, version 2 or, at | |
* your discretion, any later version, as published by the Free | |
* Software Foundation. | |
* | |
* 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. See the | |
* GNU General Public License for more details. | |
* | |
* You should have received a copy of the GNU General Public License | |
* along with this program; if not, write to the Free Software | |
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
*/ | |
class Customize_Readonly { | |
function __construct() { | |
add_action( 'customize_register', array( $this, 'init' ) ); | |
} | |
/** | |
* Setup readonly mode on customize_register. | |
*/ | |
function init() { | |
if ( ! $this->is_user_readonly() ) { | |
return; | |
} | |
add_action( 'customize_save', array( $this, 'block_customize_save' ), 1 ); | |
add_action( 'customize_controls_print_scripts', array( $this, 'enqueue_scripts' ) ); | |
} | |
/** | |
* Register/enqueue customizer scripts. | |
*/ | |
function enqueue_scripts() { | |
wp_enqueue_script( 'customize-readonly', plugin_dir_url( __FILE__ ) . '/customize-readonly.js', array( 'jquery', 'customize-controls' ) ); | |
} | |
/** | |
* Prevent customizer settings from being saved. | |
* | |
* customize_save action happens before any settings are saved. This is | |
* invoked during wp_ajax_customize_save | |
*/ | |
function block_customize_save() { | |
wp_send_json_error( 'readonly' ); | |
} | |
/** | |
* Users can be made readonly to customize via the customize_readonly filter. | |
* | |
* @return bool | |
*/ | |
function is_user_readonly() { | |
return apply_filters( 'customize_readonly', ! is_super_admin(), wp_get_current_user() ); | |
} | |
} | |
$customize_readonly = new Customize_Readonly(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment