What is do_action for in the Membership Account Page?

This is a super specific question, but I can't figure it out.

I'm trying to extend the functionality of the built in front-end account page provided by your membership plugin, and I am using your code as a template. I understand everything that is going on except for the following:

(line 37)
`do_action('edit_user_profile_update', $user_id);

What is this line of code for? At this point, it would seem as though all of the fields of the user have already been updated - so why does this have to be called?

  • William

    It won't let me upload the code, here it is (line 62 here).

    <?php
    	global $profileuser, $user_id, $user;
    
    	if(isset($_POST['action']) && $_POST['action'] == 'update') {
    
    		if( wp_verify_nonce($_REQUEST['_wpnonce'], 'update-user_' . $user_id) ) {
    			$msg = '<div class="alert alert-success">' . __('Your details have been updated.','membership') . '</div>';
    
    			$user = array( 	'ID'			=>	$_POST['user_id'],
    							'first_name'	=>	$_POST['first_name'],
    							'last_name'		=>	$_POST['last_name'],
    							'nickname'		=>	$_POST['nickname'],
    							'display_name'	=>	$_POST['display_name'],
    							'user_email'	=>	$_POST['email'],
    							'user_url'		=>	$_POST['url']
    						);
    
    			if(!empty($_POST['pass1'])) {
    				if(($_POST['pass1'] == $_POST['pass2'])) {
    					$user['user_pass'] = $_POST['pass1'];
    				} else {
    					$msg = "<div class='alert alert-error'>" . __('Your password settings do not match','membership') . "</div>";
    				}
    			}
    
    			$errors = edit_user( $user['ID'] );
    			$profileuser = get_user_to_edit($user_id);
    
    			if ( isset( $errors ) && is_wp_error( $errors ) ) {
    				$msg = "<div class='alert alert-error'>" . implode( "<br/>\n", $errors->get_error_messages() ) . "</div>";
    			}
    
    		} else {
    			$msg = "<div class='alert alert-error'>" . __('Your details could not be updated.','membership') . "</div>";
    		}
    
    		do_action('edit_user_profile_update', $user_id);
    	}
    
    ?>
    
    <div id='membership-wrapper'>
    
    <?php if(!empty($msg)) {
    ?>
    	<?php echo $msg; ?>
    <?php
    } ?>
    
    <form class="form-membership" action="<?php echo get_permalink(); ?>" method="post">
    	<?php wp_nonce_field('update-user_' . $user_id); ?>
    	<input type="hidden" name="action" value="update" />
    	<input type="hidden" name="user_id" id="user_id" value="<?php echo esc_attr($user_id); ?>" />
    
    	<fieldset>
    		<legend><?php _e( 'Edit your details', 'membership' ) ?></legend>
    
    			<div class="form-element">
    				<label class="control-label" for="user_login"><?php _e('Username', 'membership'); ?></label>
    				<div class="element">
    					<input type="text" class="input-xlarge" id="user_login" nmae="user_login" placeholder="" value="<?php echo esc_attr($profileuser->user_login); ?>" disabled="disabled" >
    					<p class="help-block"><?php _e('Usernames cannot be changed.','membership'); ?></p>
    				</div>
    			</div>
    
    			<div class="form-element">
    				<label class="control-label" for="first_name"><?php _e('First Name', 'membership'); ?></label>
    				<div class="element">
    					<input type="text" class="input-xlarge" id="first_name" name="first_name" placeholder="" value="<?php echo esc_attr($profileuser->first_name); ?>" >
    				</div>
    			</div>
    
    			<div class="form-element">
    				<label class="control-label" for="last_name"><?php _e('Last Name', 'membership'); ?></label>
    				<div class="element">
    					<input type="text" class="input-xlarge" id="last_name" name="last_name" placeholder="" value="<?php echo esc_attr($profileuser->last_name) ?>" >
    				</div>
    			</div>
    
    			<div class="form-element">
    				<label class="control-label" for="nickname"><?php _e('Nickname', 'membership'); ?></label>
    				<div class="element">
    					<input type="text" class="input-xlarge" id="nickname" name="nickname" placeholder="" value="<?php echo esc_attr($profileuser->nickname) ?>" >
    				</div>
    			</div>
    
    			<div class="form-element">
    				<label class="control-label" for="display_name"><?php _e('Display name as', 'membership'); ?></label>
    				<div class="element">
    					<select name="display_name" id="display_name">
    					<?php
    						$public_display = array();
    						$public_display['display_username']  = $profileuser->user_login;
    						$public_display['display_nickname']  = $profileuser->nickname;
    						if ( !empty($profileuser->first_name) )
    							$public_display['display_firstname'] = $profileuser->first_name;
    						if ( !empty($profileuser->last_name) )
    							$public_display['display_lastname'] = $profileuser->last_name;
    						if ( !empty($profileuser->first_name) && !empty($profileuser->last_name) ) {
    							$public_display['display_firstlast'] = $profileuser->first_name . ' ' . $profileuser->last_name;
    							$public_display['display_lastfirst'] = $profileuser->last_name . ' ' . $profileuser->first_name;
    						}
    						if ( !in_array( $profileuser->display_name, $public_display ) ) // Only add this if it isn't duplicated elsewhere
    							$public_display = array( 'display_displayname' => $profileuser->display_name ) + $public_display;
    						$public_display = array_map( 'trim', $public_display );
    						$public_display = array_unique( $public_display );
    						foreach ( $public_display as $id => $item ) {
    					?>
    						<option id="<?php echo $id; ?>" value="<?php echo esc_attr($item); ?>"<?php selected( $profileuser->display_name, $item ); ?>><?php echo $item; ?></option>
    					<?php
    						}
    					?>
    					</select>
    				</div>
    			</div>
    
    			<div class="form-element">
    				<label class="control-label" for="email"><?php _e('Email', 'membership'); ?></label>
    				<div class="element">
    					<input type="text" class="input-xlarge" name="email" id="email" value="<?php echo esc_attr($profileuser->user_email) ?>" />
    				</div>
    			</div>
    
    			<div class="form-element">
    				<label class="control-label" for="url"><?php _e('Website', 'membership'); ?></label>
    				<div class="element">
    					<input type="text" class="input-xlarge" name="url" id="url" value="<?php echo esc_attr($profileuser->user_url) ?>" />
    				</div>
    			</div>
    
    			<div class="form-element">
    				<p class="help-block"><?php _e('To change your password, enter the new password below and then repeat it to confirm, otherwise leave these two fields blank.','membership'); ?></p>
    			</div>
    
    			<div class="form-element">
    				<label class="control-label" for="pass1"><?php _e('New Password', 'membership'); ?></label>
    				<div class="element">
    					<input type="password" class="input-xlarge" name="pass1" id="pass1" value="" autocomplete="off" />
    				</div>
    			</div>
    
    			<div class="form-element">
    				<label class="control-label" for="pass1"><?php _e('Confirm Password', 'membership'); ?></label>
    				<div class="element">
    					<input type="password" class="input-xlarge" name="pass2" id="pass2" value="" autocomplete="off" />
    				</div>
    			</div>
    
    			<p><input type="submit" value="<?php _e('Update Account','membership'); ?>" class="alignright button <?php echo apply_filters('membership_account_button_color', 'blue'); ?>" name="submit"></p>
    
    	</fieldset>
    
    </form>
    
    </div>
    <?php
    ?>
  • mort3n

    Hi William.

    Phew, that's a long post.

    You can use pastebin or just include the code you ask about :slight_smile:

    The point is that you can't tell from this file what that line will actually do. It is an entry point for other code to do something at that specific point in the processing of the included file.

    This 'other code' could be in the same plugin or in another. You can also use it to hook your own code into the processing of the file you pasted.

    Example
    Say you want to send a mail to the admin when the user has updated the profile. Put the code in your functions.php file in your theme. Note, the body of the function would have to be filled out to actually do something :slight_smile:

    Function william_example( $id ){
      //some code to say mail the id to admin
    }
    add_action( 'edit_user_profile_update', 'william_example' );

    Hope this helps

    Cheers
    Mort3n

  • mort3n

    Not exactly.

    Whenever a page using the template you pasted is called, all of the template code is parsed and executed. The page is built anew every time (ignoring cache for a moment). The do_action hook essentially executes all of the pieces of code that has been hooked onto it, such as the function in my example. This means that every time the page is built, hence displayed, the function you've added is run at that point in the building of the page.

    In the current case, the page concerns updating a member profile. Every time this happens the action is executed. In the example the action we've added does something, sends a notification or whatever, and then the rest of the template page executes.

    Hope this clarifies :slight_smile:

    Cheers
    Mort3n

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.