0

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:\xampp\htdocs\endengsms\wp-content\themes\endengsms\header.php:47) in C:\xampp\htdocs\endengsms\wp-includes\pluggable.php on line 1281

Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\endengsms\wp-content\themes\endengsms\header.php:47) in C:\xampp\htdocs\endengsms\wp-includes\pluggable.php on line 1284

  • Does it get to that point in the code? Are there any PHP errors? – mozboz Jul 20 at 9:55
  • @mozboz I put the error above in question, my friend – user191919 Jul 20 at 10:38
  • You can’t redirect after anything has been output on the page. You need to process the form before get_header(). – Jacob Peattie Jul 20 at 11:09
0

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.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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