2012-08-04 42 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