Advertisement
Guest User

Untitled

a guest
Aug 19th, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.14 KB | None | 0 0
  1. <?php
  2. /**
  3. * Template Name: User Profile
  4. *
  5. * Allow users to update their profiles from Frontend.
  6. *
  7. */
  8.  
  9. /* Get user info. */
  10. global $current_user, $wp_roles;
  11. //get_currentuserinfo(); //deprecated since 3.1
  12.  
  13. /* Load the registration file. */
  14. //require_once( ABSPATH . WPINC . '/registration.php' ); //deprecated since 3.1
  15. $error = array();
  16. /* If profile was saved, update profile. */
  17. if ( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] ) && $_POST['action'] == 'update-user' ) {
  18.  
  19. /* Update user password. */
  20. if ( !empty($_POST['pass1'] ) && !empty( $_POST['pass2'] ) ) {
  21. if ( $_POST['pass1'] == $_POST['pass2'] )
  22. wp_update_user( array( 'ID' => $current_user->ID, 'user_pass' => esc_attr( $_POST['pass1'] ) ) );
  23. else
  24. $error[] = __('The passwords you entered do not match. Your password was not updated.', 'profile');
  25. }
  26.  
  27. /* Update user information. */
  28. if ( !empty( $_POST['url'] ) )
  29. wp_update_user( array( 'ID' => $current_user->ID, 'user_url' => esc_url( $_POST['url'] ) ) );
  30. if ( !empty( $_POST['email'] ) ){
  31. if (!is_email(esc_attr( $_POST['email'] )))
  32. $error[] = __('The Email you entered is not valid. please try again.', 'profile');
  33. elseif(email_exists(esc_attr( $_POST['email'] )) != $current_user->id )
  34. $error[] = __('This email is already used by another user. try a different one.', 'profile');
  35. else{
  36. wp_update_user( array ('ID' => $current_user->ID, 'user_email' => esc_attr( $_POST['email'] )));
  37. }
  38. }
  39.  
  40. if ( !empty( $_POST['first-name'] ) )
  41. update_user_meta( $current_user->ID, 'first_name', esc_attr( $_POST['first-name'] ) );
  42. if ( !empty( $_POST['last-name'] ) )
  43. update_user_meta($current_user->ID, 'last_name', esc_attr( $_POST['last-name'] ) );
  44. if ( !empty( $_POST['description'] ) )
  45. update_user_meta( $current_user->ID, 'description', esc_attr( $_POST['description'] ) );
  46.  
  47. /* Redirect so the page will show updated info.*/
  48. /*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 ){ */
  49. if ( count($error) == 0 ) {
  50. //action hook for plugins and extra fields saving
  51. do_action('edit_user_profile_update', $current_user->ID);
  52. wp_redirect( get_permalink() );
  53. exit;
  54. }
  55. }
  56. ?>
  57.  
  58. <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
  59. <div id="post-<?php the_ID(); ?>">
  60. <div class="entry-content entry">
  61. <?php the_content(); ?>
  62. <?php if ( !is_user_logged_in() ) : ?>
  63. <p class="warning">
  64. <?php _e('You must be logged in to edit your profile.', 'profile'); ?>
  65. </p><!-- .warning -->
  66. <?php else : ?>
  67. <?php if ( count($error) > 0 ) echo '<p class="error">' . implode("<br />", $error) . '</p>'; ?>
  68. <form method="post" id="adduser" action="<?php the_permalink(); ?>">
  69. <p class="form-username">
  70. <label for="first-name"><?php _e('First Name', 'profile'); ?></label>
  71. <input class="text-input" name="first-name" type="text" id="first-name" value="<?php the_author_meta( 'first_name', $current_user->ID ); ?>" />
  72. </p><!-- .form-username -->
  73. <p class="form-username">
  74. <label for="last-name"><?php _e('Last Name', 'profile'); ?></label>
  75. <input class="text-input" name="last-name" type="text" id="last-name" value="<?php the_author_meta( 'last_name', $current_user->ID ); ?>" />
  76. </p><!-- .form-username -->
  77. <p class="form-email">
  78. <label for="email"><?php _e('E-mail *', 'profile'); ?></label>
  79. <input class="text-input" name="email" type="text" id="email" value="<?php the_author_meta( 'user_email', $current_user->ID ); ?>" />
  80. </p><!-- .form-email -->
  81. <p class="form-url">
  82. <label for="url"><?php _e('Website', 'profile'); ?></label>
  83. <input class="text-input" name="url" type="text" id="url" value="<?php the_author_meta( 'user_url', $current_user->ID ); ?>" />
  84. </p><!-- .form-url -->
  85. <p class="form-password">
  86. <label for="pass1"><?php _e('Password *', 'profile'); ?> </label>
  87. <input class="text-input" name="pass1" type="password" id="pass1" />
  88. </p><!-- .form-password -->
  89. <p class="form-password">
  90. <label for="pass2"><?php _e('Repeat Password *', 'profile'); ?></label>
  91. <input class="text-input" name="pass2" type="password" id="pass2" />
  92. </p><!-- .form-password -->
  93. <p class="form-textarea">
  94. <label for="description"><?php _e('Biographical Information', 'profile') ?></label>
  95. <textarea name="description" id="description" rows="3" cols="50"><?php the_author_meta( 'description', $current_user->ID ); ?></textarea>
  96. </p><!-- .form-textarea -->
  97.  
  98. <?php
  99. //action hook for plugin and extra fields
  100. do_action('edit_user_profile',$current_user);
  101. ?>
  102. <p class="form-submit">
  103. <?php echo $referer; ?>
  104. <input name="updateuser" type="submit" id="updateuser" class="submit button" value="<?php _e('Update', 'profile'); ?>" />
  105. <?php wp_nonce_field( 'update-user' ) ?>
  106. <input name="action" type="hidden" id="action" value="update-user" />
  107. </p><!-- .form-submit -->
  108. </form><!-- #adduser -->
  109. <?php endif; ?>
  110. </div><!-- .entry-content -->
  111. </div><!-- .hentry .post -->
  112. <?php endwhile; ?>
  113. <?php else: ?>
  114. <p class="no-data">
  115. <?php _e('Sorry, no page matched your criteria.', 'profile'); ?>
  116. </p><!-- .no-data -->
  117. <?php endif; ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement