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


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;
});

Full-stack impostor syndrome sufferer & Software Engineer at Schibsted Media Group

Comments 4
  • leo
    Posted on

    leo leo

    Reply Author

    Very useful, learned from it


  • Shivam
    Posted on

    Shivam Shivam

    Reply Author

    Thanks for sharing


  • Garnacha
    Posted on

    Garnacha Garnacha

    Reply Author

    Thank you!


  • Sonu Sharma
    Posted on

    Sonu Sharma Sonu Sharma

    Reply Author

    This handy WordPress snippet ensures a smoother commenting experience for users. It automatically pre-checks the “Save my data in this browser” box if the user previously selected it.

    By using a simple JavaScript check for cookies, this snippet streamlines the process, making engagement more seamless and user-friendly. A perfect tweak for enhancing user convenience!