I'm creating an advertising portal for a small network using a custom post type. In order to prevent WordPress database corruption or unauthorized access to user information, I need to keep data separated from the advertising app by duplicating custom post type data to an external database when creating and updating an ad post. I'm using the save_post
hook to handle when the function should run, and wp_insert_post()
to push all post data into the database. What I want to happen is that the post will save in the WordPress database by default, and also save to an external database using my plugin function.
The issue I can't figure out is how to tell wp_insert_post()
to use the external database instead of the global $wpdb
variable it uses by default, as outlined in WP code reference.
How can I use a custom database with wp_insert_post
? Or is there a better method?
My function, based on WordPress codex and Stack Exchange examples:
// Function to push data on update
function my_data_push ($post_id) {
// Check for custom post type
if (get_post_type($post_id) == 'ads'):
// Set up database connection to external ads storage database
$push_to_db = new wpdb('username','password','database','host');
// Insert post data in external database
wp_insert_post($post_id, $wp_error = true);
endif;
}
// Add function to post save hook
add_action('save_post', 'my_data_push');
I'm creating an advertising portal for a small network using a custom post type. In order to prevent WordPress database corruption or unauthorized access to user information, I need to keep data separated from the advertising app by duplicating custom post type data to an external database when creating and updating an ad post. I'm using the save_post
hook to handle when the function should run, and wp_insert_post()
to push all post data into the database. What I want to happen is that the post will save in the WordPress database by default, and also save to an external database using my plugin function.
The issue I can't figure out is how to tell wp_insert_post()
to use the external database instead of the global $wpdb
variable it uses by default, as outlined in WP code reference.
How can I use a custom database with wp_insert_post
? Or is there a better method?
My function, based on WordPress codex and Stack Exchange examples:
// Function to push data on update
function my_data_push ($post_id) {
// Check for custom post type
if (get_post_type($post_id) == 'ads'):
// Set up database connection to external ads storage database
$push_to_db = new wpdb('username','password','database','host');
// Insert post data in external database
wp_insert_post($post_id, $wp_error = true);
endif;
}
// Add function to post save hook
add_action('save_post', 'my_data_push');
I have been searching for an answer to this question for over a month now - i do not think wordpress is very good for any site that has a secure external DB or for anyone who does not want to store customer data in the main wordpress database, which is strange as that seems like a highly valued function.
At any rate i came across this code during my research that may help you - it is a global variable for a external DB... according to the author it is usable in the same way as $wpdb global variable is used.
If you want the author's full blurb on it go here https://bavotasan.com/2011/access-another-database-in-wordpress/
The code is: $newdb = new wpdb($DB_USER, $DB_PASSWORD, $DB_NAME, $DB_HOST); $newdb->show_errors();
Hopefully it is of some use to you...
wp_insert_post only works for wordpress own database.
You need to write a custom query using the $wpdb class.
The $wpdb
class allows you to interact with multiple databases in WordPress.
global $wpdb;
// Define the database credentials for the other database
$other_db_name = 'other_database_name';
$other_db_user = 'other_database_user';
$other_db_password = 'other_database_password';
$other_db_host = 'other_database_host';
// Connect to the other database
$other_db = new wpdb($other_db_user, $other_db_password, $other_db_name, $other_db_host);
// Define the tables to access
$posts_table = $other_db->prefix . 'posts';
// Query the data
$insert = $other_db->insert().
Use $wpdb->insert()
.
$other_db->insert($posts_table, array(
'post_title' => 'title',
'post_name' => 'post-slug', // ... and so on
));
wpdb()
function, it is possible to connect to another database. The question is how to easily write the complete post data to the external db. – Arinthros Commented Feb 24, 2017 at 14:37