Skip to content

Instantly share code, notes, and snippets.

@asharirfan
Last active March 15, 2022 20:40
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save asharirfan/613ce19e894f2b8069997758347498fe to your computer and use it in GitHub Desktop.
Save asharirfan/613ce19e894f2b8069997758347498fe to your computer and use it in GitHub Desktop.
🚀 📦 🔥 Create and test connection to an external database in WordPress using this plugin. ⚠️NOTE: Change the values of the constants according to your database.
<?php
/**
* Plugin Name: WP Test External DB
* Plugin URI: https://asharirfan.com
* Description: Create and test connection to an external WordPress database.
* Author: Ashar Irfan
* Version: 1.0.0
* Text Domain: test-db
* Author URI: https://asharirfan.com/
* License: GPL2
*
* @package test-db
*/
/*
WP Test External DB
Copyright(c) 2018 Ashar Irfan (email : mrasharirfan@gmail.com)
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2, as
published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
/**
* Define DB constants.
*/
define( 'TEST_DB_HOST', 'localhost' ); // DB Host name.
define( 'TEST_DB_USER', 'ashar' ); // DB Username.
define( 'TEST_DB_PASS', 'ashar' ); // User's password.
define( 'TEST_DB_NAME', 'wsal_db' ); // DB Name.
define( 'TEST_DB_TABLE', 'new_table' ); // DB Test Table.
if ( ! class_exists( 'WP_Test_External_DB' ) ) :
/**
* WP Test External DB
*
* Class to test external db with preset constants
* defined above.
*/
final class WP_Test_External_DB {
/**
* Class instance.
*
* @var WP_Test_External_DB
*/
public static $instance;
/**
* External DB object.
*
* @var wpdb
*/
private $wp_test_db;
/**
* Connection indicator.
*
* @var bool
*/
private $is_connected = false;
/**
* Standard singleton pattern.
*
* @return WP_Test_External_DB - Returns the current plugin instance.
*/
public static function get_instance() {
// Set class instance.
if ( ! self::$instance ) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Method: Constructor.
*/
public function __construct() {
// Start test connection.
$this->connect();
// Add admin notices.
add_action( 'admin_notices', array( $this, 'test_db_notice' ) );
add_action( 'admin_init', array( $this, 'test_query' ) );
}
/**
* Method: Connect to database.
*/
private function connect() {
// Connect To database.
try {
$this->wp_test_db = new wpdb( TEST_DB_USER, TEST_DB_PASS, TEST_DB_NAME, TEST_DB_HOST );
} catch ( Exception $e ) {
// Log Database Error.
$this->error_log( $e->getMessage() );
}
}
/**
* Method: External DB admin notices.
*/
public function test_db_notice() {
// Add admin notice.
if ( ! empty( $this->is_connected ) ) :
?>
<div class="notice notice-success is-dismissible">
<p><?php esc_html_e( 'Connection established with external database.', 'test-db' ); ?></p>
</div>
<?php
else :
?>
<div class="notice notice-error is-dismissible">
<p><?php esc_html_e( 'Error occurred while establishing connection to the external database. Please check the debug log for errors.', 'test-db' ); ?></p>
</div>
<?php
endif;
}
/**
* Error Logger
*
* Logs given input into debug.log file in debug mode.
*
* @param mix $message - Error message.
*/
private function error_log( $message ) {
if ( WP_DEBUG === true ) {
if ( is_array( $message ) || is_object( $message ) ) {
error_log( print_r( $message, true ) );
} else {
error_log( $message );
}
}
}
/**
* Method: Test query.
*/
public function test_query() {
// Test query.
$rows = $this->wp_test_db->get_results( 'SELECT * FROM ' . TEST_DB_TABLE );
if ( ! empty( $rows ) ) {
$this->is_connected = true;
}
}
}
endif;
/**
* Load the plugin.
*/
function wp_test_db_init() {
// Iniatiate the test db class.
WP_Test_External_DB::get_instance();
}
wp_test_db_init();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment