Skip to content

Instantly share code, notes, and snippets.

@roytanck
Last active January 25, 2023 06:45
Show Gist options
  • Save roytanck/e5e38d2955140b36da7e to your computer and use it in GitHub Desktop.
Save roytanck/e5e38d2955140b36da7e to your computer and use it in GitHub Desktop.
Auto-configure Autoptimize across a WordPress network
<?php
/**
* Plugin Name: RT Autoautoptimize
* Plugin URI: http://www.this-play.nl
* Description: Automatically configures default settings for the Autoptimize plugin across a WordPress network
* Version: 0.9
* Author: Roy Tanck
* Author URI: http://www.this-play.nl
* License: GPL2
*/
/**
* Auto-configure Autoptimize if not already configured
*/
if( !function_exists( 'rt_autoautoptimize' ) ){
function rt_autoautoptimize(){
// do nothing if we're in the admin
if( is_admin() ){
return;
}
// check if the plugin is already configured
$installed = get_option( 'autoptimize_configured', 'no' );
if( $installed == 'no' ){
// default Autoptimize settings for plugin version 1.9.4
$defaults = array(
'autoptimize_version' => '1.9.3',
'autoptimize_html' => 'on',
'autoptimize_html_keepcomments' => '',
'autoptimize_js' => 'on',
'autoptimize_js_exclude' => 's_sid,smowtion_size,sc_project,WAU_,wau_add,comment-form-quicktags,edToolbar,ch_client,seal.js',
'autoptimize_js_trycatch' => '',
'autoptimize_js_justhead' => '',
'autoptimize_js_forcehead' => '',
'autoptimize_css' => 'on',
'autoptimize_css_exclude' => 'admin-bar.min.css, dashicons.min.css',
'autoptimize_css_justhead' => '',
'autoptimize_css_datauris' => '',
'autoptimize_css_defer' => '',
'autoptimize_css_defer_inline' => '',
'autoptimize_css_inline' => '',
'autoptimize_cdn_url' => '',
'autoptimize_cache_clean' => '0',
'autoptimize_cache_nogzip' => 'on',
'autoptimize_show_adv' => '0',
'autoptimize_configured' => 'yes'
);
// loop through the defaults array, and update each option with the corresponding value
foreach( $defaults as $key=>$value ){
update_option( $key, $value );
}
// log this to the error log
error_log('autoautoptimize done');
}
}
// hook into init
add_action( 'init', 'rt_autoautoptimize' );
}
/**
* Apply the default configuration to newly created sites
*/
if( !function_exists('rt_autoautoptimize_newblog') ){
function rt_autoautoptimize_newblog( $blog_id, $user_id, $domain, $path, $site_id, $meta ){
if( !empty( $blog_id ) && function_exists('rt_autoautoptimize') ){
// apply the auto-configuration
rt_autoautoptimize();
}
}
add_action( 'wpmu_new_blog', 'rt_autoautoptimize_newblog', 10, 6 );
}
?>
@PlinthID
Copy link

Hi Roy, this script may be the solution that I was looking for. I am running Autoptimize on a multisite and want to apply the same settings across all sub sites. Do I simply upload this file as a plugin and activate or are there any particular instructions to implement it? Kind regards, Sean.

@jhnpldng
Copy link

I haven't installed it yet but it looks like the "array" options match up with the plugin's options/settings. False = 0, True = 1
Most everything else, you would fill in just like the plugin settings page. Comma separated value, on/off, 0/1, yes/no. You would have to edit arrays in the file above and then install it. Since there's no gui, this one would be good for the mu-plugins folder. (autoactivateautoautoptimize)

@kpirnie
Copy link

kpirnie commented Mar 12, 2019

How about for existing subsites? I've sound that looping through the sites, and applying the options does not work...

// we need this to update the options in all the sites to the default listed below
$_site_list = get_sites( );
				
// get the current site id
$_curr_blog_id = get_current_blog_id( );

// loop through all sites
foreach( $_site_list as $_site ) {

	// set the site id
	$_site_id = $_site -> blog_id;

	// switch to the proper blog
	switch_to_blog( intval( $_site_id ) );

	// create the cache folder if it doesnt exist already
	if( ! file_exists( ABSPATH . 'wp-content/cache/autoptimize/' . $_site_id ) ) {
		mkdir( ABSPATH . 'wp-content/cache/autoptimize/' . $_site_id, 0777, true );
	}

	// loop through the defaults array, and update each option with the corresponding value
	// $defaults here = your array
	foreach( $defaults as $key => $value ) {

		// update an existing option, or add a new one
		update_option( $key, $value );
	
	}

}

// back to the original blog
switch_to_blog( $_curr_blog_id );

@futtta
Copy link

futtta commented Sep 21, 2019

For all doing multisite + Autoptimize, this might be of interest; https://blog.futtta.be/2019/09/21/autoptimize-2-6-testers-for-multisite-wanted/ :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment