<\/script>/i', // Match script tags without async '/<\/script>/i', // Match inline adsbygoogle.push scripts '/', // Replace non-async script tag '', // Replace inline push scripts with delayed versions '', ); // First pass: replace script tags $html = preg_replace($patterns[0], $replacements[0], $html); $html = preg_replace($patterns[1], $replacements[1], $html); // Second pass: replace inline push calls $html = preg_replace_callback( '/'; }, $html ); // Add a comment to indicate processing occurred if (defined('WP_DEBUG') && WP_DEBUG) { $html = str_replace('', '', $html); } return $html; } /** * Add inline script to initialize delayed AdSense * * This adds a small inline script that marks AdSense as ready to load * after the adsense-loader.js has been enqueued. */ function apus_add_adsense_init_script() { $delay_enabled = get_theme_mod('apus_adsense_delay_enabled', true); if (!$delay_enabled || is_admin()) { return; } ?> get_section('apus_performance')) { $wp_customize->add_section('apus_performance', array( 'title' => __('Performance Settings', 'apus-theme'), 'priority' => 130, )); } // Add setting for AdSense delay $wp_customize->add_setting('apus_adsense_delay_enabled', array( 'default' => true, 'sanitize_callback' => 'apus_sanitize_checkbox', 'transport' => 'refresh', )); // Add control for AdSense delay $wp_customize->add_control('apus_adsense_delay_enabled', array( 'label' => __('Delay AdSense Loading', 'apus-theme'), 'description' => __('Delay AdSense scripts until user interaction to improve page load speed.', 'apus-theme'), 'section' => 'apus_performance', 'type' => 'checkbox', )); } add_action('customize_register', 'apus_adsense_delay_customizer');