我的wordpress网站上有一张注册表。以下是我用于注册用户的代码。
$username = $wpdb->escape(trim($_POST['txtFullName']));
$email = $wpdb->escape(trim($_POST['txtEmail']));
$password = $wpdb->escape(trim($_POST['txtPassword']));
$confirmpassword = $wpdb->escape(trim($_POST['txtConfirmPassword']));
gender = $wpdb->escape(trim($_POST['gender']));
$user_id = wp_insert_user( array ('user_pass' => apply_filters('pre_user_user_pass', $password), 'user_login' => apply_filters('pre_user_user_login', $username), 'user_email' => apply_filters('pre_user_user_email', $email), 'role' => 'author' ) );
$authid = $user_id;
do_action('user_register', $user_id);
// Insert into Post table
$post = array();
$post['post_author'] = $authid ;
$post['post_date'] = date('Y-m-d H:i:s') ;
$post['post_date_gmt'] = date('Y-m-d H:i:s') ;
$post['post_name'] = $username ;
$post['post_status'] = 'pending' ;
$post['post_title'] = $username ;
$post['post_type'] = 'post';
$post['post_category'] = $gender;
wp_insert_post( $post, $wp_error );
我正在尝试在注册后将用户插入posts表中。如果我输入了像test'name
这样的用户名,它将作为testname
插入到用户表中,但它将作为test\'name
插入到posts表中。我也必须删除posts表中的引号。怎么做?
答案 0 :(得分:1)
您可以使用str_replace()
删除引号或任何字符匹配您的示例:
$post['post_name'] = str_replace("'", "", $username);