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' );
	}
});

Comments

    1. Rob Anderson
      Rob Anderson Staff July 25, 2024 at 1:42 pm

      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,

    1. Samuel Bassah
      Samuel Bassah Staff June 10, 2024 at 4:51 pm

      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,

Leave a Reply

Your email address will not be published. Required fields are marked *

  • Trouble installing this snippet? See our troubleshooting tips.
  • Need to include code? Create a gist and link to it in your comment.
  • Reporting a bug? Provide a URL where this issue can be recreated.

By commenting, I understand that I may receive emails related to Gravity Wiz and can unsubscribe at any time.