• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

RocketGeek

Home of WP-Members, The Original WordPress Membership Plugin

  • WordPress Plugins
    • WP-Members
      • FAQs
      • Quick Start
      • Documentation
      • Extensions
    • Advanced Options
      • Documentation
      • Purchase the Plugin
      • Get the Pro Bundle
    • Download Protect
      • Documentation
      • Purchase the Plugin
      • Get the Pro Bundle
    • Invite Codes
      • Documentation
      • Purchase the Plugin
      • Get the Pro Bundle
    • MailChimp Integration
      • Documentation
      • Purchase the Plugin
      • Get the Pro Bundle
    • PayPal Subscriptions
      • Documentation
      • Purchase the Plugin
      • Get the Pro Bundle
    • Salesforce Web-to-Lead
    • Security
      • Documentation
      • Purchase the Plugin
      • Get the Pro Bundle
    • Text Editor
      • Purchase the Plugin
      • Get the Pro Bundle
    • User List
      • Documentation
      • Purchase the Plugin
      • Get the Pro Bundle
    • User Tracking
      • Documentation
      • Purchase the Plugin
      • Get the Pro Bundle
    • Memberships for WooCommerce
    • WordPass
  • Store
    • Cart
    • Checkout
  • Blog
    • Basics [Free]
    • Tips and Tricks
    • Filters
    • Actions
    • Code Snippets
    • Shortcodes
    • Design
    • Release Announcements
  • Contact
  • Sign In
  • Show Search
Hide Search
Home » Filters » Multiple Selection Checkboxes

Multiple Selection Checkboxes

Chad Butler · Apr 22, 2014 ·

As of WP-Members 3.1.0, multiple selection checkbox support is an included feature in the plugin. That makes custom implementation such as described here unnecessary for most applications. I am leaving this post here however as there may continue to be users who (1) need a high level of customization that may utilize some or all of this kind of approach, and (2) for users who may not update to 3.1+ who need this support.

multi-check.fwWith the new wpmem_register_form_rows filter hook introduced in 2.9, the process of dealing with multiple selection checkboxes becomes far less complicated than previously.

The previous method involved adding each checkbox as a field, and then all of the extra code we needed to handle multiple fields as one both for storing the data, handling user updates, and also in emailing admin notifications.

But 2.9 allows us to filter the rows directly in the array of rows and insert the extra html we need for this. That means you only need to create one field in the fields manager which makes handling the data much easier on the backend.  I’ve even made the code snippets nearly cut-and-paste.  In most cases, you should only need to specify the name for the checkbox group and the labels/checked values. 

This article is only available to WP-Members Support Subscribers. If you have an existing subscription, please login below. If you do not have a current support subscription, you can purchase a support subscription here.

Already a Member? Log In Here
   
Forgot password? Click here to reset

To gain full access to WP-Members premium content, a current support subscription is required. You can purchase an annual support subscription for as little as $59, which provides you with access to priority support, a customer support forum, access to code snippets, and more.

Why wait? Choose your subscription option here.

[
Why join?]

Filters, Tips and Tricks filters, str_replace, tips, wpmem_admin_profile_field, wpmem_admin_profile_update, wpmem_register_data, wpmem_register_form_rows

Welcome to RocketGeek Interactive › Forums › Multiple Selection Checkboxes

Tagged: filters, str_replace, tips, wpmem_admin_profile_field, wpmem_admin_profile_update, wpmem_register_data, wpmem_register_form_rows

  • This topic has 23 replies, 9 voices, and was last updated 4 years, 12 months ago by dj.
