Skip to content

ISSUE #1: The link to the ajax endpoint may not work in some configurations. #30

@xecdev

Description

@xecdev

When you link to the Ajax endpoint, you cannot assume that it's always located at wp-admin/admin-ajax.php . There are different configurations in which that won't work.

This means you can't link it statically, you have to use a function to determine its location, for example: admin_url( 'admin-ajax.php' );

Obviously you would need to execute that call on PHP and then pass the information to your JS file, you can do that using the wp_localize_script() function which is also useful for other uses like passing the nonce. Let me share an example:

function myprefix_scripts() {
wp_enqueue_script( 'myprefix-script', MYPREFIX_PLUGIN_URL . 'js/script.js', array(), MYPREFIX_VERSION );

wp_localize_script( 'myprefix-script', 'myprefix-ajax', array(
  'ajax_url' => admin_url('admin-ajax.php'),
  'nonce'  => wp_create_nonce( 'myprefix-ajax-nonce' ),
));
}
add_action( 'wp_enqueue_scripts', 'myprefix_scripts' );

Once you have this, you can later refer to your Ajax endpoint in the JS file by using the variable myprefix-ajax.ajax_url variable. Please refer to the Ajax documentation: https://developer.wordpress.org/plugins/javascript/ajax/

Example(s) from your plugin:
templates/admin/paywall-settings.php:161 ...echo esc_url( home_url( '/wp-admin/admin-ajax.php?action=payment_trigger' ) ); ?></pre>...

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions