সামনের প্রান্তে কোনও ব্যবহারকারী প্রোফাইল কীভাবে সম্পাদনা করবেন?


75

আমি কীভাবে কোনও ফর্মের সাথে সামনের প্রান্তে কোনও ব্যবহারকারী প্রোফাইল সম্পাদনা করতে পারি?
প্রথম নাম, পদবি, ব্যবহারকারীর নাম, ইমেল ঠিকানা এবং পাসওয়ার্ড

উত্তর:


98

আপনি আপনার থিমের পেজ.পিএপি অনুলিপি করে ব্যবহারকারী-প্রোফাইল.php এর মতো নতুন কিছুতে অনুলিপি করে এই কোডের একেবারে শীর্ষে যুক্ত করতে পারেন:

<?php
/**
 * Template Name: User Profile
 *
 * Allow users to update their profiles from Frontend.
 *
 */

/* Get user info. */
global $current_user, $wp_roles;
//get_currentuserinfo(); //deprecated since 3.1

/* Load the registration file. */
//require_once( ABSPATH . WPINC . '/registration.php' ); //deprecated since 3.1
$error = array();    
/* If profile was saved, update profile. */
if ( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] ) && $_POST['action'] == 'update-user' ) {

    /* Update user password. */
    if ( !empty($_POST['pass1'] ) && !empty( $_POST['pass2'] ) ) {
        if ( $_POST['pass1'] == $_POST['pass2'] )
            wp_update_user( array( 'ID' => $current_user->ID, 'user_pass' => esc_attr( $_POST['pass1'] ) ) );
        else
            $error[] = __('The passwords you entered do not match.  Your password was not updated.', 'profile');
    }

    /* Update user information. */
    if ( !empty( $_POST['url'] ) )
        wp_update_user( array( 'ID' => $current_user->ID, 'user_url' => esc_url( $_POST['url'] ) ) );
    if ( !empty( $_POST['email'] ) ){
        if (!is_email(esc_attr( $_POST['email'] )))
            $error[] = __('The Email you entered is not valid.  please try again.', 'profile');
        elseif(email_exists(esc_attr( $_POST['email'] )) != $current_user->id )
            $error[] = __('This email is already used by another user.  try a different one.', 'profile');
        else{
            wp_update_user( array ('ID' => $current_user->ID, 'user_email' => esc_attr( $_POST['email'] )));
        }
    }

    if ( !empty( $_POST['first-name'] ) )
        update_user_meta( $current_user->ID, 'first_name', esc_attr( $_POST['first-name'] ) );
    if ( !empty( $_POST['last-name'] ) )
        update_user_meta($current_user->ID, 'last_name', esc_attr( $_POST['last-name'] ) );
    if ( !empty( $_POST['description'] ) )
        update_user_meta( $current_user->ID, 'description', esc_attr( $_POST['description'] ) );

    /* Redirect so the page will show updated info.*/
  /*I am not Author of this Code- i dont know why but it worked for me after changing below line to if ( count($error) == 0 ){ */
    if ( count($error) == 0 ) {
        //action hook for plugins and extra fields saving
        do_action('edit_user_profile_update', $current_user->ID);
        wp_redirect( get_permalink() );
        exit;
    }
}
?>

