Trusted WordPress tutorials, when you need them most.
Beginner’s Guide to WordPress
Coppa WPB
25 Million+
Websites using our plugins
16+
Years of WordPress experience
3000+
WordPress tutorials
by experts

Come disabilitare l’HTML nei commenti di WordPress

Nota editoriale: guadagniamo una commissione dai link dei partner su WPBeginner. Le commissioni non influenzano le opinioni o le valutazioni dei nostri redattori. Per saperne di più su Processo editoriale.

Di default, WordPress permette di inserire nei commenti alcuni tag HTML come <a> <em> <strong> ecc. Se notate, anche molti commenti di spam contengono questi tag. La maggior parte dei commenti di spam sono fatti da bot e script che utilizzano i tag HTML. Se si disabilita semplicemente l’HTML dai commenti di WordPress, si può evitare molto spam. In questo tutorial vi mostreremo come disabilitare i tag HTML nei commenti di WordPress.

Questo tutorial disabilita solo i tag HTML attivi. Quindi qualcuno può ancora pubblicare articoli come:

&lt;a&gt;&lt;em&gt;&lt;strong&gt;

Il messaggio verrà visualizzato, ma i tag non saranno funzionali. Quindi se qualcuno usa la tagga strong, il testo non verrà grassettato. Inoltre, non molti bot di spam hanno il tempo di farlo perché questo metodo richiede molto tempo e non è vantaggioso per loro.

Tutto quello che dovete fare è semplicemente aprire il vostro functions.php e aggiungere il seguente codice:

    // This will occur when the comment is posted
    function plc_comment_post( $incoming_comment ) {

    // convert everything in a comment to display literally
    $incoming_comment['comment_content'] = htmlspecialchars($incoming_comment['comment_content']);

    // the one exception is single quotes, which cannot be #039; because WordPress marks it as spam
    $incoming_comment['comment_content'] = str_replace( "'", '&apos;', $incoming_comment['comment_content'] );

    return( $incoming_comment );
    }

    // This will occur before a comment is displayed
    function plc_comment_display( $comment_to_display ) {

    // Put the single quotes back in
    $comment_to_display = str_replace( '&apos;', "'", $comment_to_display );

    return $comment_to_display;
}

Se non si vuole aggiungere manualmente questo codice, l’autore originale offre anche un plugin che si può scaricare. È sufficiente installare e attivare il plugin Literal Comments di Peter.

Il motivo per cui questo metodo è migliore è che non richiede la modifica dei file principali. Se si desidera modificare i file principali, si può andare in wp-includes/kses.php e modificare i codici lì. (Questo non è consigliato, ma è qui per conoscenza.(WP Codex per maggiori dettagli)

Divulgazione: I nostri contenuti sono sostenuti dai lettori. Ciò significa che se cliccate su alcuni dei nostri link, potremmo guadagnare una commissione. Vedi come WPBeginner è finanziato , perché è importante e come puoi sostenerci. Ecco il nostro processo editoriale .

Avatar

Editorial Staff at WPBeginner is a team of WordPress experts led by Syed Balkhi with over 16 years of experience in WordPress, Web Hosting, eCommerce, SEO, and Marketing. Started in 2009, WPBeginner is now the largest free WordPress resource site in the industry and is often referred to as the Wikipedia for WordPress.

Il kit di strumenti WordPress definitivo

Ottenete l'accesso gratuito al nostro kit di strumenti - una raccolta di prodotti e risorse relative a WordPress che ogni professionista dovrebbe avere!

Reader Interactions

24 commentiLascia una risposta

  1. Syed Balkhi says

    Hey WPBeginner readers,
    Did you know you can win exciting prizes by commenting on WPBeginner?
    Every month, our top blog commenters will win HUGE rewards, including premium WordPress plugin licenses and cash prizes.
    You can get more details about the contest from here.
    Start sharing your thoughts below to stand a chance to win!

  2. Gwyneth Llewelyn says

    Unfortunately, the Peter’s Literal Comments plugin is not active any longer; it has been updated last in 2015, and may not even work on recent versions any more. You might need to recommend a different plugin instead…

    • WPBeginner Support says

      Thank you for letting us know that the plugin is not active a the moment. When we look at updating the article we will look for an alternative :)

      Admin

    • WPBeginner Support says

      Hi,

      You can use the following HTML tags in comments by default.

      <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

      Admin

  3. anshu says

    there are lots of commnts on my every blog post … how to disable HTML hyperlink so that i can prevent my blog post from spam

  4. falvarez says

    I think that something is missing. You should add your function plc_comment_display() to the filter stack like this:

    add_filter(‘comment_text’, ‘plc_comment_display’);

    Regards.

  5. johnroypaul says

    Brilliant article thanks! Seems to work perfectly well with the latest versions of wordpress as well (3.3) Made the code a lot cleaner as mentioned and seems to have made life easier when trying to implement the comments form into side areas ect. (Had a nightmare with previous use of the normal code)

  6. wordpress hosting says

    The more mature you are, the harder at risk you are for getting something undesirable happen to you while overseas. Thanks for revealing your suggestions on this blog.You have good grapes on wordpress thanks for making my problem so easy .

  7. Kamal says

    Great, by this way one can ensure that nobody drops links in the comment box. I really hate to see people doing that unnecessarily.

  8. Cruz3N says

    Its confused me Bro, but i make some simple plugin that allow you to replace with &gt ;

    <?php
    /*
    Plugin Name: Filter Comments
    Plugin URI: http://cruzenaldo.com/plugin-sederhana-filter-komentar/
    Description: Plugin sederhana untuk melakukan filterisasi terhadap komentar dan mencegah user menginput tag – tag HTML
    Author: Cruz3N
    Author URI: http://www.cruzenaldo.com/
    Version: 1.0
    */

    function my_function ($text) {
    $text = str_replace('’, ‘&gt ;’, $text);
    return $text;
    }

    add_filter(‘comment_text’, ‘my_function’);
    ?>

    You can modification that better… Hope usefull

    Download here
    http://www.box.net/shared/rgb4lmt5uy

    And this is my ugly blog :p
    http://cruzenaldo.com/

    Best Regard
    Cruz3N

  9. Infographiste says

    Thank you for the useful tuto and easy to follow. I’ve found another one explaining how to disable HTML but was to hard and badly explained, anyway thanks again and have a great week end :)

Lascia una risposta

Grazie per aver scelto di lasciare un commento. Tenga presente che tutti i commenti sono moderati in base alle nostre politica dei commenti e il suo indirizzo e-mail NON sarà pubblicato. Si prega di NON utilizzare parole chiave nel campo del nome. Avremo una conversazione personale e significativa.