WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] [Plugin: Register Plus Redux 3.9] Checkbox value not saving (33 posts)

  1. planktonwebdesign
    Member
    Posted 1 year ago #

    I'm having trouble getting the checkbox extra field working.

    I have created an extra field called 'Instruments/Services' with the following items as available to be checked: Bass Guitar Lessons, Trumpet Lessons, Recorder Lessons.

    The field appears in the edit profile great, I can check the ones I want but when I hit save they revert back to unchecked.

    Here are screenshots of the setup:
    http://cl.ly/image/42070t0C0M32

    and how it appears in the edit profile page:
    http://cl.ly/image/2l2x2G0O2b2V

    Radio buttons work great as do selects, it's just the checkboxes that don't seem to save their value. I downloaded the plugin from here: http://radiok.info/wp-content/uploads/2012/10/register-plus-redux.3.9.zip

    Thanks so much for your help!

  2. radiok
    Member
    Posted 1 year ago #

    Hey, I'm thinking, and I could be wrong that the database key might be the problem, that slash looks... not good to me. Just edit that to be a - and see how that fairs. If that works I'll improve the key algorithm, I'm surprised it put a slash in there to begin with.

  3. planktonwebdesign
    Member
    Posted 1 year ago #

    Hey, thanks heaps for replying!

    I have a couple of extra fields with /'s in the key and seem to be working great for those. Just made another screenshot: http://cl.ly/image/080I0v1L2X19

    Still no luck though. I'm using radio's for the project I'm on and just using Yes or No for them but having checkboxes would be sooo good as my instrument list is getting massive :)

  4. radiok
    Member
    Posted 1 year ago #

    OK, interesting, well then I'll check out checkboxes, it's probably something stupid, but I'll figure it out.

  5. radiok
    Member
    Posted 1 year ago #

    Found the bug, it's on line 173 of rpr-login.php

    echo "\n", '<input type="checkbox" name="', $meta_key, '[]" id="', $meta_key, '-', $option, '" value="$option" ';

    should be

    echo "\n", '<input type="checkbox" name="', $meta_key, '[]" id="', $meta_key, '-', $option, '" value="', $option, '" ';

    This change will make into 3.9.1 and future versions but that alone will get you going.

  6. planktonwebdesign
    Member
    Posted 1 year ago #

    Awesome!

    If it's possible to answer. If my field is Instruments and options (checkboxes) are bass guitar, trumpet etc and they check bass guitar. Will the following code:

    the_author_meta('rpr_instruments');

    output:

    bass guitar

    Haven't tried it yet but presume that's how the checkbox works. Thanks again really appreciate you having a look at code, will donate when project is complete. This really is an great plugin.

  7. planktonwebdesign
    Member
    Posted 1 year ago #

    Bugger just tried it and still not saving?

  8. radiok
    Member
    Posted 1 year ago #

    On the registration side right? Because I just found a bug on the profile side with checkboxes while I was checking out the rest of the code around this feature. Line 158 of register-plus-redux.php needs to be changed.

    $value = get_magic_quotes_gpc() ? stripslashes( $_POST[$meta_field['meta_key']] ) : $_POST[$meta_field['meta_key']];

    to

    $value = get_magic_quotes_gpc() ? stripslashes_deep( $_POST[$meta_field['meta_key']] ) : $_POST[$meta_field['meta_key']];

    Also, you are correct, the_author_meta('rpr_instruments') would output 'bass guitar', or 'trumpet,bass guitar' or whatever, comma separated.

  9. planktonwebdesign
    Member
    Posted 1 year ago #

    Cool they are saving now, only last little thing is that you now can't uncheck. When you uncheck and hit save the options remain checked. I'm testing via the WordPress backend via the user edit page. Haven't tested the registration process so see if the checkboxes save. At the moment I'm just editing current users, hope this helps.

  10. radiok
    Member
    Posted 1 year ago #

    Ah, now that's an interesting bug. I have a solution for that, but it won't be a one liner... Give me a few minutes.

  11. radiok
    Member
    Posted 1 year ago #

    OK, the code block starting on line 157, through 160 of register-plus-redux.php needs to change from:

    if ( array_key_exists( $meta_field['meta_key'], $_POST ) ) {
    	$value = get_magic_quotes_gpc() ? stripslashes_deep( $_POST[$meta_field['meta_key']] ) : $_POST[$meta_field['meta_key']];
    	$this->rpr_update_user_meta( $user_id, $meta_field, $value );
    }

    to

    $value = array_key_exists( $meta_field['meta_key'], $_POST ) ?  $_POST[$meta_field['meta_key']] : '';
    $value = get_magic_quotes_gpc() ? stripslashes_deep( $value ) : $value;
    $this->rpr_update_user_meta( $user_id, $meta_field, $value );

    The problem is that if a field gets nulled out, it never gets posted back, the If statement I had in place wasn't allowing nulls to be saved.

  12. radiok
    Member
    Posted 1 year ago #

    I hope you are done finding all my flaws because I'm heading to bed for the evening! Please let me know if you find anything else interesting or undesirable!

  13. planktonwebdesign
    Member
    Posted 1 year ago #

    Thankyou sooo much mate, I'll keep working but I reckon this should be all good :)

  14. planktonwebdesign
    Member
    Posted 1 year ago #

    Yip that was the fix!

  15. planktonwebdesign
    Member
    Posted 1 year ago #

    One thing I noticed was that the output of the checkbox items using;

    the_author_meta('rpr_instruments');

    Bass Guitar Lessons,Trumpet Lessons,Theory Lessons

    is spat out as the following in my template

    bass_guitar_lessons,trumpet_lessons,theory_lessons

    but in the labels of the form, spacing and capitalisation is respected

    If I put spaces they are replaced with _'s. Is it possible to output the checkboxes as they appear in the labels? I know I can do a str_replace() but wondered if this is avoidable?

    Also I was keen to do a WP_User_Query query to see if the user had checked an option but the value contains all the checked options as above. Do think it is possible to do something like this if the user has checked multiple options?

    $professional_crew = array(
    			'role' => 'professional',
    			'meta_key' => rpr_instruments,
    			'meta_value' => 'theory_lessons'
        		);
    
    WP_User_Query($professional_crew);
  16. radiok
    Member
    Posted 1 year ago #

    Well, I could, and perhaps should, do something about the parsing to underscores. The query question is trickier. You need to check against the meta_value using some kind of wildcard. I'll look into how that would work functionally.

  17. radiok
    Member
    Posted 1 year ago #

    OK, I'm not sure what the default 'meta_compare' is, but you may need to include 'meta_compare' => 'LIKE' in your query. But it looks like it should be able to handle finding a specific instrument in the comma separated list.

  18. radiok
    Member
    Posted 1 year ago #

    Also you may need to wrap the 'meta_value' in wildcards too (i.e '%theory_lessons%'). But I'm not 100% sure, I'll have to do some experimenting.

  19. planktonwebdesign
    Member
    Posted 1 year ago #

    Yeah, I did the meta_compare to 'LIKE' and also tried 'IN' and 'EXISTS' and 'BETWEEN' but none of them worked if the user has checked a couple of options. Havn't tried to break up the options with spaces with a str_replace and see if it works, would I add the %'s to the options when creating them or in the query?

  20. radiok
    Member
    Posted 1 year ago #

    I'm recreating your scenario I'll let you know when I get it working.

  21. planktonwebdesign
    Member
    Posted 1 year ago #

    Cool, here is an example of what I have going in the template if that helps

    // the url is nzmusicteachers.co.nz/theory-lessons
    // the extra field key is rpr_instruments, values are theory_lessons,trumpet_lessons,guitar_lessons
    
    		$slug = basename(get_permalink()); // Page Slug which is theory-lessons
    		$service = str_replace("-", "_", $slug); // so theory-lessons becomes theory_lessons
    
    		$professional_crew = array(
    			'role' => 'professional',
    			'meta_key' => rpr_instruments,
    			'meta_value' => $service,
    			'meta_compare' => 'LIKE'
        	);
    
        ////////////       Professional       ////////////
    
    	// Create the WP_User_Query object
    
    		$wp_user_query = new WP_User_Query($professional_crew);
    
    	// Get the results
    
    		$authors = $wp_user_query->get_results();
    
    	// Check for results
    
    		if (!empty($authors)) {
    
    		// loop through each author
    
    	    	foreach ($authors as $author) { 
    
    	    		// do stuff like
    
    	    		$instruments = get_the_author_meta('rpr_instruments');
    	    		$no_underscores = str_replace("-", "_", $instruments);
    	    		echo str_replace(", ", ",", $no_underscores); // add a space after the comma
    
    			}
    
    		}
  22. radiok
    Member
    Posted 1 year ago #

    OK, well I got it to work on my site. I couldn't mess with the role because I don't have that defined, but I believe that your only problem is that rpr_instruments isn't quoted, change it to 'rpr_instruments' and you should start getting results.

  23. planktonwebdesign
    Member
    Posted 1 year ago #

    Grr I just whacked that example together so shouldn't have made that mistake on my actual template. If you have it working I'll need to try looking harder at my code as it might be something like that that is stopping it from working.

    Thanks so much for taking the time to look at this. Is my str_replace the best way to go to output stuff so it looks like the label I setup in RPR?

    You must be busy so no dramas as I can roll with my hodge podge of code.

    Thanks again! Is is possible to ask you to paste your code here so I can see how you got it going to make sure I'm on the right track?

  24. radiok
    Member
    Posted 1 year ago #

    No problem, let me preface that I use a plugin called Shortcode Exec PHP so that I can run PHP directly without needing a page so I'm running with less overhead then you. But here's the code I wrote that works:

    $service = 'theory_lessons';
    $user_query = new WP_User_Query(array('meta_key' => 'rpr_instruments',
    	'meta_value' => $service,
    	'meta_compare' => 'LIKE'));
    if ( !empty( $user_query->results ) ) {
    	foreach ( $user_query->results as $user ) {
    		echo '<p>', $user->display_name, '</p>';
    	}
    } else {
    	echo 'No users found.';
    }
  25. radiok
    Member
    Posted 1 year ago #

    Oh, as for the str_replace, that looks good enough to me. I'm going to include a new option for you, well, for everyone, to disable my aggressive cleanup code. With this feature enabled the_author_meta would output something less manipulated, in your case "Trumpet Lessons,Theory Lessons" instead of "trumpet_lessons,theory_lessons". The problem with that is that it would actually complicate your code further because your slug "theory-lessons" wouldn't match "Theory Lessons", you'd actually have to texturize the slug which is harder then the reverse.

  26. radiok
    Member
    Posted 1 year ago #

    OK, I kept looking at this and realized I caused a regression from Redux 3.7.3. Your values should have been stored in the database in their full format, "Trumpet Lessons,Theory Lessons". I have fixed this because it will break potentially thousands of sites that upgrade from 3.7.3 to 3.9.

    What does that mean for you? Hm. Change $service = str_replace("-", "_", $slug); to $service = str_replace("-", " ", $slug);. Otherwise you should be good, at least in my demo site that's all I had to do. Let me know how that works for you.

  27. planktonwebdesign
    Member
    Posted 1 year ago #

    Hey Radiok,

    I'll look into this when I update to 3.9.2 saw the upgrade notice :)

    I've pushed the site to a testing server, bugger is the registration process isn't saving any of the additional extra fields :( I can go in an edit my profile and save them which is great but the initial registration isn't saving anything.

    I'm using theme my login too. Do you reckon there is a conflict when you register?

    staging.nzmusicteachers.co.nz

  28. planktonwebdesign
    Member
    Posted 1 year ago #

    Bugger just did a test. I disabled Theme my Login (6.2.3) and the extra fields all save as expected.

    So there must be a conflict once you click register, I'll try disabling some of the redirection and email notification things I have setup in Theme My Login and try using the options available in RPR and see if I can get it to work.

  29. radiok
    Member
    Posted 1 year ago #

    Yup, I can't see Register Plus Redux and Theme My Login working well together anytime soon. I think they may have played well together long ago but Theme My Login has grown and grown nearly to be a full fledged competitor to Redux. You'll have to let me know if there are any features from Theme My Login that you cannot recreate in Redux.

  30. planktonwebdesign
    Member
    Posted 1 year ago #

    It's really just having the login form and register pages in the template:

    http://staging.nzmusicteachers.co.nz/login

    http://staging.nzmusicteachers.co.nz/join-up/?action=register form is down the bottom of this page

    Register Plus has everything except having forms in the front end and some redirect to stop subscribers going to backend on login etc

    The old site nzmusicteachers.co.nz has them working together but the versions are old like you said hmm. Might have to sit on this for a bit?

Topic Closed

This topic has been closed to new replies.

About this Topic