2012-08-04 43 views
-2

我在我的wordpress网站有注册表单。以下是我用于注册用户的代码。删除引号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插入到posts表中。我必须删除posts表中的引号。怎么做?

回答

1

您可以删除引号或者str_replace()函数

匹配任何字符到你的例子:

$post['post_name'] = str_replace("'", "", $username); 
+0

感谢。它正在工作。 – designersvsoft 2012-08-04 07:58:45

+0

我很高兴我能帮助你。但也许你应该搜索一些正则表达式的例子来删除一行代码中的其他'微妙'字符。 – Ello 2012-08-04 08:01:07

+0

当然。感谢您的建议。 – designersvsoft 2012-08-04 09:04:41