How to add a required chckbox to forms?

Can anyone tell me what is the easiest, and simplest way - without using a plugin (i.e. code I can put directly into my site-specific plugin) to achieve this:

I’m presently using a plugin to insert it, which is not ideal for a number of reasons, so I thought I’d ask if anyone knows how to insert a checkbox like that that is required and returns an error if submitted unchecked?

Thanks all!

2 Likes

…using a plugin is the simplest way. Have you considered just copying that plugin’s code into your own site-specific plugin?

Aside of this, you could hook in on the comment_form filter to add a checkbox…but, that only adds it. You’d still need code to check the value, return to (and repopulate) the form if not checked, somewhere to store the fact that the user checked the box…and code to show/hide the checkbox depending on the activation state of Akismet… probably more.

In other words, it might as well just be a plugin.

2 Likes

I suppose it depends on how your plugin is coded and what you want to achieve. If you’re just looking to prevent people from submitting the form without them acknowledging that their comments will be checked by Akismet, you could do something simple with Javascript.

Maybe something like this Codepen https://codepen.io/nurdlefurter/pen/RwNpMOv

If you’re looking for something more than that, then I guess we’d need more info.

1 Like

If using JS for this, then you’d have to pretty much hide the form altogether if JS is disabled to prevent people from getting around it. I tend to surf JS-free (allowing scripts only as-needed.)

2 Likes

I don’t disagree. Without having more detailed info, that was just intended to be a quick “here’s something to think about” rather than a complete solution.

For years, I used NoScript. But I eventually got fed up of having to enable Javascript on a site-by-site basis. I think JS has become more prevalent and accepted in recent years, especially given the increased use of frameworks like Vue, React and AngularJS, etc. Most GDPR notices depend on JS for instance, which is ironic in a way. :slight_smile:

3 Likes

That doesn’t address my question at all! This is a coding question - how do I do it myself? Jeez I said I’m not happy with 3rd-party plugin.

No Javascript, it must be pure back-end (as if let the client authenticate itself!)

1 Like

…this is how you’d do it yourself. I didn’t realize you were asking for all the actual code. :slight_smile:

2 Likes

Maybe a search for adding a checkbox to the “wordpress comments” form?

Google turned this up. It looks like a good starting point, if not a full solution.

https://isabelcastillo.com/add-checkbox-wordpress-comment-reply-form

2 Likes

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.