Php 在前端创建高级/可编辑wordpress配置文件页面。

Php 在前端创建高级/可编辑wordpress配置文件页面。,php,wordpress,frontend,user-profile,Php,Wordpress,Frontend,User Profile,我花了好几天的时间编写一个前端用户配置文件页面,登录的用户可以编辑他们的数据,但似乎没有正确的方法。在网络上,似乎没有人在不使用插件的情况下解释一种安全且良好的编码方法 因此,我创建了一个名为profile-page.php的文件 <?php /* * Template Name: User Profile * Allow users to update their profiles from Frontend. */ global $current_user, $wp_roles;

我花了好几天的时间编写一个前端用户配置文件页面,登录的用户可以编辑他们的数据,但似乎没有正确的方法。在网络上,似乎没有人在不使用插件的情况下解释一种安全且良好的编码方法

因此,我创建了一个名为profile-page.php的文件

<?php

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


global $current_user, $wp_roles;
get_currentuserinfo();



/* Someone do not use global variable
   but this code: (which one is better?)

   require_once( ABSPATH . 'wp-admin/includes/user.php' );
   require_once( ABSPATH . 'wp-admin/includes/misc.php' );
   define( 'IS_PROFILE_PAGE', true );
   wp_enqueue_script( 'user-profile' );
   $current_user = wp_get_current_user();
*/





if ( 'POST' == $_SERVER['REQUEST_METHOD'] ) {


   check_admin_referer( 'update-user_' . $current_user->ID );


   if ( ! current_user_can( 'edit_user', $current_user->ID ) )
   wp_die( __( 'Error Message' ) );


   $errors = edit_user( $current_user->ID );

   // Save
   if ( ! is_wp_error( $errors ) ) {

      $message = __( '<strong>Success</strong>: Profile updated', '' );
      $style = 'success';

      do_action( 'personal_options_update', $current_user->ID ); // Save all data in db
      wp_redirect( get_permalink() );

      exit;

   // Error
   } else {

      $message = $errors->get_error_message();
      $style = 'error';

   }
}


get_header();

?>


<div id="profile-user">



<!-- welcome message -->  
<?php if ( !is_user_logged_in() ) : ?>
     <p class="warning">
        <?php _e('You must log in to edit.', 'textdomain'); ?>
     </p>
  <?php else : ?>
  <?php echo '<p>Welcome <strong>'. $current_user->first_name .'</strong></p>'; ?>



<!-- error/success message -->
<?php
if ( isset( $message ) ) {
   echo '<div class="' . $style . '">' . $message . '</div>';
} ?>




<form class="" action="<?php the_permalink(); ?>" method="post" id="your-profile">



    <?php wp_nonce_field( 'update-user_' . $current_user->ID ); ?>

    <input type="hidden" name="from" value="profile" />
    <input type="hidden" name="checkuser_id" value="<?php echo $current_user->ID; ?>" />




    <!-- HERE ALL MY FORM INPUT (I show you only one for exemple) -->

    <input type="text" name="first_name" id="input_01" value="<?php echo esc_attr( get_the_author_meta( 'first_name', $user->ID ) ); ?>" />




    <input type="hidden" name="action" value="profile" />
    <input type="hidden" name="user_id" id="user_id" value="<?php echo esc_attr( $current_user->ID ); ?>" />

    <input type="submit" class="button-primary" value="<?php esc_attr_e( 'Save' ); ?>" name="submit" />



</form>


</div>


<?php get_sidebar(); ?>
<?php get_footer(); ?>


我认为这个问题(关于通用WP概念,而不是代码)比这里更适合。你也可以试一下-所以更多的是针对具体问题,而你正在寻找代码审查。谢谢!我要去那里试试,奥尔索!