WordPress AJAX: Updating wp theme options table as well $wpdb (external DB)

Hy Guys,

I have a wordpress theme admin page which stores most theme options in wordpress’s wp-option table, and some admin form meta info stored in a custom database (outside wordpress). External DB conn info and sql queries for custom db is included (<?php include('db_include.php'); ?>) in the main theme-option.php. The structure is shown in the image below:

enter image description here

The submit button (shown in the image) updates the theme settings as well as form meta in their resp db’s via AJAX. While the WP options tables are updated successfully (with theme settings) however form meta values via $wpdb object instance($externalQuery) in the same update function (that updates wp-option table) does not work. Infact it does nothing.

Can somebody help me get this to work. I would like to via AJAX have both “wp_options” table as well as external db table on clicking “Submit” button

Here is my Code:


/* DB Config Values*/
$db_host = 'data';
$db_user = 'data';
$db_pass = 'data';

$db_name = 'ads_meta';
$table_name = 'ads_info';
$ad_id = 1;

/* Connect to the new database */
$externalQuery = new wpdb($db_user, $db_pass, $db_name, $db_host);

$select_row = "SELECT ad_name, ad_time FROM $table_name WHERE ad_id='$ad_id'";

$update_row = "UPDATE $table_name SET ad_name='test' WHERE ad_id='$ad_id'";

$del_row = "DELETE FROM $table_name WHERE ad_id='$ad_id'";


function theme_settings() {
//Get Theme Options from wp_option's in an array
$ad_elements = get_option('ad_elements');

//External DB included - gets form meta values

<form id="theme_options"> 
AD Line 1: <input type="text" id="ad_line1" name="ad_line1" value="<?php echo $ad_elements["ad_line1"];?>" />

AD Line 2: <input type="text" id="ad_line2" name="ad_line2" value="<?php echo $ad_elements["ad_line2"];?>" />

<!--FORM META -->
$newQuery = $externalQuery->get_results($select_row) or die(mysql_error());
<?php foreach($newQuery as $q){ ?>

<form id="form_meta"> 
AD Name: <input type="text" id="ad_name" name="ad_name" value="<?php echo $q->ad_name; ?>" />

AD Time: <input type="text" id="ad_time" name="ad_time" value="<?php echo $q->ad_time; ?>" />

<input type="submit" id="submit_options" />
<div id="save_msg" style="display:none"> </div>


add_action('admin_footer', 'update_init');

function update_init(){ ?>
    <script type="text/javascript">
        jQuery(document).ready(function($) {

            $('#submit_options').live('click', function(){
                var data = {
                    action: 'ad_settings_action',

                    ad_line1: $('#ad_line1').val(),
                    ad_line2: $('#ad_line2').val(),

                $.post(ajaxurl, data, function (response) {
                    .success(function () {})
                    .error(function() { alert("error");})
                    .complete(function() {$('#save_msg').html('AD Updated').hide(2000); });


add_action('wp_ajax_ad_settings_action', 'ad_settings_save');   

function ad_settings_save() {
$post_arr = array(
    "ad_line1" => $_POST['ad_line1'],
    "ad_line2" => $_POST['ad_line2']

//- WP-OPTION - Update theme options
update_option('ad_elements', $post_arr);

//- External DB - Updating form meta 
include('db_include.php'); **//INCLUDED 2nd Time**

echo "Success";
die; }                  

Source: wpdb

Leave a Reply