Viewing 13 reply threads
  • Author
    Posts
    • April 22, 2014 at 5:35 pm #4733
      Chad Butler
      Keymaster

      With the new wpmem_register_form_rows filter hook introduced in 2.9, the process of dealing with multiple selection checkboxes becomes far less compli
      [See the full post at: Multiple Selection Checkboxes]

    • April 24, 2014 at 3:59 am #4750
      KCIT-pa
      Blocked

      Hi Chad..

      Thanks for the code, i needed multiple checkboxes for more than 1 fields, so modified the mode to suit that. Registration, Profile Update via Frontend all works fine but user profile doesn’t show correctly in admin. Here are some screenshots:

      1) http://awesomescreenshot.com/0af2pbh675 (Registration Screen)
      2) http://awesomescreenshot.com/0322pbhs42 (Profile Update)
      3) http://awesomescreenshot.com/0402pbi8be (Admin User Profile)

      I check the code and it seems the function my_checkbox_array_admin( $field ), in this $field doesn’t contain anything when filter is called.

      Can you suggest the fix?

      Thanks

      • April 26, 2014 at 9:04 am #4783
        Chad Butler
        Keymaster

        The challenge in your case is that the example is coming from the assumption of one group of checkboxes. But your case has multiple groups.

        It really doesn’t make sense to apply an entire block of this code snippet for each of the checkbox groups – that would be inefficient for you to setup.

        I need to think through this a little more to figure out a way to make it functional for your application.

        • April 26, 2014 at 10:01 pm #4787
          Chad Butler
          Keymaster

          I’ve updated the code to handle multiple checkbox groups. That should correct the issue you are having on the backend. Since the first/original example was kind of limited to a single group, I thought that it was probably better to update the post and the example code rather than post this as a new example.

          The setup is essentially no different than the original – you would set up a placeholder checkbox exactly the same as before. The only difference is that the snippet uses one helper function that will contain the information for ALL checkbox groups. I hope that I’ve commented this section well enough. If not, let me know and I’ll try to better clarify.

          The other thing I changed was the CSS. The original tagged the specific checkbox label. But that could be tedious in a large group like yours. So I modified the section in the code that updates the row and slipped in a second class “.multi” so now instead of label[for=”option_name”] it can be styled with label.multi

          Hope this makes sense. Like I said, the core concept is essentially the same.

    • June 1, 2014 at 1:24 am #5009
      VanWebDev
      Participant

      This is a fantastic bit of code, but I’m having one issue with it. It works perfectly when the user marks one in each group of checkboxes, but when they don’t mark any, I get php errors all over the place. Is there any fix for this?

      Thank you

    • June 1, 2014 at 7:32 am #5012
      Chad Butler
      Keymaster

      Thanks for noting this. A simple check to see if the posted checkbox is set resolves it. I guess I didn’t test it with any empty values. I’ve modified the code snippet to include this. (The change is at line 73 in the example – adding a test for “isset”.)

      What you are probably getting is actually “notices” and not “errors”. These are notices from PHP and can be turned off. Generally, on a production site, you would have error settings for PHP to only show errors and not notices. These can also show if WP is set in debug mode, which you also should probably not do on a production site.

    • June 1, 2014 at 12:13 pm #5013
      VanWebDev
      Participant

      Actually they were “Warnings”, (in_array() expects parameter 2 blah blah blah) so yes a minor exaggeration. Appreciate you addressing this, and so quickly too!

      Thank you.

    • June 1, 2014 at 12:23 pm #5014
      VanWebDev
      Participant

      This fixed the in_array issue, but on form submission there is a Warning: implode() [function.implode]: Invalid arguments passed being returned, referring to what would be line 102 of the example. I prefer to avoid potential complications, so yes, even though the warnings can be turned off, I would prefer to resolve them before taking a site live.

      Thank you.

      • June 1, 2014 at 11:56 pm #5016
        Chad Butler
        Keymaster

        I think I have a resolution for that, but I’m going to do some more testing on it.

        • June 2, 2014 at 3:31 pm #5022
          Chad Butler
          Keymaster

          The extra testing was the result of some errors I couldn’t figure out, which turned out to be due to the fact that I didn’t follow my own setup instructions – DOH!

          Anyway, I think I’ve got it now. I changed the my_checkbox_array_post() function to implode only if the checkbox is posted. Otherwise it is just the empty value. I also updated the admin/profile update to better handle the explode of the value (passing an empty array if the stored value is empty).

          The code in the post now reflects these updates.

          Hope that helps.

    • June 2, 2014 at 3:36 pm #5023
      VanWebDev
      Participant

      Men + instructions don’t mix! Thanks for this. Launch is tomorrow, so appreciate the fast support.

    • June 4, 2014 at 12:10 pm #5038
      dmenne
      Participant

      Page overflow with multiple selection checkboxes

      There seems to be a page overflow problem when using long lists of multiple-selection checkboxes

      The img link seem to be dysfunctional.

      https://www.dropbox.com/sh/rw87zcqgkg5t783/AAByAwp63j_6_8JrqrAH2X2Ea

      • June 4, 2014 at 1:29 pm #5041
        dmenne
        Participant

        #wpmem_reg .multi {
        height: 166px;
        }

        Hi, Dieter, you have to change the CSS.

        Greetings. Dieter

    • January 26, 2015 at 7:57 am #6978
      mazzaji@gmail.com
      Participant

      Hi Chad,

      Could you perhaps advise how to display the checkboxes in line. (verticle)

      • January 26, 2015 at 10:20 am #6981
        Chad Butler
        Keymaster

        I think you mean horizontal, since the current example would display vertically.

        That is something that would be determined by the HTML you output. The example code is just one particular example of what you could do. Even though the code snippet has a comment in it that says “you don’t need to change anything past this…” that really just refers to setting up the example “as-is”. But I’ve commented the code beyond that so you can figure out what is going on in each step along the way and thus customize it beyond the example given.

        If you look at the first function past that comment, my_checkbox_array(), this is the function that filters form row for the placeholder checkbox and replaces that with some HTML for the multi-select checkboxes. Going down into that function, you’ll see a comment that says “assemble the checkboxes” and this is where that HTML is put together.

        There are two lines of that process. The first just determines if a given checkbox should be checked or not. The second is the one that you need to concern yourself with. This is putting the new output HTML into a variable called $output.

        The example looks like this:

        $output.= '<input name="' . $box_key . '[]" type="checkbox" id="' . $box_key . '" value="' . $key . '"' . $chked . ' /> ' . $val . ' <br />';

        So it outputs an input tag, some text (with no tag), and a line break (
        ).

        If you want things in-line (horizontal display), the easy thing to do would be just eliminate the line break. However, you need a little more control to get things presented well, so it may be advantageous to wrap each of these with a tag you could give a class to and apply some CSS (such as margins, padding, etc). I would use a span for that rather than a div, which would still allow these all on one line without additional CSS for that. So adding a span before that input tag and closing it at the end in place of the line break would make that line look like this:

        $output.= '<span class="multicheck"><input name="' . $box_key . '[]" type="checkbox" id="' . $box_key . '" value="' . $key . '"' . $chked . ' /> ' . $val . '</span>';

        I gave my example span a class “multicheck” that I could then use to apply some CSS for adjusting margins/padding between the choices, such as:

        .multicheck { margin-right: 6px; }

        There’s not a “one-size-fits-all” example, so in order to get this to lay out well, you’ll need to figure out what kind of HTML and CSS you need in your particular application, but this generalization would be a “one-size-fits-most” and should give most users the information they need to further their customization of the example.

    • May 13, 2015 at 6:47 pm #7912
      zonkeroo
      Blocked

      Checkboxes show fine after adding code.

      But now getting:

      Undefined offset: 1 in wp-content/plugins/wp-members/utilities.php on line 82

      Seems I am not alone: Google

      Thanks for any help.

      • May 13, 2015 at 10:43 pm #7913
        Chad Butler
        Keymaster

        A couple of things about that message: First, it is a “notice” so it only shows if you are running with debug mode turned on, or you have PHP’s messaging set to a level of showing all messages – neither of these two things should you do on a production site.

        The issue arises if you have a comma at the end of the list of values for a dropdown list. I can see that the code could probably be improved to avoid this, but you can resolve it by editing any dropdown fields and look at the values, make sure the last value does not have a comma at the end.

    • September 23, 2015 at 12:42 pm #8602
      digourideas
      Participant

      I have the Multiple Selections Checkboxes script implemented on my site, but they don’t show up in the dashboard when I try to add a new user. Any idea why this may be? They show up when I edit a user, but not when I add a new user. Is there a way to make these checkboxes show up in the dashboard when I add a new user?

      • November 4, 2015 at 4:04 pm #8846
        Chad Butler
        Keymaster

        Sorry for the delayed response – I missed this somehow. Currently, no, there is not a hook in the process that we can use for when a new user is created via the admin.

    • November 16, 2015 at 3:10 am #8926
      sustainergies
      Participant

      Hello!

      I like and understand the explanation about this code snippet. But I can’t find a description of where I should put the code, in which file so to speak? I don’t want my code to be overwritten during updates of the WordPress files or other plugins.

      And how come I can’t see the other 18 replies that have been created related to this post and topic? Maybe someone has asked the same question before.

      Thanks!

      • November 16, 2015 at 8:51 am #8927
        Chad Butler
        Keymaster

        I don’t want my code to be overwritten during updates of the WordPress files or other plugins.

        I don’t want that to happen either! 😉

        The place you generally want to put custom code is in your theme’s functions.php file. (Side note: you should be using a child theme for customizing the theme and you should use the functions.php in the child.)

        There is a section of the site called “Basics” with a number of posts on WordPress best practices such as the functions.php and using a child theme. Hopefully those give you some helpful guidance. If you need more info on those topics definitely ask – you can either use the forum or contact me directly.

        I’m not sure why you are unable to see previous replies. I double checked your login and you are enabled for forum access. You should be able to see the entire thread at the bottom of the post and also in the forum. If you can’t see the entire thread in either of those locations, let me know.

    • March 7, 2016 at 6:15 pm #9715
      dj
      Participant

      Hi Chad,

      Is it possible to apply the checkboxes only to a version of the form that is set by user role? We’re showing 3 different forms based on user role using the Request additional data fields on a specific page approach. The checkboxes field is showing only on one, and this solution works beautifully as-is on that form, but for the others where that checkboxes field is hidden we are obviously getting a series of errors as the checkboxes plugin is looking for an undefined row.

      My attempts at wrapping a conditional around parts of this plugin have been pretty rudimental. Any pointers in the right direction would be much appreciated, even though this functionality is on your roadmap for 3.1!

      Cheers.

      • March 7, 2016 at 9:01 pm #9716
        Chad Butler
        Keymaster

        Any time you have a field that is used in a specific instance and not another, you have to consider how it is handled when it is needed but also when it is not. That sounds a little weird, but that’s the gist of it.

        The trick here is that the handling for multiple selection checkboxes do currently revolve around a placehoder checkbox field that you essentially convert into a multi select. And unlike slipping in a “regular” field, there are several additional functions that have to be considered.

        It may be an issue of using is_page() for checking that you are on the page this is being used on:

        if ( is_page( 'the_page_slug' ) ) {
             //... 
        }

        Hope that helps

    • March 8, 2016 at 7:24 pm #9718
      dj
      Participant

      Hi Chad,

      Can’t see this conversation thread at all (the link to your reply above leads to a ‘Not found, error 404’ page), can you shoot me an email or move the convo to the forums perchance? Thanks 🙂

  • Author
    Posts
