Created
April 26, 2025 17:15
-
-
Save junaidte14/173f1edd2c10b3fadcefa18b91243f2b to your computer and use it in GitHub Desktop.
Managing custom tweaks for your WordPress site can be challenging — especially when switching themes or updating plugins. All your customizations risk being lost! To solve this, I created a simple, secure, and powerful plugin that lets you easily add your own custom CSS, JavaScript, and PHP code without touching your theme files.
This file contains hidden or 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
| jQuery(document).ready(function ($) { | |
| const codemirrorSettings = wp.codeEditor.defaultSettings || {}; | |
| const textareas = { | |
| 'css': 'textarea[name*="[custom_css]"]', | |
| 'js': 'textarea[name*="[custom_js]"]', | |
| 'php': 'textarea[name*="[custom_php]"]' | |
| }; | |
| for (const type in textareas) { | |
| const textarea = document.querySelector(textareas[type]); | |
| if (textarea) { | |
| wp.codeEditor.initialize(textarea, Object.assign({}, codemirrorSettings, { | |
| mode: type === 'php' ? 'application/x-httpd-php' : type, | |
| lineNumbers: true, | |
| indentUnit: 4, | |
| tabSize: 4 | |
| })); | |
| } | |
| } | |
| }); |
This file contains hidden or 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: Custom Site Tweaks | |
| * Description: Safely add custom CSS, JavaScript, and PHP (actions/filters) via admin panel without modifying your theme. | |
| * Version: 1.1.0 | |
| * Author: Junaid Hassan | |
| * Text Domain: custom-site-tweaks | |
| * Domain Path: /languages | |
| */ | |
| if ( ! defined( 'ABSPATH' ) ) { | |
| exit; | |
| } | |
| class Custom_Site_Tweaks { | |
| const VERSION = '1.1.0'; | |
| const OPTION_NAME = 'cst_code_options'; | |
| private static $instance; | |
| public static function get_instance() { | |
| if ( null === self::$instance ) { | |
| self::$instance = new self(); | |
| } | |
| return self::$instance; | |
| } | |
| private function __construct() { | |
| $this->define_constants(); | |
| $this->load_textdomain(); | |
| add_action( 'admin_menu', [ $this, 'add_settings_page' ] ); | |
| add_action( 'admin_init', [ $this, 'register_settings' ] ); | |
| add_action( 'wp_enqueue_scripts', [ $this, 'enqueue_custom_assets' ] ); | |
| add_action( 'init', [ $this, 'run_custom_php_code' ] ); | |
| add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_codemirror_assets' ] ); | |
| } | |
| private function define_constants() { | |
| define( 'CST_PLUGIN_DIR', plugin_dir_path( __FILE__ ) ); | |
| define( 'CST_PLUGIN_URL', plugin_dir_url( __FILE__ ) ); | |
| } | |
| public function load_textdomain() { | |
| load_plugin_textdomain( 'custom-site-tweaks', false, dirname( plugin_basename( __FILE__ ) ) . '/languages' ); | |
| } | |
| public function add_settings_page() { | |
| add_options_page( | |
| __( 'Custom Site Tweaks', 'custom-site-tweaks' ), | |
| __( 'Custom Tweaks', 'custom-site-tweaks' ), | |
| 'manage_options', | |
| 'custom-site-tweaks', | |
| [ $this, 'render_settings_page' ] | |
| ); | |
| } | |
| public function register_settings() { | |
| register_setting( 'cst_code_group', self::OPTION_NAME, [ | |
| 'sanitize_callback' => [ $this, 'sanitize_code_inputs' ], | |
| ] ); | |
| } | |
| public function sanitize_code_inputs( $input ) { | |
| return [ | |
| 'custom_css' => isset( $input['custom_css'] ) ? wp_kses_post( $input['custom_css'] ) : '', | |
| 'custom_js' => isset( $input['custom_js'] ) ? wp_kses_post( $input['custom_js'] ) : '', | |
| 'custom_php' => current_user_can( 'manage_options' ) && isset( $input['custom_php'] ) | |
| ? $input['custom_php'] | |
| : get_option( self::OPTION_NAME )['custom_php'], | |
| ]; | |
| } | |
| public function render_settings_page() { | |
| $options = get_option( self::OPTION_NAME ); | |
| $can_edit_php = current_user_can( 'manage_options' ); | |
| ?> | |
| <div class="wrap"> | |
| <h1><?php esc_html_e( 'Custom Site Tweaks', 'custom-site-tweaks' ); ?></h1> | |
| <form method="post" action="options.php"> | |
| <?php settings_fields( 'cst_code_group' ); ?> | |
| <?php $values = wp_parse_args( $options, [ | |
| 'custom_css' => '', | |
| 'custom_js' => '', | |
| 'custom_php' => '', | |
| ] ); ?> | |
| <h2><?php esc_html_e( 'Custom CSS', 'custom-site-tweaks' ); ?></h2> | |
| <textarea name="<?php echo esc_attr( self::OPTION_NAME ); ?>[custom_css]" rows="10" style="width: 100%;"><?php echo esc_textarea( $values['custom_css'] ); ?></textarea> | |
| <h2><?php esc_html_e( 'Custom JavaScript', 'custom-site-tweaks' ); ?></h2> | |
| <textarea name="<?php echo esc_attr( self::OPTION_NAME ); ?>[custom_js]" rows="10" style="width: 100%;"><?php echo esc_textarea( $values['custom_js'] ); ?></textarea> | |
| <h2><?php esc_html_e( 'Custom PHP (Hooks/Filters)', 'custom-site-tweaks' ); ?></h2> | |
| <?php if ( ! $can_edit_php ) : ?> | |
| <p><em><?php esc_html_e( 'You do not have permission to edit PHP code. Please contact the site administrator.', 'custom-site-tweaks' ); ?></em></p> | |
| <?php endif; ?> | |
| <textarea name="<?php echo esc_attr( self::OPTION_NAME ); ?>[custom_php]" rows="10" style="width: 100%;" <?php disabled( ! $can_edit_php ); ?>><?php echo esc_textarea( $values['custom_php'] ); ?></textarea> | |
| <?php submit_button(); ?> | |
| </form> | |
| </div> | |
| <?php | |
| } | |
| public function enqueue_custom_assets() { | |
| $opts = get_option( self::OPTION_NAME ); | |
| if ( ! empty( $opts['custom_css'] ) ) { | |
| wp_add_inline_style( 'wp-block-library', $opts['custom_css'] ); | |
| } | |
| if ( ! empty( $opts['custom_js'] ) ) { | |
| wp_add_inline_script( 'jquery-core', $opts['custom_js'] ); | |
| } | |
| } | |
| public function run_custom_php_code() { | |
| $opts = get_option( self::OPTION_NAME ); | |
| if ( ! empty( $opts['custom_php'] ) ) { | |
| try { | |
| eval( $opts['custom_php'] ); // ⚠️ Use with caution; only allow trusted admins. | |
| } catch ( Throwable $e ) { | |
| error_log( 'Custom PHP Error: ' . $e->getMessage() ); | |
| } | |
| } | |
| } | |
| public function enqueue_codemirror_assets( $hook ) { | |
| if ( $hook !== 'settings_page_custom-site-tweaks' ) { | |
| return; | |
| } | |
| wp_enqueue_code_editor( [ 'type' => 'text/css' ] ); | |
| wp_enqueue_code_editor( [ 'type' => 'text/javascript' ] ); | |
| wp_enqueue_code_editor( [ 'type' => 'application/x-httpd-php' ] ); | |
| wp_enqueue_script( 'cst-codemirror-init', plugin_dir_url( __FILE__ ) . 'codemirror-init.js', [ 'jquery', 'code-editor' ], self::VERSION, true ); | |
| } | |
| } | |
| Custom_Site_Tweaks::get_instance(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment