How do I create an instance of the API?

Hi…

I’m trying to build a plugin to manage some forms through the API, but I’m getting an error:

Fatal error: Uncaught Error: Class "GFAPI" not found in…

In my plugin file I’m doing this:

add_action( ‘wp_enqueue_scripts’, function() {
wp_enqueue_style(‘duplicate-tax-styles’ , plugin_dir_url( FILE ) . ‘/assets/duplicate-tax.css’ );
wp_enqueue_script( ‘duplicate-tax-script’, plugin_dir_url( FILE ) . ‘/assets/duplicate-tax.js’, array(‘jquery’), ‘1.0’, true );
} );

My JS file, on page load, will call a PHP function, which throws the error on the first line:

$form=GFAPI::get_form(20);

Do I need to include the API file first?

Thank you so much!

/kevin

You need to hook your function that is calling the GFAPI to the WordPress init action, so it only runs after Gravity Forms has loaded.

Okay, but trying that, I’m getting the same error:

add_action(“plugins_loaded”,“load_tax_files”);

function load_tax_files() {
	add_action( 'wp_enqueue_scripts', function() {
		wp_enqueue_style('duplicate-tax-styles' , plugin_dir_url( __FILE__ ) . '/assets/duplicate-tax.css' );
		wp_enqueue_script( 'duplicate-tax-script', plugin_dir_url( __FILE__ ) . '/assets/duplicate-tax.js', array('jquery'), '1.0', true );
	} );
}

Is this not the right way?

EDIT: Sorry, are you saying this should be in the wp-settings.php file and not at the start of my plugin code?

It’s the function that contains the $form=GFAPI::get_form(20); line that you need to delay running. One way to do that is by hooking it to the WordPress init action.

Hm. I thought that was happening by using “plugins_loaded” or “gform_loaded” because that line isn’t called until of these is fired.

I’ve been trying a million different things and I’m wondering if the API is just not available once my script loads.

To be clear, I’m using an AJAX POST to launch my script.

Without seeing your code, including the hook it is bound to, I can only guess it running before the WordPress init action is triggered. I’ve confirmed on my test site that the GFAPI is available when init is triggered.

Hi…

Here is the block in my plugin:

add_action("init","load_tax_files");

function load_tax_files() {
	
	add_action( 'wp_enqueue_scripts', function() {
		wp_enqueue_style('duplicate-tax-styles' , plugin_dir_url( __FILE__ ) . '/assets/duplicate-tax.css' );
		wp_enqueue_script( 'duplicate-tax-script', plugin_dir_url( __FILE__ ) . '/assets/duplicate-tax.js', array('jquery'), '1.0', true );
	} );
}

Here is the JS script:

jQuery(document).ready(function ($) {if ($(document).attr(“title”)==“Duplicate Tax Statement Request”) {

document.addEventListener("gform/post_render",(event) => {
			$.ajax({
			type: "POST",
			url: "wp-content/plugins/uwsa-gf-request-duplicate-tax-statement/assets/add_new_fields.php",
			success: function (response) {
				console.log(response);
			},
			error: function (errMsg) {
				console.log(errMsg.responseText);
			}
		});
		});
		
	}
});

And the PHP file:

$form=GFAPI::get_form(20);

	$new_field_id = GFFormsModel::get_next_field_id( $form['fields'] );

	$properties['type'] = 'text';

	$field = GF_Fields::create( $properties );

	$field->id = $new_field_id;
	$field->label = 'My New Field';

	GFAPI::update_form($form);

I should also say I don’t think I’ll be allowed to edit any WP files, including functions.php.

Hey @kevin.frey,

When you use AJAX to post directly to a PHP file in your custom plugin it doesn’t automatically load WordPress. That’s why the GFAPI class error occurs.

Best practice is to register an AJAX handler in your plugin that’ll process your POST request. You’ll also need to localize the AJAX URL and a security nonce when you register the Javascript file, and then update your jQuery to post to the correct URL with the correct data (action + nonce).

It’s worth noting that AJAX handlers are a common attack vector as developers overlook user authentication or nonce checks before completing an action. Other common problems include missing input sanitization and output escaping.

All the best with your plugin. :+1:

1 Like

Thank you!

I very much appreciate the input. Right now this is only a proof of concept and not final code.

It’s still unclear if what I’m trying to accomplish can even be done with the API. Pretty sure not 100% will be possible so I’m wondering how much extra code will be needed, if that’s even a solution.

Anyway, I thank you all very much for the help!

/kevin

1 Like