Code Snippet: Edit Oxygen Stylesheets Outside Builder




Oxygen is a powerful plugin for WordPress that deactivates the WordPress theme, supports the creation of own templates with a visual builder and thus enables a fully individual design for your website.

Within the Oxygen Builder you can define your own CSS to implement special layouts and formatting. This is very handy and comfortable.

However, since loading the Builder can take some time depending on the platform and browser, even just checking CSS code can be a tedious process.


I developed a code snippet that allows editing the Oxygen stylesheets outside of the Builder. This provides very quick access to the custom CSS without having to launch the Oxygen Builder first.

In the WordPress admin menu in the Oxygen section you will find a new entry "Edit Stylesheets".

For attention:
My code snippet prevents saving a stylesheet when an Oxygen Builder is open in another browser window (also by another user). Simultaneous changes would overwrite each other.
For this purpose my Code Snippet checks in intervals of a few seconds whether an Oxygen Builder has been opened in another tab or by another user. In this case a warning is issued and saving is prevented.
At the same time my Code Snippet checks whether the state of the currently displayed stylesheet still corresponds to the state in the database. If there are deviations, for example because changes were made in another browser window or by another user, a reload of the stylesheet is recommended.


The code snippet is available for download here:

Version 1.0.4, 2021-01-08

For installation and use of the downloaded JSON file you will need the plugin Code Snippets.
You can install the JSON file using the "Import" function of the plugin. 
Don't forget to activate the snippet after import.

If my previous snippet "Column 'Application' in Oxygen template list" is still installed, please delete it!

Alternative: At the end of this page you can view and copy the complete source code of the snippet.

New functionalities and bug fixes are documented in the change log.


I have developed and tested the code snippet to the best of my knowledge under WordPress 5.6 and Oxygen 3.6.1.
I provide the code snippet for free use.
I cannot give any guarantee for the functionality because of the countless possible variations in WordPress environments.
Download and use of this code snippet is at your own risk and responsibility.

Change Log

  • 2020-12-17
    • Start of development
  • Version 1.0.0 (2020-12-19)
    • Initial release
  • Version 1.0.1 (2020-12-30)
    • Bug fix: Fresh Oxygen site threw error since no stylesheets defined
  • Version 1.0.2 (2021-01-06)
    • Bug fix: Correct handling of quotes that get backslashed during transfer
    • Tweak: Implemented confirmation dialog for switching stylesheets when current one has been changed
  • Version 1.0.3 (2021-01-07)
    • Bug fix: Corrected changed flag logic
  • Version 1.0.4 (2021-01-08)
    • Tweak: Full height editor

Source Code

Plugin Name:  MA Oxygen Edit Stylesheets
Description:  Direct access to Oxygen Stylesheet Editor
Author:       <a href="">Matthias Altmann</a>
Version:      1.0.4
Plugin URI:
Description:  en:
Copyright:    © 2020, Matthias Altmann

Version History:
Date		Version		Description
2020-12-17				Development start
2020-12-19	1.0.0		Initial Release 
2020-12-30	1.0.1		Bug fix: Fresh Oxygen site threw error since no stylesheets defined
2021-01-06	1.0.2		Bug fix: Correct handling of quotes that get backslashed during transfer
						Tweak: Implemented confirmation dialog for switching stylesheets when current one has been changed
2020-01-07	1.0.3		Bug fix: Corrected changed flag logic 
2020-01-08	1.0.4		Tweak: Full height editor 

class MA_Oxygen_Edit_Stylesheets {

	const TITLE     	= 'MA Oxygen Edit Stylesheets';
	const SLUG	    	= 'ma_oxygen_edit_stylesheets';
	const VERSION   	= '1.0.4';
	// Configuration
	private static $debug				= false; // caution! may produce a lot of output
	private static $timing				= false; // caution! may produce a lot of output
	private static $status_interval 	= 5000; // interval (ms) to refresh  "builder open" and "stylesheet changed" status
	// Internal data
	private static $oxygen_universal_message = ''; // temp storage for succes/error message from oxygen