তারপরে এই পৃষ্ঠার লুপটি প্রতিস্থাপন করুন:

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <div id="post-<?php the_ID(); ?>">
        <div class="entry-content entry">
            <?php the_content(); ?>
            <?php if ( !is_user_logged_in() ) : ?>
                    <p class="warning">
                        <?php _e('You must be logged in to edit your profile.', 'profile'); ?>
                    </p><!-- .warning -->
            <?php else : ?>
                <?php if ( count($error) > 0 ) echo '<p class="error">' . implode("<br />", $error) . '</p>'; ?>
                <form method="post" id="adduser" action="<?php the_permalink(); ?>">
                    <p class="form-username">
                        <label for="first-name"><?php _e('First Name', 'profile'); ?></label>
                        <input class="text-input" name="first-name" type="text" id="first-name" value="<?php the_author_meta( 'first_name', $current_user->ID ); ?>" />
                    </p><!-- .form-username -->
                    <p class="form-username">
                        <label for="last-name"><?php _e('Last Name', 'profile'); ?></label>
                        <input class="text-input" name="last-name" type="text" id="last-name" value="<?php the_author_meta( 'last_name', $current_user->ID ); ?>" />
                    </p><!-- .form-username -->
                    <p class="form-email">
                        <label for="email"><?php _e('E-mail *', 'profile'); ?></label>
                        <input class="text-input" name="email" type="text" id="email" value="<?php the_author_meta( 'user_email', $current_user->ID ); ?>" />
                    </p><!-- .form-email -->
                    <p class="form-url">
                        <label for="url"><?php _e('Website', 'profile'); ?></label>
                        <input class="text-input" name="url" type="text" id="url" value="<?php the_author_meta( 'user_url', $current_user->ID ); ?>" />
                    </p><!-- .form-url -->
                    <p class="form-password">
                        <label for="pass1"><?php _e('Password *', 'profile'); ?> </label>
                        <input class="text-input" name="pass1" type="password" id="pass1" />
                    </p><!-- .form-password -->
                    <p class="form-password">
                        <label for="pass2"><?php _e('Repeat Password *', 'profile'); ?></label>
                        <input class="text-input" name="pass2" type="password" id="pass2" />
                    </p><!-- .form-password -->
                    <p class="form-textarea">
                        <label for="description"><?php _e('Biographical Information', 'profile') ?></label>
                        <textarea name="description" id="description" rows="3" cols="50"><?php the_author_meta( 'description', $current_user->ID ); ?></textarea>
                    </p><!-- .form-textarea -->

                    <?php 
                        //action hook for plugin and extra fields
                        do_action('edit_user_profile',$current_user); 
                    ?>
                    <p class="form-submit">
                        <?php echo $referer; ?>
                        <input name="updateuser" type="submit" id="updateuser" class="submit button" value="<?php _e('Update', 'profile'); ?>" />
                        <?php wp_nonce_field( 'update-user' ) ?>
                        <input name="action" type="hidden" id="action" value="update-user" />
                    </p><!-- .form-submit -->
                </form><!-- #adduser -->
            <?php endif; ?>
        </div><!-- .entry-content -->
    </div><!-- .hentry .post -->
    <?php endwhile; ?>
<?php else: ?>
    <p class="no-data">
        <?php _e('Sorry, no page matched your criteria.', 'profile'); ?>
    </p><!-- .no-data -->
<?php endif; ?>

এবং আপনার যা করার বাকি রয়েছে তা হল একটি নতুন পৃষ্ঠা তৈরি করা এবং পৃষ্ঠার টেমপ্লেট হিসাবে "ব্যবহারকারী প্রোফাইল" নির্বাচন করুন।

এখন যদি এই সমস্ত কিছু খুব বেশি হয় তবে আপনি এমন কিছু প্লাগইন ব্যবহার করতে পারেন যা আপনার পছন্দ মত কঠোর পরিশ্রম করে:


2
আসলে, আমার মনে হয় আমি এটি কাজ করেছিলাম, এটা কি গ্রহণযোগ্য? আমি আপনার কোডটি রূপান্তরিত করেছি: if ( !$error ) { wp_redirect( get_permalink() .'?updated=true' ); exit; } এবং তারপরে <? Php যুক্ত হলে (added _GET ['আপডেট'] == 'সত্য'):? <p> আপনার প্রোফাইল আপডেট হয়েছে </ p> <? Php endif; ?>
এন্ড্রু

5
@ নিকোলা, আমারও একই সমস্যা ছিল। ইমেল এবং ওয়েবসাইটের ইউআরএল ব্যবহারকারী মেটা না হওয়ার কারণ এটি; সেগুলি wp_usersটেবিলের মধ্যে সংরক্ষণ করা হয় (বনাম wp_usermetaটেবিল)। ইমেল এবং ইউআরএল আপডেট করতে, আপনার wp_update_user()পরিবর্তে ব্যবহার করা দরকার update_usermeta()
রায়ান

1
@ ব্যান্ডনর্যান্ডন আমি একটি সাধারণ ইমেল বৈধতা, ডুপ্লিকেশন চেক সহ কোডটি আপডেট করেছি এবং প্লাগইন একীকরণের অনুমতি দেওয়ার জন্য ওয়ার্ডপ্রেস ব্যবহার করে এমন কিছু ক্রিয়াকলাপ যুক্ত করেছি। ধন্যবাদ
বাইনারনেট

1
এই @ বেনটারনেট পড়ুন দয়া করে। আমি শেষ হয়ে গিয়ে অবাক হইনি এবং তাই আপনার অবতার
raam86

1
@ বেনারনেট কোডের সুন্দর অংশটির জন্য আপনাকে ধন্যবাদ। কিছু সংশোধন সম্পাদনা করেছে এবং কিছু অবহেলিত লাইন মন্তব্য করেছে।
মায়নুল ইসলাম
আমাদের সাইট ব্যবহার করে, আপনি স্বীকার করেছেন যে আপনি আমাদের কুকি নীতি এবং গোপনীয়তা নীতিটি পড়েছেন এবং বুঝতে পেরেছেন ।
Licensed under cc by-sa 3.0 with attribution required.