从表单
-
-
@William,您好,您的问题有点广泛,因为它不是真正的WordPress特定的,而是PHP的一般知识.请先检查HTML和PHP的表单结构-之后,您可以使用`wp_update_user($ user)`函数来实现.Hi @William, your question is a bit broad as it is not really WordPress specific, but also general PHP knowledge. Please check the HTML & PHP formbuilding first - afterwards you can use the `wp_update_user($user)` function to achieve that.
- 0
- 2012-12-11
- fischi
-
1 个回答
- 投票数
-
- 2012-12-11
使用
wp-admin/admin-post.php
作为表单操作处理程序,并将您的自定义函数绑定为该函数的回调.一个简单的电子邮件更新示例.我们将在此处使用名为
[userform]
的简码,但是您也可以使用模板.add_shortcode( 'userform', 'wpse_75723_userform' ); add_action( 'admin_post_update_user_email', 'wpse_75723_update' ); /** * Create the form. */ function wpse_75723_userform() { $here = esc_url( home_url( $_SERVER['REQUEST_URI'] ) ); if ( ! is_user_logged_in() ) return 'You have to <a href="' . wp_login_url( $here ) . '">log in</a> to use this page.'; $action = admin_url( 'admin-post.php'); $user_id = get_current_user_id(); return "<form method='post' action='$action'> <input type='hidden' name='action' value='update_user_email'> <input type='hidden' name='redirect' value='$here'> <input type='hidden' name='user_id' value='$user_id'> <input type='email' name='email' size='15'> <input type='submit'> </form>"; } /** * Update user email */ function wpse_75723_update() { if ( ! isset ( $_POST['user_id'] ) ) die( 'no id' ); $user_id = absint( $_POST['user_id'] ); if ( ! current_user_can( 'edit_user', $user_id ) ) die( 'not allowed' ); if ( ! isset ( $_POST['email'] ) ) die( 'no email' ); if ( ! is_email( $_POST['email'] ) ) die( 'invalid email' ); $user = get_userdata( $user_id ); if ( empty ( $user->user_login ) ) die( 'user denied' ); global $wpdb; $wpdb->query( $wpdb->prepare( "UPDATE {$wpdb->users} SET user_email = %s WHERE user_login = %s", $_POST['email'], $user->user_login ) ); $location = isset ( $_POST['redirect'] ) ? urldecode( $_POST['redirect'] ) : home_url( '/' ); wp_redirect( $location, 303 ); exit; }
插入...
[userform]
…进入页面将产生一种基本形式:
用户可以在此处更改其电子邮件地址.
要了解可用的变量及其存储位置,请查看以下文件:
-
wp-admin/user-edit.php
-
wp-admin/includes/user.php
和 -
wp-includes/user.php
如果要发送普通的SQL查询,表
users
和user_meta
也值得一看.Use
wp-admin/admin-post.php
as form action handler, and bind your custom function as callback to that.A simple example for email updates. We will use a shortcode named
[userform]
here, but you can use a template too.add_shortcode( 'userform', 'wpse_75723_userform' ); add_action( 'admin_post_update_user_email', 'wpse_75723_update' ); /** * Create the form. */ function wpse_75723_userform() { $here = esc_url( home_url( $_SERVER['REQUEST_URI'] ) ); if ( ! is_user_logged_in() ) return 'You have to <a href="' . wp_login_url( $here ) . '">log in</a> to use this page.'; $action = admin_url( 'admin-post.php'); $user_id = get_current_user_id(); return "<form method='post' action='$action'> <input type='hidden' name='action' value='update_user_email'> <input type='hidden' name='redirect' value='$here'> <input type='hidden' name='user_id' value='$user_id'> <input type='email' name='email' size='15'> <input type='submit'> </form>"; } /** * Update user email */ function wpse_75723_update() { if ( ! isset ( $_POST['user_id'] ) ) die( 'no id' ); $user_id = absint( $_POST['user_id'] ); if ( ! current_user_can( 'edit_user', $user_id ) ) die( 'not allowed' ); if ( ! isset ( $_POST['email'] ) ) die( 'no email' ); if ( ! is_email( $_POST['email'] ) ) die( 'invalid email' ); $user = get_userdata( $user_id ); if ( empty ( $user->user_login ) ) die( 'user denied' ); global $wpdb; $wpdb->query( $wpdb->prepare( "UPDATE {$wpdb->users} SET user_email = %s WHERE user_login = %s", $_POST['email'], $user->user_login ) ); $location = isset ( $_POST['redirect'] ) ? urldecode( $_POST['redirect'] ) : home_url( '/' ); wp_redirect( $location, 303 ); exit; }
Inserting …
[userform]
… into a page will produce a basic form:
The user can change her/his email address here.
To understand what variables are available and where they are stored look at these files:
wp-admin/user-edit.php
wp-admin/includes/user.php
andwp-includes/user.php
The tables
users
anduser_meta
are worth a look too if you want to send plain SQL queries.-
我在任何地方都找不到此钩子" admin_post_update_user_email".你从哪里得到的?I can't find this hook 'admin_post_update_user_email' anywhere. Where do you get it from?
- 0
- 2013-09-21
- Ari
-
@AriSusanto这是`admin_post_`和`
`的值的组合. @AriSusanto This is a combination of `admin_post_` and the value of ``.- 0
- 2013-09-21
- fuxia
-
如何在其中添加其他有效的输入字段,例如用户的网站等?How to add another working input fields to it such as user's website etc?
- 0
- 2013-09-21
- Ari
-
只需将其写入" wpse_75723_userform()",然后在" wpse_75723_update()"中读取即可.我不明白这个问题.Just write it into `wpse_75723_userform()` and read it in `wpse_75723_update()`. I don’t understand this question.
- 0
- 2013-09-21
- fuxia
-
是的,它只是在工作.我使用wp_update_user来更新其他用户配置文件输入字段.Yes, it is simply working. I use `wp_update_user` to update additional user profile input field.
- 0
- 2013-09-22
- Ari
我正在尝试创建一个表单,该表单将允许用户在前端更新其信息.我刚刚开始学习PHP(我非常了解C#).如果我使用此代码在WordPress页面上创建表单,当用户按下"更新"按钮然后更新数据库时如何从用户那里获取信息?