我正在WordPress中创建一个自定义表单来存储数据并将其发送到外部数据库。作为测试的一部分,我成功地创建了一个表单,通过单击submit按钮将表单引用回自身,将表单数据提交到外部数据库。
我现在想利用WordPress管理帖子功能,而不是让表单引用回自身。我已经正确设置了操作挂钩,因为在提交表单后,我会使用以下方式显示$\\u POST变量:
<form action="<?php echo esc_url( admin_url( \'admin-post.php\' ) ); ?>" method="post" id="test" >
我的问题是,外部数据库是否可以用于管理帖子?此时,当为外部数据库连接引用全局变量时,会抛出一个PHP错误,说明insert引用的是空值。以下是与此问题相关的代码。。。
<?php
class x94 {
public function __construct(){
add_action( \'admin_post_submitForm9\', array( $this, \'formHandling\' ), 11, 1 );
add_action( \'wp_head\', array( $this, \'externalDB\' ), 10, 1 );
}
//External DB connection
function externalDB(){
global $externalDB;
$externalDB = new wpdb(DB_USER2, DB_PASSWORD2, DB_NAME3, DB_HOST2);
}
//create form
function userForm() {
//Global Variables
global $externalDB;
//print_r($externalDB);
// starts output buffering
ob_start();
?>
<form action="<?php echo esc_url( admin_url( \'admin-post.php\' ) ); ?>" method="post" id="test" >
<input type="hidden" name="action" id="userForm_action" value="submitForm9" />
<input type="text" name="visitor_name" id="visitor_name" />
<input type="text" name="visitor_age" id="visitor_age" />
<input type="text" name="visitor_gender" id="visitor_gender" />
<input type="submit" name="submit_form" value="submit" />
</form>
<?php
$html = ob_get_clean();
if ( isset( $_POST["submit_form"] ) && $_POST["visitor_name"] != "" ) {
//$_POST Variables
$name = strip_tags($_POST["visitor_name"], "");
$age = $_POST[\'visitor_age\'];
$gender = $_POST[\'visitor_gender\'];
}
// if the form is submitted but the name is empty
if ( isset( $_POST["submit_form"] ) && $_POST["visitor_name"] == "" )
$html .= "<p>You need to fill the required fields.</p>";
// outputs everything
return $html;
}
function formHandling(){
global $externalDB;
print_r($externalDB); //not displaying data, global not recognized
print_r($_POST); //displays data
if (1 == 1){
$externalDB->insert(
\'basic_user_info\',
array(
\'name\' => $name,
\'age\' => $age,
\'gender\'=> $gender,
),
//field formats
array(\'%s\',
\'%s\',
\'%s\',
)
);
//$html = "<p>Your name <strong>$name</strong> was successfully recorded. Thanks!!</p>";
die();
}
}
}//end class
//shortcodes
add_shortcode(\'basic-info\', array( \'x94\', \'userForm\' ) );
new x94();
?>
SO网友:29A
我回答这个问题是为了帮助未来的WordPress开发人员寻求知识。答案是肯定的,您可以在使用admin_post action. 以下是更正的来源。。。
<?php
class x94 {
private $externalDB01;
public function __construct(){
add_action( \'admin_post_submitForm9\', array( $this, \'formHandling\' ), 11, 1 );
add_action( \'plugins_loaded\', array( $this, \'externalDB\' ), 10, 1 );
}
//External DB connection
function externalDB(){
$this->externalDB01 = new wpdb(DB_USER2, DB_PASSWORD2, DB_NAME3, DB_HOST2);
}
//create form
function userForm() {
ob_start();
?>
<form action="<?php echo esc_url( admin_url( \'admin-post.php\' ) ); ?>" method="post" id="test" >
<input type="hidden" name="action" id="userForm_action" value="submitForm9" />
<input type="text" name="visitor_name" id="visitor_name" />
<input type="text" name="visitor_age" id="visitor_age" />
<input type="text" name="visitor_gender" id="visitor_gender" />
<input type="submit" name="submit_form" value="submit" />
</form>
<?php
$html = ob_get_clean();
return $html;
}
function formHandling(){
if ( isset( $_POST) ) {
//sanatize data from the admin-post array
$name = sanitize_text_field( $_POST[\'vistor_name\'] );
$age = sanitize_text_field( $_POST[\'vistor_age\'] );
$gender = sanitize_text_field( $_POST[\'vistor_gender\'] );
//submit data to external database
$externalDB->insert(
\'basic_user_info\',
array(
\'name\' => $name,
\'age\' => $age,
\'gender\'=> $gender,
),
//field formats
array(\'%s\',
\'%s\',
\'%s\',
)
);
}
else {
wp_die();
}
}
}//end class
//shortcodes
add_shortcode(\'basic-info\', array( \'x94\', \'userForm\' ) );
new x94();
?>