Advertisement
kingBethal

WordPress Subscriber FrontEnd Profile Page

Sep 25th, 2020 (edited)
328
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 8.33 KB | None | 0 0
  1. <?php
  2. /**
  3.  * @package
  4.  *
  5.  * SUBSCRIBER FORM
  6.  * (https://wordpress.stackexchange.com/a/9786/86024)
  7.  *
  8.  */
  9. /* Get user info. */
  10. global $current_user, $wp_roles;
  11. $current_user = wp_get_current_user();
  12. //get_currentuserinfo(); //deprecated since 3.1
  13.  
  14. $wp_http_referer = remove_query_arg(array('update', 'delete_count', 'user_id'), $wp_http_referer);
  15.  
  16. /* Load the registration file. */
  17. //require_once( ABSPATH . WPINC . '/registration.php' ); //deprecated since 3.1
  18. $error = array();
  19. /* If profile was saved, update profile. */
  20. if ('POST' == $_SERVER['REQUEST_METHOD'] && !empty($_POST['action']) && $_POST['action'] == 'update-user') {
  21.  
  22.     /* Update user password. */
  23.     if (!empty($_POST['pass1']) && !empty($_POST['pass2'])) {
  24.         if ($_POST['pass1'] == $_POST['pass2']) {
  25.             wp_update_user(array('ID' => $current_user->ID, 'user_pass' => esc_attr($_POST['pass1'])));
  26.         } else {
  27.             $error[] = __('The passwords you entered do not match.  Your password was not updated.', 'profile');
  28.         }
  29.     }
  30.  
  31.     /* Update user information. */
  32.     if (!empty($_POST['url'])) {
  33.         wp_update_user(array('ID' => $current_user->ID, 'user_url' => esc_url($_POST['url'])));
  34.     }
  35.     if (!empty($_POST['email'])) {
  36.         if (!is_email(esc_attr($_POST['email']))) {
  37.             $error[] = __('The Email you entered is not valid.  please try again.', 'profile');
  38.         } elseif (email_exists(esc_attr($_POST['email'])) != $current_user->id) {
  39.             $error[] = __('This email is already used by another user.  try a different one.', 'profile');
  40.         } else {
  41.             wp_update_user(array('ID' => $current_user->ID, 'user_email' => esc_attr($_POST['email'])));
  42.         }
  43.     }
  44.  
  45.     if (!empty($_POST['first-name'])) {
  46.         update_user_meta($current_user->ID, 'first_name', esc_attr($_POST['first-name']));
  47.     }
  48.     if (!empty($_POST['last-name'])) {
  49.         update_user_meta($current_user->ID, 'last_name', esc_attr($_POST['last-name']));
  50.     }
  51.     if (!empty($_POST['display_name'])) {
  52.         update_user_meta($current_user->ID, 'display_name', esc_attr($_POST['display_name']));
  53.     }
  54. }
  55. ?>
  56.  
  57.  
  58. <?php
  59. if (!is_user_logged_in()) {
  60.     wp_redirect(get_bloginfo('url'));
  61.     exit;
  62. }
  63. ?>
  64.  
  65. <div class="logged-in-form text-center px-0 px-md-3">
  66.  
  67.     <div class="container-fluid mb-3">
  68.         <div class="row">
  69.             <div class="col-3">
  70.                 <!-- Subscriber Avatar -->
  71.                 <img class="rounded-circle img-thumbnail mb-2" src="<?php echo get_wp_user_avatar_src(get_the_author_meta('ID')); ?>" alt="" />
  72.             </div>
  73.             <div class="col-9 text-left">
  74.                 <!-- Subscriber Name -->  
  75.                 <small class="d-block font-weight-bold">Hello</small>
  76.                 <h3 class="text-uppercase text-secondary mt-0 mb-2 font-weight-bolder"><?php echo $current_user->first_name ?></h3>
  77.                 <small class="mt-2 d-block font-weight-bold">Email: <?php echo esc_attr($current_user->user_email); ?></small>
  78.             </div>
  79.         </div>
  80.  
  81.     </div>
  82.  
  83.  
  84.     <form id="your-profile" action="<?php echo admin_url() . 'user-edit.php'; ?>" method="post" novalidate="novalidate"
  85.     <?php do_action('user_edit_form_tag'); ?>
  86.           >
  87.               <?php wp_nonce_field('update-user_' . $user_id); ?>
  88.               <?php if ($wp_http_referer) : ?>
  89.             <input type="hidden" name="wp_http_referer" value="<?php echo esc_url($wp_http_referer); ?>" />
  90.         <?php endif; ?>
  91.         <p>
  92.             <input type="hidden" name="from" value="profile" />
  93.             <input type="hidden" name="checkuser_id" value="<?php echo get_current_user_id(); ?>" />
  94.         </p>
  95.  
  96.         <div class="container-fluid">
  97.             <div class="row">
  98.  
  99.                 <!-- FISRT NAME -->
  100.                 <div class="col-12 mb-2">
  101.                     <label class="sr-only" for="first-name">First Name</label>
  102.                     <div class="input-group mb-2 mr-sm-2">
  103.                         <div class="input-group-prepend">
  104.                             <div class="input-group-text bg-light font-weight-bold">First Name</div>
  105.                         </div>
  106.                         <input id="first-name" class="form-control font-weight-bolder px-2" name="first-name" type="text" value="<?php the_author_meta('first_name', $current_user->ID); ?>" disabled="disabled"/>
  107.                     </div>
  108.                 </div>
  109.  
  110.                 <!-- LAST NAME -->
  111.                 <div class="col-12 mb-2">
  112.                     <label class="sr-only" for="last-name">Last Name</label>
  113.                     <div class="input-group mb-2 mr-sm-2">
  114.                         <div class="input-group-prepend">
  115.                             <div class="input-group-text bg-light font-weight-bold">Last Name</div>
  116.                         </div>
  117.                         <input id="last-name" class="form-control font-weight-bolder px-2" name="first-name" type="text" value="<?php the_author_meta('last_name', $current_user->ID); ?>" disabled="disabled"/>
  118.                     </div>
  119.                 </div>
  120.  
  121.                 <!-- DISPLAY NAME -->
  122.                 <div class="col-12 mb-2">
  123.                     <label class="sr-only" for="last-name">Display Name</label>
  124.                     <div class="input-group mb-2 mr-sm-2">
  125.                         <div class="input-group-prepend">
  126.                             <div class="input-group-text bg-light font-weight-bold">Nicename&nbsp;</div>
  127.                         </div>
  128.                         <select id="display_name" class="custom-select font-weight-bolder" name="display_name">
  129.                             <?php
  130.                             $public_display = array();
  131.                             $public_display['display_nickname'] = $current_user->nickname;
  132.                             $public_display['display_username'] = $current_user->user_login;
  133.  
  134.                             if (!empty($current_user->first_name)) {
  135.                                 $public_display['display_firstname'] = $current_user->first_name;
  136.                             }
  137.  
  138.                             if (!empty($current_user->last_name)) {
  139.                                 $public_display['display_lastname'] = $current_user->last_name;
  140.                             }
  141.  
  142.                             if (!empty($current_user->first_name) && !empty($current_user->last_name)) {
  143.                                 $public_display['display_firstlast'] = $current_user->first_name . ' ' . $current_user->last_name;
  144.                                 $public_display['display_lastfirst'] = $current_user->last_name . ' ' . $current_user->first_name;
  145.                             }
  146.  
  147.                             if (!in_array($current_user->display_name, $public_display, true)) { // Only add this if it isn't duplicated elsewhere.
  148.                                 $public_display = array('display_displayname' => $current_user->display_name) + $public_display;
  149.                             }
  150.  
  151.                             $public_display = array_map('trim', $public_display);
  152.                             $public_display = array_unique($public_display);
  153.  
  154.                             foreach ($public_display as $id => $item) {
  155.                                 ?>
  156.                                 <option <?php selected($current_user->display_name, $item); ?>><?php echo $item; ?></option>
  157.                                 <?php
  158.                             }
  159.                             ?>
  160.                         </select>
  161.                     </div>
  162.                 </div>
  163.  
  164.             </div>
  165.  
  166.         </div>
  167.  
  168.         <?php
  169. // action hook for plugin, custom and extra fields
  170. // do_action('edit_user_profile', $current_user);
  171.         ?>
  172.  
  173.         <div class="d-flex justify-content-center mt-2">
  174.             <button id="updateuser" name="updateuser" type="submit" class="btn btn-outline-primary font-weight-bold mr-2"/><?php _e('Update Profile', 'text_domain'); ?></button>
  175.             <?php wp_nonce_field('update-user') ?>
  176.             <a href="<?php echo wp_logout_url("index.php"); ?>"type="button" class="btn btn-outline-secondary font-weight-bold ml-2">Log Out</a>
  177.         </div>
  178.  
  179.         <input type="hidden" name="action" value="update" />
  180.         <input type="hidden" name="user_id" id="user_id" value="<?php echo esc_attr($current_user->ID); ?>" />
  181.  
  182.     </form><!-- #adduser -->
  183. </div>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement