TransWikia.com

wp_redirect not working after update_user_meta

WordPress Development Asked on October 30, 2021

I have create a custom page to make users able to edit their profiles. every thing is OK but I do not know really why wp_redirect does not work???
.
my edit-profile-proccess.php
.

<?php

$user = wp_get_current_user();
$userID = $user->ID;

$has_error = false;
$has_success = false;
$message = array();
    
if(isset($_POST['edpr_profile_submit'])){
    if(! isset($_POST['security']) || ! wp_verify_nonce($_POST['security'],'edit-profile-nonce')){
        print('<p">be careful</p>');
    }else{
            
        $firstnameuser = sanitize_text_field($_POST['edpr_firstname']);
        $lastnameuser = sanitize_text_field($_POST['edpr_lastname']);
        
        
        //if( empty($firstnameuser) || empty($lastnameuser) || empty($emailuser) || empty($cellphoneuser) || $genderuser == 'nonete'  ){
        if( empty($firstnameuser) || empty($lastnameuser)  ){
            
            $has_error = true;
            $message[] = "fill all fields";
            
        }
        
        //if(!$has_error) {
        else {
            
            update_user_meta($userID,'first_name',$firstnameuser);
            update_user_meta($userID,'last_name',$lastnameuser);

            //$has_success = true;
            //$message[] = "updated successfully";
            
            $redirecturlll = site_url('/edit-profile/?editprofile=true');
            wp_redirect( $redirecturlll );
            exit;
               
            
        }
    
    
    }
}




// Never Delete The Following Line:
$usermeta = get_user_meta($userID);

.
my edit-profile.php
.

<?php /* Template Name: edit-profile */ ?>

<?php get_header(); ?>
        
<div class="mysignuppagebody">
        
<?php if ( !is_user_logged_in() ) { ?>
    
    <section class="top_message">
        <p>do login</p>
    </section>

<?php } else { ?>

    <?php include get_template_directory() . '/templates/edit-profile-proccess.php'; ?>
    
    <div class="mysignuppage">
                
        <?php if( $has_error ){ ?>
            <div class="myloginpage_message error">
                <?php foreach ($message as $item) { ?>
                <p><?php echo $item; ?></p>
                <?php } ?>
            </div>
        <?php } ?>
        <?php if( $has_success ){ ?>
            <div class="myloginpage_message success">
                <?php foreach ($message as $sitem) { ?>
                <p><?php echo $sitem; ?></p>
                <?php } ?>
            </div>
        <?php } ?>

        <form action="<?php echo get_permalink(); ?>" method="post" enctype="multipart/form-data" class="">
            
            <?php wp_nonce_field('edit-profile-nonce', 'security'); ?>

            <input type="text" value="<?php echo esc_attr($usermeta['first_name'][0]); ?>" name="edpr_firstname" required placeholder="first name *">
                
            <input type="text" value="<?php echo esc_attr($usermeta['last_name'][0]); ?>" name="edpr_lastname" required placeholder="last name *">

            <input type="submit" value="edit profile" name="edpr_profile_submit">

        </form>

    </div>
<?php } ?>  

</div>
        
        
<?php get_footer(); ?>  

.
the error I see after click on submit
.

Warning: Cannot modify header information – headers already sent by (output started at C:xampphtdocsendengsmswp-contentthemesendengsmsheader.php:47) in C:xampphtdocsendengsmswp-includespluggable.php on line 1281

Warning: Cannot modify header information – headers already sent by (output started at C:xampphtdocsendengsmswp-contentthemesendengsmsheader.php:47) in C:xampphtdocsendengsmswp-includespluggable.php on line 1284

One Answer

wp_redirect sends a HTTP header to the browser. Once HTTP headers are sent, that's it, they're gone, you can't add anymore.

However, you called get_header and rendered some HTML. To do this headers need to be sent to the browser telling it to expect a HTML page.

So by the time you load edit-profile-proccess.php, it's too late. Headers have already been sent, you can't add the redirect header. It's already happened, the horse has already bolted out the stable. Time travel would be necessary.

That's why you're getting PHP warning messages about this instead of the redirect you expected. Notice that they tell you exactly whata the problem is, they even tell you where the first output occurred, the file and line it happened in.

Instead, you need to perform this check and redirect before any output happens. If even a single space character or tag is output then the headers are sent, so the order you do this in is critical, and non-negotiable.

Answered by Tom J Nowell on October 30, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP