I would add a hidden input field with the same name before the checkbox:
<input type="hidden" name="email_opt_in" value="0" />
Because the forms are processed sequentially you would get “0” if the form was submitted, but the checkbox wasn’t checked (instead of not getting anything).
Then assume the checkbox is checked if the form wasn’t submitted or if it was submitted and the checkbox was checked:
$checked = !isset($_POST['email_opt_in'])
|| (isset($_POST['email_opt_in']) && $_POST['email_opt_in']);
Later:
... <?php checked($checked); ?>
The entire idea is to store the unchecked state of the checkbox in case of an input error, so the user doesn’t have to check it again if he didn’t want to on the first submission.
Sorry for this long and boring answer for this simple question 🙂
Related Posts:
- Is it possible to remove username field from the registration page? If so, how?
- users table – user_name vs. nicename
- Use the user_activation_key for other purposes
- How to limit user registration to a specific set of domains?
- How do I create a user using the new JSON api in 4.7?
- How to check if the user registration is allowed/active?
- Custom registration form page/template
- Insert “New User” and update/set meta data at once
- Send automatic email to user after wp_create_user
- How to prevent certain usernames from being registered?
- How to disable or protect against disposable email accounts?
- Advice on setting up a site with front end registration
- How to add a description to the user name input field in the registration form?
- How to add a checkbox to registration and user profile?
- wp_create_user hook
- White spaces in username give 404 on author page
- WordPress members-only page with link visible only to members
- Password field (and confirmation) showing up twice on registration
- auto assign sequence base username while registration
- How to disable user registration for the default subscriber user role?
- How do I show errors after validation with a custom form frontend?
- How can I find users that didn’t set a password?
- Send custom signup approval email to different Administrators selectively
- After e-junkie payment, send a http post to register user automatically?
- Email confirmation in user registration form without a plugin
- How to add additional information to a user on user creation?
- How to allow wordpress to create username with symbols like +
- Invalid key on activation and password reset
- Where are people registering on my website?
- How can I have user registration verify a whether a new user is listed in a table of pre-approved users?
- Mobile User Registration
- Conditionally registering users
- WordPress Register Form
- Creating custom registration and login link in wordpress
- Adding register & login in WordPress website
- Adding register & login at top right corner
- How can I disable all client side new accounts without disabling new user accounts?
- Register users without confirmation
- Unique registration for new writers to sign up
- Include “registration.php” for custom registration form?
- Custom registration form with different registration process based on what’s selected
- can registration be enabled programatically?
- register user only with specific Host name
- Generate long hashed user ID
- Disable New User Registration Email sent to WordPress admin email on 4.9.5
- my custom css file downloads then it STOPS!
- Registration area + reserved area
- I want to add a tax-ID field in registration form and upon submission, perform a lookup by tax-ID
- Custom user registration
- Please suggest me some plugins in WordPress networking
- wordpress – user registration ( signup registration )
- Force display name to nickname wp
- not sending correct link to set the password in registration email [closed]
- How to integrate CAPTCHA on register page? [closed]
- wp_insert_user not returning anything
- New register email notification sending some fields blank on site admin emai id
- Profile picture in registration form WordPress [closed]
- Controller functionality – User not logged in/ redirect to registration page (not wp_login)
- Register multiple users in one form
- loading custom registration template
- How to show password fields on registration form w/o plugins
- Why is wp_new_user_notification not working?
- How to add user registration and signup in WordPress and create members only page?
- Single Sign On or Bridge with in the WordPress WebSites?
- Wikipedia – CMS
- Is there a way to check the email of the user trying to register before he registers?
- signup_user() throws fatal error
- Custom Registration Form Based on user location Woocommerce
- Action Hook that fires function upon registration at Woocommerce checkout
- Redirection after registration
- My new website registration error
- Not receiving any sign up mail, when user registers… Both admin & user
- Create a unique username with validation on wordpress registration
- I want to give every new registering user a unique key apart from password field for external use
- Is it possible to remove the password field in the registration page in woocommerce?
- Send a password to a user who has just registered for a member area
- The requested URL /wordpress/register/ was not found on this server
- I have restricted the username to numerical format but not able to integrate the same with PeepSo plugin
- How to hide username on wordpress registration?
- What’s the Point of Spam Registrations?
- Add custom Date column to “All Users” admin panel in WP
- Update a user field with a generated text
- Custom Registration with Select and Upload function
- Macros for WordPress, creating subdomains out of registrations for current domain
- How to make user to logged into website & redirect to homepage after successful registration?
- generate an auto incremented id number when a new user is registered
- Remove wp-login link from auto generated wordpress’s email
- Multisite/Network What file to edit to change the “new blog” registration text
- How do spam users register while I’ve only enabled registration by Gmail via Janrain?
- disabling emails received by admins every time a new user signs up (function not working)
- how to allow unregistred users to view normal posts [closed]
- Wrong user registration date in wordpress
- What hooks, actions or filters i can use to customize wordpress registration page and form?
- Can email address be used as user name?
- Website visible only to Registered users (non wp-admin)
- Plugin recommendation for directory site [closed]
- enqueue style google fonts in functions.php in array?
- Fields in register page is pre-filled out with name and email
- Login to comment: not just user and email
- Change default error message “This email address is already in use” when registering a new user in WordPress using Caldera Forms