Viewing 13 reply threads
  • You must be logged in to reply to this topic.
Log In

Primary Sidebar

Forgot?  Register

Topics

  • Actions
  • Basics
  • Blog
  • Design
  • Download Protect
  • Filters
  • How I Did It
  • MailChimp
  • Release Announcements
  • Shortcodes
  • Templates
  • Tips and Tricks
  • Uncategorized
  • User List
  • User Tracking
  • WooCommerce

Recent Posts

  • WP-Members User List 2.0.1 release
  • WP-Members Download Protect version 1.7.0
  • WP-Members Security 1.3.0
  • WP-Members 3.3.8 Update
  • Patch file available for password reset issues

Tags

actions add_action basics bug-fix css demo email filters forms free get_avatar get_currentuserinfo hooks is_page is_user_logged_in mailchimp menus paypal registration release-notes security shortcodes str_replace template_redirect the_content tips user-list utility wp-filters wpdb wpmem_admin_after_block_meta wpmem_admin_block_meta_save wpmem_block wpmem_login_form wpmem_member_links wpmem_post_register_data wpmem_post_update_data wpmem_pre_register_data wpmem_register_data wpmem_register_form wpmem_register_form_rows wpmem_securify wpmem_sidebar_form wpmem_sidebar_status wp_mail

Ready to get started?

Join Today!

© 2021 · butlerblog.com · RocketGeek is built using WordPress, WP-Members, and the Genesis Framework

  • butlerblog.com
  • WP-Members Support Subscription
  • Terms of Service
  • Refund Policy