Custom Database
By default, Populate Anything will only show the database that the current WordPress installation is on.
The following snippet allows registering additional an additional database with custom credentials.
You can add multiple databases by adjusting the class names and adding additional
gp_populate_anything()->register_object_type()
calls.
Instructions
Code
Filename: gppa-custom-database.php
<?php
/**
* Gravity Perks // Populate Anything // Custom Database
* https://gravitywiz.com/documentation/gravity-forms-populate-anything/
*
* Instruction Video: https://www.loom.com/share/f9bf62e358dd4c54ab346998c5f3b516
*
* By default, Populate Anything will only show the database that the current WordPress
* installation is on.
*
* The following snippet allows registering additional an additional database with custom credentials.
*
* You can add multiple databases by adjusting the class names and adding additional
* `gp_populate_anything()->register_object_type()` calls.
*/
class GPPA_Object_Type_Database_Testing extends GPPA_Object_Type_Database {
const DB_NAME = 'testing';
const DB_USER = DB_USER;
const DB_PASSWORD = DB_PASSWORD;
const DB_HOST = DB_HOST;
public $wpdb_instance;
public function get_label() {
return esc_html__( 'Database: ', 'gp-populate-anything' ) . self::DB_NAME;
}
public function get_db() {
if ( ! isset( $this->wpdb_instance ) ) {
$this->wpdb_instance = new wpdb( self::DB_USER, self::DB_PASSWORD, self::DB_NAME, self::DB_HOST );
}
return $this->wpdb_instance;
}
}
add_action('init', function() {
if ( class_exists( 'GP_Populate_Anything' ) ) {
gp_populate_anything()->register_object_type( 'database-testing', 'GPPA_Object_Type_Database_Testing' );
}
});
Is it safe to assume this only works with MySQL databases?
Hi Gary,
You are correct; this snippet adds support for MySQL only. Other database types are not supported at this time. If additional database support is something you’d like to see, please just let us know.
Cheers,
Is there a way to use Populate Anything to retrieve data from a third-party API?
Hi Art,
GP Populate Anything, natively doesn’t support populating from API. We do, however, have a couple experimental snippets that you can use as a starting point if your API endpoint returns data as JSON
https://github.com/gravitywiz/snippet-library/blob/master/experimental/gppa-object-type-json-api.php https://github.com/gravitywiz/snippet-library/blob/master/experimental/gppa-object-type-json-api-with-query-params.php
Both snippets will require modification to work, but they should give you an idea of how to set things up. Which snippet you choose depends on how your API functions. If you need to pass args as query params, you’ll use the second snippet.
I hope this helps.
Best,