	static function init() {
		add_action('admin_menu', array( __CLASS__, 'admin_menu' ), 20 );
		add_action('wp_ajax_'.self::SLUG.'_status', [__CLASS__, 'status' ]);

	static function admin_menu() {
		// check Oxygen user access
		if (!oxygen_vsb_current_user_can_full_access()) {return;}

		// Add submenu page to the Oxygen menu.
		add_submenu_page(	'ct_dashboard_page', 						// parent slug of "Oxygen"
							_x('Edit Stylesheets','admin page title','ma_oxygen_edit_stylesheets'), 	// page title
							_x('Edit Stylesheets','admin menu title','ma_oxygen_edit_stylesheets'), 	// menu title
							'manage_options',							// capabilitiy
							self::SLUG.'_page',							// menu slug
							[__CLASS__,'edit_stylesheets']				// method
		if (WP_DEBUG && self::$debug) {error_log(sprintf('%s::%s() admin subpage added.',self::TITLE,__FUNCTION__));}
	// Ajax handler to check 1) Oxygen Builder active, 2) Stylesheets changed in database
	static function status() {
		$st = microtime(true);
		// get parameters
		$ssid = isset($_REQUEST['ssid']) ? $_REQUEST['ssid'] : null;
		$hash = isset($_REQUEST['hash']) ? $_REQUEST['hash'] : null;
		// prepare data object to be returned
		$retval = (object)[
			'ct_builder_active' 	=> false,
			'ct_stylesheet_changed'	=> false,
		// check if Oxygen Builder is active
		$retval->ct_builder_active = get_transient('oxygen_post_edit_lock');
		// if we currently have a stylesheet selected...
		if ($ssid && $hash) {
			// ... check if this stylesheet has changed
			$stylesheets = get_option('ct_style_sheets');
			$new_hash = null;
			// loop through stylesheets to find current one
			foreach ($stylesheets as $stylesheet) {
				if ($stylesheet['id']==$ssid) {
					$sscode = base64_decode($stylesheet['css']);
					$new_hash = sha1($sscode);
			$retval->ct_stylesheet_changed = ($hash !== $new_hash);

		if (WP_DEBUG && self::$debug) {error_log(sprintf('%s::%s() retval: %s',self::TITLE,__FUNCTION__,print_r($retval,true)));}
		$et = microtime(true);
		if (WP_DEBUG && self::$timing) {error_log(sprintf('%s::%s() Timing: %.5f sec.',self::TITLE,__FUNCTION__,$et-$st));}

		// return JSON encoded data object
		echo json_encode($retval);
	static function edit_stylesheets() {
		$st = microtime(true);

		if (WP_DEBUG && self::$debug) {error_log(sprintf('%s::%s() request: %s',self::TITLE,__FUNCTION__,print_r($_REQUEST,true)));}
		// check Oxygen user access
		if (!oxygen_vsb_current_user_can_full_access()) {return;}

		// check if action = save 
		if (isset($_REQUEST['action']) && ($_REQUEST['action'] == 'save-css') && isset($_REQUEST['ssid']) && isset($_REQUEST['code'])) {
			// save
			$ssid = $_REQUEST['ssid'];
			$code = $_REQUEST['code'];
			if (!isset($_REQUEST['save-css-nonce']) || !wp_verify_nonce($_REQUEST['save-css-nonce'], 'save-css-'.$ssid)) {
				echo '<div class="notice notice-error"><p>Invalid request (Nonce error)</p></div>';
			} else {
				// read current stylesheets
				$stylesheets = get_option('ct_style_sheets');
				foreach ($stylesheets as &$stylesheet) {
					if ($stylesheet['id']==$ssid) {
						// remove backslashes from quotes that might be added during transfer 
						$code = stripslashes($code);
						// override css code
						$stylesheet['css'] = base64_encode($code);
						// regenerate universal cache and show Oxygen's result message
						$result = oxygen_vsb_cache_universal_css();
						if ($result) 	{self::$oxygen_universal_message = __('Universal CSS cache generated successfully.','oxygen');}
						else 			{self::$oxygen_universal_message = __('Universal CSS cache not generated.','oxygen');}
					echo '<div id="notice-saved" class="notice notice-success is-dismissible"><p>Saved successfully. Oxygen '.self::$oxygen_universal_message.'</p></div>'.



		// stylesheet selection, current stylesheet code
		$ssid = @$_REQUEST['ssid'] ?? null;
		$ssdata = null;
		$sscode = '';
		$sshash = '';

		$stylesheets = get_option('ct_style_sheets');
		$stylesheets_select_options = sprintf('<optgroup label="%s">','Uncategorized');
		$stylesheets_tree = '<ul id="ss-tree"><li><span>Uncategorized</span><ul>';
		if ($stylesheets) {
			foreach ($stylesheets as $stylesheet) {
				if (array_key_exists('folder',$stylesheet)) {
					$stylesheets_select_options .= sprintf('</optgroup><optgroup label="%s">', $stylesheet['name']);
					$stylesheets_tree .= sprintf('</ul></li><li><span>%s</span><ul>', $stylesheet['name']);
				$stylesheets_select_options .= sprintf('<option value="%d" %s>%s</option>',$stylesheet['id'], $stylesheet['id']==$ssid?'selected':'', $stylesheet['name']);
				$stylesheets_tree .= sprintf('<li %s><a onclick="ma_oxygen_edit_stylesheets_switch(%d)">%s</a></li>',$stylesheet['id']==$ssid?'class="active"':'', $stylesheet['id'], $stylesheet['name']);
				if ($stylesheet['id']==$ssid) {
					$ssdata = $stylesheet;
					$sscode = base64_decode($stylesheet['css']);
					$sshash = sha1($sscode);
		$stylesheets_select_options .= '</optgroup>';
		$stylesheets_tree .= sprintf('</ul></li></ul>');
		var $can_change = false; //
		var $has_changed = false; // gets true if css code has been edited
		function sscode_changed() {
			$has_changed = true;
			//if ($can_change) {jQuery('#but-css-save').removeAttr('disabled');}

		function ma_oxygen_edit_stylesheets_switch($id) {
			if (!$has_changed || confirm('Stylesheet has been changed. Switch to another stylesheet anyways?')) {
				document.location.href='?page=<?php echo self::SLUG.'_page'; ?>&ssid='+$id;

		function ma_oxygen_edit_stylesheets_status() {
			var $ajax_data = {
				'action': '<?php echo self::SLUG.'_status'; ?>', // name of the AJAX function
				'ssid': '<?php echo $ssid; ?>',
				'hash': '<?php echo $sshash; ?>',
				url: ajaxurl, // this will point to admin-ajax.php
				type: 'POST',
				data: $ajax_data, 
				success: function ($response) {
					$response = JSON.parse($response);
					$notice = [];
					if ($response.ct_builder_active) {
						$can_change = false;
					} else {
						$can_change = true;
					if ($response.ct_stylesheet_changed) {
					} else {
			setTimeout(ma_oxygen_edit_stylesheets_status,<?php echo self::$status_interval; ?>);
		#wpbody-content * {box-sizing:border-box;}
		#wpbody-content {position:relative;height:calc(100vh - 32px);max-width:100%;padding-bottom:0;margin-right:20px;}
		#wpbody-content > .wrap {width:100%;height:100%;display:flex;flex-direction:column;margin:0;}
		#edit-screen {flex-grow:1;display:flex;flex-direction:row;}
		@media (max-width:900px) {#edit-screen {display:flex;flex-direction:column;}}
		.edit-screen__control {}
		.edit-screen__content {flex-grow:1;display:flex;flex-direction:column;}
		.edit-screen__content #code {display:none;}
		.edit-screen__title {font-size:18px;margin-bottom:10px;}
		#ss-tree {margin:0 20px 0 0;width:300px; border:1px solid darkgray;border-radius:3px;padding:5px;}
		#ss-tree li {margin:0 0margin-bottom:5px;}
		#ss-tree li span {display:block;padding:5px;font-weight:bold;background-color:darkgray;border-radius:3px;margin-bottom:5px;}
		#ss-tree ul {margin-left:20px;}
		#ss-tree ul li {}
		#ss-tree ul li a {display:inline-block;width:calc(100% - 10px);padding:5px;border-radius:3px;background-color:lightgray;color:black;cursor:pointer;}
		#ss-tree ul a {background-color:lightblue;color:black;}
		.ma_oxygen_edit_stylesheets_code {border: 1px solid lightgray;flex-grow:1;margin-bottom:20px;margin-right:20px;}
		.ma_oxygen_edit_stylesheets_code form {height:100%;display:flex;flex-direction:column;}
		.ma_oxygen_edit_stylesheets_code .CodeMirror {flex-grow:1}

		<div class="wrap">
			<h1><?php echo esc_html(get_admin_page_title()); ?></h1>
			<?php do_action( 'admin_notices' ); ?>
			<div id="notice-builder-open" class="notice notice-error" style="display:<?php echo get_transient('oxygen_post_edit_lock')?'block':'none'?>;">
				<p><?php _ex('Oxygen Builder is open in another browser tab. CSS changes not allowed.','builder open warning','ma_oxygen_edit_stylesheets'); ?></p>
			<div id="notice-stylesheet-changed" class="notice notice-warning" style="display:none">
				<p><?php _ex('This stylesheet has been changed in database. Please <a href="" onclick="location.reload()">reload</a>!','stylesheet changed warning','ma_oxygen_edit_stylesheets'); ?></p>
			<div id="edit-screen">
				<div class="edit-screen__control">
					<div class="edit-screen__title">Stylesheets:</div>
					<?php echo $stylesheets_tree; ?>
				<div class="edit-screen__content" <?php echo $ssid ? '':'style="display:none"';?>>
					<div class="edit-screen__title">Current Stylesheet: <?php echo (isset($ssdata['name']) ? $ssdata['name'] : ''); ?></div>
					<div class="ma_oxygen_edit_stylesheets_code">
						<form method="post">
							<input type="hidden" name="action" value="save-css"/>
							<input type="hidden" name="id" value="<?php echo $ssid ; ?>"/>
							<?php wp_nonce_field('save-css-'.$ssid,'save-css-nonce'); ?>
							<textarea id="code" name="code"><?php echo $sscode; ?></textarea>
							<p style="text-align:center;"><button id="but-css-save" type="submit" class="button button-primary" disabled >Save</button></p>


		$editor = wp_enqueue_code_editor(['type'=>'text/css']);

		wp_add_inline_script('code-editor', sprintf(
			'jQuery( function( $ ) {
				if ( $( "#code" ).length ) {
					var editor = wp.codeEditor.initialize( "code", %s );
					editor.codemirror.on("change",function(codemirror) {sscode_changed();});
			} );',
			wp_json_encode( $editor )
		) );
		$et = microtime(true);
		if (WP_DEBUG && self::$timing) {error_log(sprintf('%s::%s() Timing: %.5f sec.',self::TITLE,__FUNCTION__,$et-$st));}