Skip to content

Commit f808018

Browse files
committed
Initial commit: WP Console Mute (MU) v1.0.0
1 parent 7ff7582 commit f808018

File tree

4 files changed

+359
-3
lines changed

4 files changed

+359
-3
lines changed

.gitignore

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,8 @@ wp-config.php
4545
#
4646
# Note: If you wish to whitelist themes,
4747
# uncomment the next line
48-
#/wp-content/themes
48+
#/wp-content/themes
49+
50+
51+
52+
TODO*

.vscode/settings.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"php.stubs": [
3+
"pcntl",
4+
"wordpress",
5+
"*"
6+
]
7+
}

README.md

Lines changed: 150 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,150 @@
1-
# wp-console-mute
2-
Lightweight WordPress utility to silence unwanted JavaScript console output.
1+
# 🧩 WP Console Mute (MU Plugin Edition)
2+
3+
> **MU-plugin minimale per WordPress** che disattiva in modo selettivo o totale i messaggi della console JavaScript, mantenendo il frontend e la dashboard puliti in produzione.
4+
5+
---
6+
7+
## 🚀 Funzionalità principali
8+
9+
**Modalità selettiva (default)**
10+
Silenzia solo i warning della **Google Maps JavaScript API**, senza toccare gli altri log utili per il debug.
11+
12+
**Modalità totale (full mute)**
13+
Disabilita completamente tutti i metodi `console.*` (`log`, `warn`, `error`, `info`, ecc.) anche negli iframe presenti o creati dinamicamente.
14+
15+
**Sicuro e moderno**
16+
17+
- Nessuna modifica a `document.createElement`
18+
- Usa `MutationObserver` per intercettare iframe futuri
19+
- Compatibile con `WP_DEBUG`
20+
- 100% inline-JS senza dipendenze esterne
21+
22+
**Progettato per ambienti CodeCorn™**
23+
Perfettamente integrabile in stack moderni e child-theme professionali.
24+
25+
---
26+
27+
## ⚙️ Installazione
28+
29+
### 🔹 Metodo 1 — Come MU-plugin (consigliato)
30+
31+
1. Clona o scarica la repo:
32+
```bash
33+
git clone https://github.com/codecorntech/wp-console-mute.git
34+
```
35+
36+
````
37+
38+
2. Copia il file principale direttamente nella directory MU-plugins di WordPress:
39+
40+
```
41+
wp-content/mu-plugins/mu-cc-console-mute.php
42+
```
43+
44+
3. WordPress caricherà automaticamente lo script su **tutti i siti** senza necessità di attivazione da Bacheca.
45+
46+
---
47+
48+
### 🔹 Metodo 2 — Da `functions.php`
49+
50+
Puoi anche copiare le funzioni direttamente nel tuo tema child:
51+
52+
```php
53+
require_once get_stylesheet_directory() . '/inc/mu-cc-console-mute.php';
54+
```
55+
56+
---
57+
58+
## 🧩 Utilizzo
59+
60+
### 🔸 Modalità predefinita — solo Google Maps warnings
61+
62+
Il comportamento di default silenzia i warning della Google Maps API:
63+
64+
```php
65+
add_action( 'wp_footer', 'cc_console_mute_gmaps_warnings', PHP_INT_MAX );
66+
add_action( 'admin_footer', 'cc_console_mute_gmaps_warnings', PHP_INT_MAX );
67+
```
68+
69+
### 🔸 Modalità avanzata — zittisci tutta la console
70+
71+
Per disabilitare completamente tutti i log:
72+
73+
```php
74+
// add_action( 'wp_footer', 'cc_console_mute_all', PHP_INT_MAX );
75+
// add_action( 'admin_footer', 'cc_console_mute_all', PHP_INT_MAX );
76+
```
77+
78+
> 💡 Decommenta solo in ambienti di produzione, non in sviluppo.
79+
80+
---
81+
82+
## 🎛️ Personalizzazione
83+
84+
Aggiungi pattern personalizzati per silenziare altri warning:
85+
86+
```php
87+
add_filter( 'cc_console_mute_patterns', function( $patterns ) {
88+
$patterns[] = 'Some noisy library';
89+
$patterns[] = 'Deprecated feature';
90+
return $patterns;
91+
});
92+
```
93+
94+
---
95+
96+
## 🧠 Hooks disponibili
97+
98+
| Hook | Descrizione |
99+
| -------------------------- | ---------------------------------------------------------------------------------- |
100+
| `cc_console_mute_patterns` | Array di stringhe da confrontare con i messaggi `console.warn` (case-insensitive). |
101+
102+
---
103+
104+
## 🔒 Sicurezza
105+
106+
* Non altera in modo invasivo l’ambiente JS globale
107+
* Non disattiva log lato PHP o server
108+
* Opera solo dopo il caricamento di tutti gli script in footer
109+
110+
---
111+
112+
## 📦 Struttura del progetto
113+
114+
```bash
115+
wp-console-mute/
116+
117+
├── mu-cc-console-mute.php # MU-plugin principale
118+
├── README.md # Questo file
119+
├── LICENSE # Licenza MIT
120+
└── .gitignore # File Git standard
121+
```
122+
123+
---
124+
125+
## 🧰 Compatibilità
126+
127+
* WordPress 5.8+
128+
* PHP 7.4 – 8.3
129+
* Gutenberg & Dashboard moderna
130+
* Testato su Hello Elementor, Astra, GeneratePress
131+
132+
---
133+
134+
## 🧑‍💻 Autore
135+
136+
**CodeCorn™ Technology**
137+
Soluzioni DevOps, Plugin WordPress e Infrastructure Tools.
138+
📍 Roma, Italia — [www.codecorn.it](https://www.codecorn.it)
139+
140+
---
141+
142+
## ⚖️ Licenza
143+
144+
Rilasciato sotto **MIT License** — libero uso e modifica con attribuzione.
145+
146+
---
147+
148+
> 💬 *“Silenziare il rumore, per vedere meglio ciò che conta.”*
149+
> — CodeCorn™ Dev Team
150+
````

mu-cc-console-mute.php

Lines changed: 197 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,197 @@
1+
<?php
2+
/**
3+
* Plugin Name: WP Console Mute (MU)
4+
* Plugin URI: https://github.com/codecorntech/wp-console-mute
5+
* Description: Disattiva in modo selettivo o totale i messaggi della console JavaScript in WordPress (anche negli iframe). Ideale per ambienti di produzione.
6+
* Version: 1.0.0
7+
* Author: CodeCorn™ Technology
8+
* Author URI: https://www.codecorn.it
9+
* License: MIT
10+
* License URI: https://opensource.org/licenses/MIT
11+
* Text Domain: wp-console-mute
12+
* Domain Path: /languages
13+
*
14+
* @package CodeCorn\WPConsoleMute
15+
* @since 1.0.0
16+
*
17+
* ============================================================
18+
* MU-PLUGIN NOTICE:
19+
* Questo file può essere copiato in:
20+
* → /wp-content/mu-plugins/mu-cc-console-mute.php
21+
* Verrà caricato automaticamente da WordPress, senza attivazione manuale.
22+
* ============================================================
23+
*/
24+
25+
/**
26+
* Stampa JS che silenzia SOLO i warning della Google Maps JavaScript API.
27+
*
28+
* Usa console.warn "wrapping": inoltra tutto tranne i messaggi che combaciano
29+
* uno dei pattern configurabili via filtro `cc_console_mute_patterns`.
30+
*
31+
* Hook consigliati: wp_footer, admin_footer
32+
*
33+
* @since 1.0.0
34+
* @return void
35+
*/
36+
function cc_console_mute_gmaps_warnings()
37+
{
38+
$disable_in_dev = false; // se vuoi sempre silenziare, metti false
39+
40+
// In sviluppo lascia passare i log (facoltativo: commenta se vuoi sempre silenziare).
41+
if (defined('WP_DEBUG') && WP_DEBUG) {
42+
error_log("[WP Console Mute] MU-plugin caricato correttamente. \$disable_in_dev: $disable_in_dev");
43+
if ($disable_in_dev)
44+
return;
45+
}
46+
47+
/**
48+
* Permette di estendere i pattern da silenziare.
49+
*
50+
* @since 1.0.0
51+
* @param string[] $patterns Elenco di pattern (case-insensitive) cercati in args[0] se stringa.
52+
*/
53+
$patterns = apply_filters(
54+
'cc_console_mute_patterns',
55+
[
56+
'Google Maps JavaScript API', // warning classici della Maps JS API
57+
'Google Maps JavaScript API has been loaded',
58+
]
59+
);
60+
61+
// Prepara array JS in modo sicuro.
62+
$json_patterns = wp_json_encode(array_values(array_filter(array_map('strval', $patterns))));
63+
if (!$json_patterns) {
64+
$json_patterns = '[]';
65+
}
66+
67+
$js = <<<JS
68+
(function(){
69+
try {
70+
var patterns = {$json_patterns};
71+
var originalWarn = console.warn ? console.warn.bind(console) : function(){};
72+
73+
console.warn = function() {
74+
try {
75+
var first = arguments[0];
76+
if (typeof first === 'string' && patterns.some(function(p){ return first.toLowerCase().indexOf(String(p).toLowerCase()) !== -1; })) {
77+
return; // ignora i warning che combaciano
78+
}
79+
} catch(e) {}
80+
return originalWarn.apply(console, arguments);
81+
};
82+
} catch(e) {}
83+
})();
84+
JS;
85+
86+
echo "<script>{$js}</script>";
87+
}
88+
89+
/**
90+
* Stampa JS che disabilita TUTTI i metodi console (anche negli iframe, presenti e futuri).
91+
*
92+
* Metodi toccati: log, warn, error, debug, info, trace, table, group, groupCollapsed, groupEnd, dir
93+
*
94+
* Hook consigliati: wp_footer, admin_footer
95+
*
96+
* @since 1.0.0
97+
* @return void
98+
*/
99+
function cc_console_mute_all()
100+
{
101+
// Frena in sviluppo (facoltativo): evita di perdere log utili in dev.
102+
if (defined('WP_DEBUG') && WP_DEBUG) {
103+
return;
104+
}
105+
106+
$methods = ['log', 'warn', 'error', 'debug', 'info', 'trace', 'table', 'group', 'groupCollapsed', 'groupEnd', 'dir'];
107+
$json_methods = wp_json_encode($methods);
108+
if (!$json_methods) {
109+
$json_methods = '[]';
110+
}
111+
112+
$js = <<<JS
113+
(function(){
114+
try {
115+
var noop = function(){};
116+
var methods = {$json_methods};
117+
118+
function patchConsole(win){
119+
if(!win) return;
120+
try {
121+
if (win.console) {
122+
methods.forEach(function(m){
123+
try { win.console[m] = noop; } catch(e){}
124+
});
125+
}
126+
} catch(e){}
127+
}
128+
129+
// Patch finestra corrente
130+
patchConsole(window);
131+
132+
// Patch iframe ESISTENTI (già in pagina)
133+
try {
134+
var iframes = document.getElementsByTagName('iframe');
135+
for (var i=0; i<iframes.length; i++){
136+
var f = iframes[i];
137+
try { patchConsole(f.contentWindow); } catch(e){}
138+
// in caso l'iframe carichi dopo
139+
f.addEventListener('load', function(ev){
140+
try { patchConsole(ev.target && ev.target.contentWindow); } catch(e){}
141+
});
142+
}
143+
} catch(e){}
144+
145+
// Patch iframe FUTURI via MutationObserver
146+
try {
147+
var obs = new MutationObserver(function(mutations){
148+
mutations.forEach(function(m){
149+
if (m.type === 'childList' && m.addedNodes && m.addedNodes.length){
150+
m.addedNodes.forEach(function(node){
151+
if (node && node.tagName && node.tagName.toLowerCase() === 'iframe'){
152+
try { patchConsole(node.contentWindow); } catch(e){}
153+
node.addEventListener('load', function(ev){
154+
try { patchConsole(ev.target && ev.target.contentWindow); } catch(e){}
155+
});
156+
}
157+
// se vengono aggiunti container con iframe annidati
158+
if (node && node.querySelectorAll) {
159+
node.querySelectorAll('iframe').forEach(function(f){
160+
try { patchConsole(f.contentWindow); } catch(e){}
161+
f.addEventListener('load', function(ev){
162+
try { patchConsole(ev.target && ev.target.contentWindow); } catch(e){}
163+
});
164+
});
165+
}
166+
});
167+
}
168+
});
169+
});
170+
obs.observe(document.documentElement || document.body, {childList:true, subtree:true});
171+
} catch(e){}
172+
} catch(e){}
173+
})();
174+
JS;
175+
176+
echo "<script>{$js}</script>";
177+
}
178+
179+
/**
180+
* HOOKS
181+
* Abilita UNO dei due blocchi seguenti:
182+
*/
183+
184+
// 1) Silenzia SOLO i warning di Google Maps (ATTIVO di default)
185+
add_action('wp_footer', 'cc_console_mute_gmaps_warnings', PHP_INT_MAX);
186+
add_action('admin_footer', 'cc_console_mute_gmaps_warnings', PHP_INT_MAX);
187+
188+
// 2) Zittisce TUTTO (sconsigliato in dev) — abilita se/quando necessario
189+
// add_action( 'wp_footer', 'cc_console_mute_all', PHP_INT_MAX );
190+
// add_action( 'admin_footer', 'cc_console_mute_all', PHP_INT_MAX );
191+
192+
// 3) ### Note rapide
193+
// /** Se vuoi aggiungere altri messaggi da ignorare nel filtro “gmaps”, puoi usare: */
194+
// add_filter('cc_console_mute_patterns', function($p){
195+
// $p[] = 'Some noisy library';
196+
// return $p;
197+
// });

0 commit comments

Comments
 (0)