<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Filtro replace testo]]></title><description><![CDATA[<p dir="auto">Ottimo.</p>
]]></description><link>https://dev.connect.gt/topic/241381/filtro-replace-testo</link><generator>RSS for Node</generator><lastBuildDate>Fri, 27 Nov 2026 10:54:02 GMT</lastBuildDate><atom:link href="https://dev.connect.gt/topic/241381.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 14 May 2020 12:07:49 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Filtro replace testo on Thu, 14 May 2020 10:04:22 GMT]]></title><description><![CDATA[<p dir="auto">Non l'ho scritto prima però lo scrivo ora, prima di fare qualsiasi modifica sul database fai un backup e assicurati che sia funzionante, poi puoi fare tutte le modifiche che vuoi.</p>
]]></description><link>https://dev.connect.gt/post/1268791</link><guid isPermaLink="true">https://dev.connect.gt/post/1268791</guid><dc:creator><![CDATA[overclokk]]></dc:creator><pubDate>Thu, 14 May 2020 10:04:22 GMT</pubDate></item><item><title><![CDATA[Reply to Filtro replace testo on Thu, 14 May 2020 09:00:56 GMT]]></title><description><![CDATA[<p dir="auto">Appunto, non può sapere dove lo sostituirlo se non sei più esplicito, "lo voglio sostituire solo qui".</p>
<p dir="auto">Un array è semplicemente un contenitore con chiavi e valori:</p>
<pre><code class="language-php">array (
    0 =&gt; 'Valore',
    1 =&gt; 'Valore',
    2 =&gt; 'Valore',
    3 =&gt; 'Valore',
)
</code></pre>
<p dir="auto">Nell'array $data (che è un array associativo) sono contenute le informazioni del post:</p>
<pre><code class="language-php">$data[
   'post_content' = 'Lorem Ipsum',
   'post_title' = 'Titolo delpost',
   // ... ecc ecc
];
</code></pre>
<p dir="auto">Per avere solo e soltanto il contenuto dell'editor devi indicare la chiave che in questo caso è 'post_content', ovvero la posizione del valore che vuoi modificare:</p>
<pre><code class="language-php">$data['post_content'];
</code></pre>
<p dir="auto">Ora visto che la funzione str_replace accetta anche gli array quello che fa è cercare 'a href' in ogni settore dell'array quindi come puoi ben capire se tu per esempio hai 'a href' anche nel titolo te la sostituisce anche li, ora è una possibilità molto remota che tu inserisca 'a href' nel titolo però è importante da subito cercare di limitare il più possibile futuri bug che poi diventerebbero problematici da trovare proprio per il motivo detto sopra.</p>
<p dir="auto">Ad ogni modo se non ti ho convinto prova direttamente e vedi quello che succede.</p>
<p dir="auto">Qui c'è la spiegazione di cosa sono gli array <a href="https://www.php.net/manual/en/language.types.array.php" rel="nofollow ugc">https://www.php.net/manual/en/language.types.array.php</a></p>
]]></description><link>https://dev.connect.gt/post/1268790</link><guid isPermaLink="true">https://dev.connect.gt/post/1268790</guid><dc:creator><![CDATA[overclokk]]></dc:creator><pubDate>Thu, 14 May 2020 09:00:56 GMT</pubDate></item><item><title><![CDATA[Reply to Filtro replace testo on Thu, 14 May 2020 07:54:39 GMT]]></title><description><![CDATA[<p dir="auto">Funziona lo stesso perché la funzione str_replace ($search, $replace, $subject, &amp;$count = null) accetta sia stringe che array di stringhe nei suoi primi 3 parametri <a href="https://www.php.net/manual/en/function.str-replace.php" rel="nofollow ugc">https://www.php.net/manual/en/function.str-replace.php</a> e visto che $data è un array che contiene stringhe allora non da errore.</p>
<p dir="auto">Però nel tuo caso è meglio essere più espliciti nel codice per evitare incomprensioni inserendo solo la parte che deve essere modificata e non tutto l'array così eviti anche che se un giorno tu volessi usare 'a href' nel titolo questo non venga sostituito.</p>
<p dir="auto">Di seguito la tua funzione semplificata con solo quello che ti serve.</p>
<pre><code class="language-php">function filter_handler( array $data ) {
   $data['post_content'] = str_replace('a href', 'a class="links" href', $data['post_content']);
   return $data;
}
add_filter( 'wp_insert_post_data', 'filter_handler', '99' );
</code></pre>
]]></description><link>https://dev.connect.gt/post/1268788</link><guid isPermaLink="true">https://dev.connect.gt/post/1268788</guid><dc:creator><![CDATA[overclokk]]></dc:creator><pubDate>Thu, 14 May 2020 07:54:39 GMT</pubDate></item><item><title><![CDATA[Reply to Filtro replace testo on Wed, 13 May 2020 16:09:09 GMT]]></title><description><![CDATA[<p dir="auto">La variabile $data è un array e sopra ti avevo indicato come usarla per il contenuto:</p>
<pre><code class="language-php">$data['post_content']
</code></pre>
]]></description><link>https://dev.connect.gt/post/1268789</link><guid isPermaLink="true">https://dev.connect.gt/post/1268789</guid><dc:creator><![CDATA[overclokk]]></dc:creator><pubDate>Wed, 13 May 2020 16:09:09 GMT</pubDate></item><item><title><![CDATA[Reply to Filtro replace testo on Wed, 13 May 2020 15:35:29 GMT]]></title><description><![CDATA[<p dir="auto">Il mio ultimo post è proprio il codice che ti serve, devi solo adattarlo alle tue esigenze aggiungendo per str_replace che hai nel tuo codice, ho solo sostituito l'hook con uno più adatto allo scopo, tutto il resto è invariato.</p>
]]></description><link>https://dev.connect.gt/post/1268787</link><guid isPermaLink="true">https://dev.connect.gt/post/1268787</guid><dc:creator><![CDATA[overclokk]]></dc:creator><pubDate>Wed, 13 May 2020 15:35:29 GMT</pubDate></item><item><title><![CDATA[Reply to Filtro replace testo on Wed, 13 May 2020 14:56:33 GMT]]></title><description><![CDATA[<p dir="auto">Non funziona perché 'save_post' passa tre parametri di cui il primo è l'id del post, il secondo l'oggetto del post e il terzo un valore boleano che indica se stai aggiornando o salvando un nuovo contenuto, tu hai usato la tua funzione senza cambiare nulla e così non funzionerà mai, anzi, probabilmente nel log ci sono degli errori.</p>
<p dir="auto">Add ogni modo c'è un hook che viene eseguito prima di 'save_post' che è 'wp_insert_post_data' <a href="https://codex.wordpress.org/Plugin_API/Filter_Reference/wp_insert_post_data" rel="nofollow ugc">https://codex.wordpress.org/Plugin_API/Filter_Reference/wp_insert_post_data</a></p>
<p dir="auto">Se leggi bene la documentazione ti da uno snippet di esempio:</p>
<pre><code class="language-php">function filter_handler( $data , $postarr ) {
  // do something with the post data
  return $data;
}

