• Resolved navyspitfire

    (@navyspitfire)


    I am trying to update a table when the user is approved. The code I am using to try and update the table works in the users profile page when they update the zipcode, so I am trying to append this code to the approval section.

    elseif ( 'Approve' == $action ) {
    	global $wpdb;
    
    	// get the zipcode value
    	$zipcode = xprofile_get_field_data( 5, $user_id );
    
    	switch ( $zipcode ) {
    
    	    case '10026':
    	    	$wpdb->update( 'wppl_friends_locator',
    			    array(
    		    		'zipcode' => '10026',
    		    		'lat' 	  => '40.802381',
    		    		'long' 	  => '-73.952681'
    			    ),
    			    array( 'member_id' => $user_id ),
    			    array(
    			    	'%s',
    			    	'%s',
    			    	'%s'
    			    ),
    				array( '%d' )
    			);
    		break;
    
    	    case '10027':
    	    	$wpdb->update( 'wppl_friends_locator',
    			    array(
    		    		'zipcode' => '10027',
    		    		'lat' 	  => '40.811407',
    		    		'long' 	  => '-73.953060'
    			    ),
    			    array( 'member_id' => $user_id ),
    			    array(
    			    	'%s',
    			    	'%s',
    			    	'%s'
    			    ),
    				array( '%d' )
    			);
    		break;
    	}
    
    	bp_registration_set_moderation_status( $user_id, 'false' );
    
    	bp_core_process_spammer_status( $user_id, 'ham' );
    
    	//assigns role upon approval
    	wp_update_user(array(
    	    'ID' => $user_id,
    	    'role' => 'subscriber'
    	));
    
    	do_action( 'bpro_hook_approved_user', $user_id );
    
    	bp_registration_options_delete_user_count_transient();
    
    	$sql = "DELETE FROM wp_signups WHERE user_email='". $user->user_login . "'";
    	$wpdb->query($sql);
    }

    I tried moving the switch statement to end thinking maybe it’s a placement issue. Any idea?

    https://ww.wp.xz.cn/plugins/bp-registration-options/

