-2

我的 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 ); 

注册后,我也尝试将用户插入帖子表中。如果我输入了用户名test'name,它将像testname在用户表中一样插入,但它将像test\'name在帖子表中一样插入。我也必须删除帖子表中的引号。怎么做?

4

1 回答 1

1

您可以使用 str_replace() 删除引号或任何字符

与您的示例匹配:

$post['post_name'] = str_replace("'", "", $username);
于 2012-08-04T07:42:51.073 回答