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
                $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 -->
        $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)';
    <?php else : ?>

        <?php if ( $error ) : ?>
            <p class="error"><!-- echo errors if users fails -->
                <?php echo $error; ?>
        <?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.


1 Answer 1


The basic building block of WordPress templating is template tag, that is function echoing (the_*()) or returning (get_*()) specific information. It's no that far off from other kinds of templating, the largest difference is that WP "context" is typically much larger, way less contained, and rarely controlled by developer directly much.

Form workflows in WordPress are historically very messy, with large volumes of historical code and APIs that make everyone groan (like Settings API).

In a nutshell your goal should be to engineer it in a way that is convenient and maintainable for you, since WP offers little guidance for it.

Answering the direct part of question — yes, it is perfectly normal for WP functions to echo, either called directly in template tag fashion or being used in hooks, meant for additional output.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.