Viewing 11 replies - 1 through 11 (of 11 total)
  • Michael Beckwith

    (@tw2113)

    The BenchPresser

    Any reason why you’re not using the do_action hook, out of curiosity? Are you needing to run things specifically before the approval actually occurs? or just right around when one is approved.

    Would help with making sure we’re not mixing your attempts to modify things, and the default values for that block of code in our plugin.

    Thread Starter navyspitfire

    (@navyspitfire)

    Use do action to update the table?

    Maybe I am misunderstanding. The user registers and goes to the pending status, waiting for approval. Once they are approved, all their info is added to the db, at which point I want my wppl_friends_locator table to be updated with one of the fields $zipcode = xprofile_get_field_data( 5, $user_id ); they filled out in the initial registration.

    Michael Beckwith

    (@tw2113)

    The BenchPresser

    Well, it looks like you’re editing our plugin file directly, which would ruin the ability to update it later whenever we get a new release ready.

    What I’m suggesting is using this hook: do_action( 'bpro_hook_approved_user', $user_id ); to run your own code at the point of approval.

    function navyspitfire_update_tables( $user_id = 0 ) {
        // Do all your things here, using the $user_id variable that is passed in by the action hook.
    }
    add_action( 'bpro_hook_approved_user', 'navyspitfire_update_tables' );

    You can put that in your theme’s functions.php file and it’ll be ready to do everything at the time of approval.

    Thread Starter navyspitfire

    (@navyspitfire)

    Hmm is $user_id = 0 right or do I have to change that? I tried the switch code with and without setting a value but neither worked. I don’t know if it makes a difference but the user’s ID is different when they are pending vs when they are accepted.

    All the code I had in the plugin file before I tried adding the switch statement (wp_update_user, #sql DELETE) worked when the user got approved, but the switch statement wasn’t saving the registration info to that database table.

    function navyspitfire_update_tables( $user_id = 0 ) {
        global $wpdb;
    
        // get the zipcode value
        $zipcode = xprofile_get_field_data( 5, $user_id );
    
        switch ( $zipcode ) {
    
            case '10026':
                $wpdb->update( 'wppl_friends_locator',
                    array(
                        'zipcode'               => '10026',
                        'lat'                   => '40.802381',
                        'long'                  => '-73.952681',
                        'state'                 => 'NY',
                        'state_long'            => 'New York',
                        'country'               => 'US',
                        'country_long'          => 'United States',
                        'address'               => '   NY 10026 US',
                        'formatted_address'     => 'New York, NY 10026, USA',
                        'map_icon'              => '_default.png'
                    ),
                    array( 'member_id' => $user_id ),
                    array(
                        '%s',
                        '%s',
                        '%s',
                        '%s',
                        '%s',
                        '%s',
                        '%s',
                        '%s',
                        '%s',
                        '%s'
                    ),
                    array( '%d' )
                );
            break;
    
            case '10027':
                $wpdb->update( 'wppl_friends_locator',
                    array(
                        'zipcode'               => '10027',
                        'lat'                   => '40.811407',
                        'long'                  => '-73.953060',
                        'state'                 => 'NY',
                        'state_long'            => 'New York',
                        'country'               => 'US',
                        'country_long'          => 'United States',
                        'address'               => '   NY 10027 US',
                        'formatted_address'     => 'New York, NY 10027, USA',
                        'map_icon'              => '_default.png'
                    ),
                    array( 'member_id' => $user_id ),
                    array(
                        '%s',
                        '%s',
                        '%s',
                        '%s',
                        '%s',
                        '%s',
                        '%s',
                        '%s',
                        '%s',
                        '%s'
                    ),
                    array( '%d' )
                );
            break;
    
            default:
                $wpdb->update( 'wppl_friends_locator',
                    array(
                        'zipcode'               => '10001',
                        'lat'                   => '40.750633',
                        'long'                  => '-73.997177',
                        'state'                 => 'NY',
                        'state_long'            => 'New York',
                        'country'               => 'US',
                        'country_long'          => 'United States',
                        'address'               => '   NY 10001 US',
                        'formatted_address'     => 'New York, NY 10001, USA',
                        'map_icon'              => '_default.png'
                    ),
                    array( 'member_id' => $user_id ),
                    array(
                        '%s',
                        '%s',
                        '%s',
                        '%s',
                        '%s',
                        '%s',
                        '%s',
                        '%s',
                        '%s',
                        '%s'
                    ),
                    array( '%d' )
                );
            break;
        }
    }
    add_action( 'bpro_hook_approved_user', 'navyspitfire_update_tables' );

    This doesn’t work.

    Michael Beckwith

    (@tw2113)

    The BenchPresser

    The $user_id = 0 aspect is just setting a default value if one isn’t provided. This should be providing an ID, and it’d be the one matching what is in the plugin function at that time, which would be the one being approved.

    Not sure about the pending vs approved user ID. As far as I’m aware, that would be the same. Changing the user ID values would create too much potential for conflicts, especially on a site intended to have many users. The only case I could see for this is if the signups area uses the wp_signups table, for which that signup_id would not be the same as the user_id. You would need to select by user_login for that.

    Have you verified what the $user_id is, and if it matches the database values, in your callback above?

    Thread Starter navyspitfire

    (@navyspitfire)

    No I haven’t, how would I do that?

    I’m not sure why the ID’s are different, but they are, by 31 I think.

    Michael Beckwith

    (@tw2113)

    The BenchPresser

    You’d need to echo the value to the browser or if you have the dev tool setup, a breakpoint at that point would work.

    Thread Starter navyspitfire

    (@navyspitfire)

    Ah thanks for your help. $user_id and $zipcode echo the correct values, but for some reason that table isn’t updating. Frustrating.

    Michael Beckwith

    (@tw2113)

    The BenchPresser

    At that point, it’s on your SQL UPDATE statements 😉

    Thread Starter navyspitfire

    (@navyspitfire)

    You’re right. It’s supposed to be insert, not update, since it’s a new entry!

    Michael Beckwith

    (@tw2113)

    The BenchPresser

    …that makes sense 😀

Viewing 11 replies - 1 through 11 (of 11 total)

The topic ‘Updating table after approval’ is closed to new replies.