Question

I am developing a WP Plugin.

Currently, I am making an ajax call with jquery to a standalone php file in my plugin folder.
Let's presume that this file is called test.php.

The file is NOT loaded prior to this call, so no native wordpress functions work in this file.

As a result, I have require_once('wp.load'); in test.php.

Here stems my questions:

  1. The only function I need from wordpress is $wpdb->insert. Is there a better function to include than wp-load? Something that doesn't include so much, since all I need is this one function!

  2. I understand that require_once loads the file, ONLY if it hasn't been loaded yet. Maybe I'm overlooking the situation, but how does this work with ajax? If there is a "success" response, does test.php close, thus when its called again, it loads wp-load.php again?

Thanks!

Was it helpful?

Solution

If there is a "success" response, does test.php close, thus when its called again, it loads wp-load.php again?

Exactly. The concept behind require_once (or include_once) is to make sure you don’t reload the same functions, classes & variables into the same currently running PHP code at the same time:

include_once may be used in cases where the same file might be included and evaluated more than once during a particular execution of a script, so in this case it may help avoid problems such as function redefinitions, variable value reassignments, etc.

And since PHP scripts run only on request, what you describe is exactly how it would happen.

That said, I wouldn’t over-think the concept of loading the whole wp.load each time it’s needed. I doubt it will cause such a performance hit that it would even be noticeable.

OTHER TIPS

You should consider follow this method to write an Ajax powered plugin. There is usualy no need to create a separate PHP file and load WordPress core into that file. Read that carefully and you'll find out, that is relatively easy to accomplish and is more flexible thant stand alone PHP file...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top