Useful Snippets

Welcome!


This blog is used to collect useful snippets related to Linux, PHP, MySQL and more. Feel free to post comments with improvements or questions!

Are your smart devices spying on you? Make better purchasing choices and find products that respect your privacy at Unwanted.cloud

RSS Latest posts from my personal blog


Most viewed posts


Subscribe to RSS feed


Automatically check the “Save my name, email, and website in this browser for the next time I comment.” box in WordPress

Stanislav KhromovStanislav Khromov

Surprisingly WordPress always seems to leave the box unchecked when loading a page, so a user has to keep checking it each time they want to leave a comment to keep their name saved. With this simple JavaScript snippet the checkbox will be prefilled if a user has previously checked it and submitted a comment.

<?php
/**
 * Automatically tick the "Save my data in this browser" checkbox on comments if it was ticked before.
 */
add_action('wp_footer', function() {
    if(is_single() && comments_open()):
    ?>
        <script>
            if(document.cookie && document.cookie.includes('comment_author_')) {
                document.getElementById('wp-comment-cookies-consent').checked = true;
            }
        </script>
    <?php
    endif;
});

Web Developer at Aftonbladet (Schibsted Media Group)
Any opinions on this blog are my own and do not reflect the views of my employer.
LinkedIn
Twitter
WordPress.org Profile
Visit my other blog

Comments 2
  • leo
    Posted on

    leo leo

    Reply Author

    Very useful, learned from it


  • Shivam
    Posted on

    Shivam Shivam

    Reply Author

    Thanks for sharing