Ok so I used to develop in Wordpress a lot but in the last year have been using Codeigniter instead. Obviously there are times when Codeigniter is more applicable to a situation than Wordpress and vice versa, but I've had to come back to use Wordpress for my next project.
With MVC it makes so much sense to have all the "echoing" in the views and haver all the processing in the controllers etc.. but now coming back to Wordpress I feel odd echoing inside a function.
I am making a registration page (most of the code is from here but I am yet to change it to my custom fields etc) where the HTML is in the template page with a do_action ('process_customer_registration_form');
and all the processing AND ECHOING is done in function.php:
function registration_process_hook() {
if (isset($_POST['adduser']) && isset($_POST['add-nonce']) && wp_verify_nonce($_POST['add-nonce'], 'add-user')) {
// die if the nonce fails
if ( !wp_verify_nonce($_POST['add-nonce'],'add-user') ) {
wp_die('Sorry! That was secure, guess you\'re cheatin huh!');
} else {
// auto generate a password
$user_pass = wp_generate_password();
// setup new user
$userdata = array(
'user_pass' => $user_pass,
'user_login' => esc_attr( $_POST['user_name'] ),
'user_email' => esc_attr( $_POST['email'] ),
'role' => get_option( 'default_role' ),
);
// setup some error checks
if ( !$userdata['user_login'] )
$error = 'A username is required for registration.';
elseif ( username_exists($userdata['user_login']) )
$error = 'Sorry, that username already exists!';
elseif ( !is_email($userdata['user_email'], true) )
$error = 'You must enter a valid email address.';
elseif ( email_exists($userdata['user_email']) )
$error = 'Sorry, that email address is already used!';
// setup new users and send notification
else{
$new_user = wp_insert_user( $userdata );
wp_new_user_notification($new_user, $user_pass);
}
}
}
if ( $new_user ) : ?>
<p class="alert"><!-- create and alert message to show successful registration -->
<?php
$user = get_user_by('id',$new_user);
echo 'Thank you for registering ' . $user->user_login;
echo '<br/>Please check your email address. That\'s where you\'ll recieve your login password.<br/> (Be sure to check your spam folder)';
?>
</p>
<?php else : ?>
<?php if ( $error ) : ?>
<p class="error"><!-- echo errors if users fails -->
<?php echo $error; ?>
</p>
<?php endif; ?>
<?php endif;
}
add_action('process_customer_registration_form', 'registration_process_hook');
My question is... is it best practice in WP to have content echoed within an action function like it is at the end if this one? Why not just have a standard if(isset(submit)) situation and process the form in the same page? I know thatby moving it to the functions its seperating the logic, but it's also echoing a response within the function.. Doesn't seem right.
Thanks