I am creating a custom form in WordPress to store and send data to an external database. As part of testing, I was successful in creating a form that submitted form data to the external database by having the form refer back to itself upon clicking the submit button.
I now would like to leverage WordPress admin-post functionality instead of having the form refer back to itself. I have set up the action hook correctly as I see the $_POST variables displayed once the form is submitted using:
<form action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>" method="post" id="test" >
My question, can an external database be used with admin-post? At this point, when referencing the global variable for the external database connection, a PHP error is thrown stating that insert is referring to a NULL value. Below is relevant code pertaining to the issue...
<?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();
?>
add_action( 'wp_head', array( $this, 'externalDB' ), 10, 1 );
- how about replace that with$this->externalDB();
? Or if you want to wait for plugins to be loaded, replacewp_head
withplugins_loaded
.x94
class, why not store thewpdb
instance (which is currently the global$externalDB
variable) in a property of the class? Like this: addprivate $db;
then in theexternalDB()
function, use$this->db = new wpdb(DB_USER2, DB_PASSWORD2, DB_NAME3, DB_HOST2);
, then remove theglobal $externalDB;
and change the$externalDB
to$this->db
. (And renameexternalDB()
todb()
..)