add_filter( 'wp_insert_post_data', 'filter_handler', '99', 2 );
</code></pre>
<p dir="auto">La variabile $data è un array e per accedere al contenuto puoi usare la chiave 'post_content' in questo modo:</p>
<pre><code class="language-php">$data['post_content']
</code></pre>
<p dir="auto">In questo modo puoi cambiare il contenuto dell'articolo.</p>
]]></description><link>https://dev.connect.gt/post/1268786</link><guid isPermaLink="true">https://dev.connect.gt/post/1268786</guid><dc:creator><![CDATA[overclokk]]></dc:creator><pubDate>Wed, 13 May 2020 14:56:33 GMT</pubDate></item><item><title><![CDATA[Reply to Filtro replace testo on Wed, 13 May 2020 12:15:32 GMT]]></title><description><![CDATA[<p dir="auto">Hai visto la documentazione?</p>
<p dir="auto">Come e dove hai scritto il codice?</p>
]]></description><link>https://dev.connect.gt/post/1268785</link><guid isPermaLink="true">https://dev.connect.gt/post/1268785</guid><dc:creator><![CDATA[overclokk]]></dc:creator><pubDate>Wed, 13 May 2020 12:15:32 GMT</pubDate></item><item><title><![CDATA[Reply to Filtro replace testo on Wed, 13 May 2020 11:51:50 GMT]]></title><description><![CDATA[<p dir="auto">Cosa vuol dire che non funzionano del tutto?</p>
]]></description><link>https://dev.connect.gt/post/1268783</link><guid isPermaLink="true">https://dev.connect.gt/post/1268783</guid><dc:creator><![CDATA[overclokk]]></dc:creator><pubDate>Wed, 13 May 2020 11:51:50 GMT</pubDate></item><item><title><![CDATA[Reply to Filtro replace testo on Wed, 13 May 2020 11:15:04 GMT]]></title><description><![CDATA[<p dir="auto">Potresti usare 'save_post' <a href="https://developer.wordpress.org/reference/hooks/save_post/" rel="nofollow ugc">https://developer.wordpress.org/reference/hooks/save_post/</a> o ancora meglio "save_post_{$post-&gt;post_type}" <a href="https://developer.wordpress.org/reference/hooks/save_post_post-post_type/" rel="nofollow ugc">https://developer.wordpress.org/reference/hooks/save_post_post-post_type/</a></p>
]]></description><link>https://dev.connect.gt/post/1268782</link><guid isPermaLink="true">https://dev.connect.gt/post/1268782</guid><dc:creator><![CDATA[overclokk]]></dc:creator><pubDate>Wed, 13 May 2020 11:15:04 GMT</pubDate></item><item><title><![CDATA[Reply to Filtro replace testo on Wed, 13 May 2020 10:37:01 GMT]]></title><description><![CDATA[<blockquote>
<p dir="auto"><strong>Apthu</strong></p>
<pre><code>function replace_text($text) {    
    $text = str_replace('&lt;a href', '&lt;a class="links" href', $text);
    return $text;
}
add_filter('the_content', 'replace_text');
</code></pre>
</blockquote>
<p dir="auto">Questo snippet funziona solo lato front end del tuo sito perché usa l'hook 'the_content'.</p>
<p dir="auto">Se vuoi filtrare il testo al salvataggio allora l'hook da usare è differente.</p>
]]></description><link>https://dev.connect.gt/post/1268784</link><guid isPermaLink="true">https://dev.connect.gt/post/1268784</guid><dc:creator><![CDATA[overclokk]]></dc:creator><pubDate>Wed, 13 May 2020 10:37:01 GMT</pubDate></item><item><title><![CDATA[Reply to Filtro replace testo on Wed, 13 May 2020 09:40:32 GMT]]></title><description><![CDATA[<p dir="auto">Se vuoi migliorare il caricamento delle immagini usa uno dei tanti plugin che applicano il lazyload, cambiare l'url per renderla relativa invece che assoluta è pressoché ininfluente per le performance.</p>
<p dir="auto">Se invece vuoi farlo a scopo didattico allora ci possiamo lavorare su.</p>
]]></description><link>https://dev.connect.gt/post/1268781</link><guid isPermaLink="true">https://dev.connect.gt/post/1268781</guid><dc:creator><![CDATA[overclokk]]></dc:creator><pubDate>Wed, 13 May 2020 09:40:32 GMT</pubDate></item></channel></rss>