0

Active plugin PHP files are interpreted automatically when you view a page, post, admin page, etc.

I could also run a plugin PHP directly if I knew the fully qualified URL, however it will most likely throw errors as non of the Wordpress functions will have been included.

How would I test if a plugins PHP page has been executed through Wordpress? Is there a global variable that is safe to rely on?

8
  • Are you asking how to tell if a plugin file has been loaded directly via a browser?
    – Tom J Nowell
    Commented Mar 5, 2014 at 11:15
  • I am trying to process the code one way if it is executed via Wordpress and another way if I entered www.mywebsite.com/wp-content/plugins/myplugin/myplugin.php
    – McShaman
    Commented Mar 5, 2014 at 11:22
  • Why? Are you trying to implement an AJAX handler or form submission?
    – Tom J Nowell
    Commented Mar 5, 2014 at 11:29
  • In this case... Yes I am
    – McShaman
    Commented Mar 5, 2014 at 11:45
  • 1
    Then I strongly suggest you instead look at the WP AJAX api, you should never do what you're trying to do
    – Tom J Nowell
    Commented Mar 5, 2014 at 12:00

2 Answers 2

2

You should never need to send a user or a form or an AJAX request directly to a plugin files URL.

If you're submitting a form, no new URL is needed at all, simply:

if ( !empty( $_POST['formfield'] ) {
    // handle form submission and print thank you
} else {
    // display form
}

Or as most people do, use gravity forms or a plugin such as contact7

If you're doing an AJAX call, use the provided API for AJAX calls

PHP:

function example_ajax_request() {
    if ( isset($_REQUEST) ) {
        $fruit = $_REQUEST['fruit'];
        if ( $fruit == 'Banana' ) {
            $fruit = 'Apple';
        }
        echo $fruit;
    }
    // Always die in functions echoing ajax content
   die();
}
add_action( 'wp_ajax_example_ajax_request', 'example_ajax_request' );
add_action( 'wp_ajax_nopriv_example_ajax_request', 'example_ajax_request' );

Javascript

jQuery(document).ready(function($) {
    // We'll pass this variable to the PHP function example_ajax_request
    var fruit = 'Banana';
    // This does the ajax request
    $.ajax({
        url: ajaxurl,
        data: {
            'action':'example_ajax_request',
            'fruit' : fruit
        },
        success:function(data) {
            // This outputs the result of the ajax request
            console.log(data);
        },
        error: function(errorThrown){
            console.log(errorThrown);
        }
    });
});

Notes on WP AJAX

The only time you should test if the file is being loaded as you requested, is so that you can immediatley exit the script to avoid security concerns.

E.g.

if ( !defined( 'WPINC' ) ) {
    die();
}

Faster AJAX calls

The standard AJAX caller can sometimes be under-performant when doing things on super high traffic sites, in this case, I defer to Rarsts answer here:

Ajax takes 10x as long as it should/could

But even in that case, I would not have a single file handle both the AJAX and the non-AJAX, it's poor separation of concerns. Your AJAX handler should be a separate dedicated file if it isn't using the WP AJAX APIs.

3

There are a lot of way, you can look at a WordPress constant, or a WordPress function...

Examples:

Check for a constant

if ( defined( 'WPINC' ) ) {
  // the constant WPINC is defined, so WordPress is loaded
  // you should not define a cosntant with this name... and if you absolutely need
  // you can use another WordPress specific constant, examples here:
  // http://phpxref.ftwr.co.uk/wordpress/nav.html?_constants/index.html
}

Check for a function + hook fired

if ( function_exists('did_action') && did_action( 'muplugins_loaded' ) ) {
  // this check if did_action function exists and the muplugins_loaded is fired
  // if this is tru you can be pretty sure WordPress is loaded.
  // muplugins_loaded is the first hook fired by WP (before any regular plugin is loaded)
  // However if you define the constant SHORTINIT and set to a non-empty value
  // this will never be true
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.