Mecha CMS

Mecha CMS blog and documentation.

Store User Comment Data in the Comment Form

Updated: Sunday, 07 August 2016

Using cookies to store your visitor’s comment data in the comment form.

Add this snippet to your functions.php:

// Save data on comment submit
Weapon::add('on_comment_construct', function() {
    Session::set('cookie:user_comment_data', array(
        'name' => Request::post('name', ""),
        'email' => Request::post('email', ""),
        'url' => Request::post('url', "")
    ), 30); // 30 days
});

// Call data on `GET` request
if(Request::method('get') && $config->page_type === 'article') {
    Weapon::add('shield_before', function() use($config) {
        $user = Session::get('cookie:user_comment_data', array(
            'name' => "",
            'email' => "",
            'url' => ""
        ));
        Guardian::memorize($user);
    });
}

With this way, you can save your visitor’s comment data to the cookies once a new comment successfully added to your blog article. So if next time your visitor want to add a comment again, then they will see that the name, email and URL field(s) in the comment form are already filled in with their previous data.

Donation and Email Subscription