1

I am working a plugin where I am trying to connect an HR system with WordPress. There is an XML API on the HR system so I have written the plugin to take the data and put it into a WordPress database table specific to my plugin. This is all done via the built in AJAX etc but there was a request for the person to be able to schedule a task so that it wasn't querying the HR system on every site visit (the list of positions need to be displayed on the site front page). Then the problem becomes syncing. This is an IIS environment so they want to use Scheduled Tasks to sync the site. I explained the WordPress scheduled task API but they prefer to have it sync manually through their own infrastructure.

How do I create code (essentially a php file) that can be run by a scheduler without username/password without calling wp-load.php? Their scheduler needs to be able to run a URL alone so this makes it somewhat difficult.

I'm trying to explain this without code since its a generic problem but in this case, the bulk script I have created and works looks like this:

if ( !defined('WP_LOAD_PATH') ) {

    /** classic root path if wp-content and plugins is below wp-config.php */
    $classic_root = dirname(dirname(dirname(dirname(dirname(__FILE__))))) . '/' ;
    //echo $classic_root;
    if (file_exists( $classic_root . 'wp-load.php') )
        define( 'WP_LOAD_PATH', $classic_root);
    else
        if (file_exists( $path . 'wp-load.php') )
            define( 'WP_LOAD_PATH', $path);
        else
            exit("Could not find wp-load.php");
}

// let's load WordPress
require_once( WP_LOAD_PATH . 'wp-load.php');

// If the plugin class is not found, die.
if (!class_exists('Aragon_eRH_RSS')) {
    die('Aragon-eRH RSS not installed.');
}

$core = new Aragon_eRH();
$core->aerh_xml_parser_bulk();

Now they can run http://site.com/wp-content/plugins/aragon-erh-rss/lib/bulk-sync.php?nonce=thenonce

This performs the syncing function. Note that there is a nonce required and can be changed from the plugin's admin menu. See screenshot below: Admin Screnshot

Note: I want to avoid it because:

  1. Considered bad practice
  2. Plugin was rejected due to including WP-Load
3
  • It's not clear what is your issue with including wp-load.php. If you need WP core loaded for something - you have to load it one way or another.
    – Rarst
    Commented Dec 15, 2013 at 21:28
  • @Rarst Plugin got rejected from WordPress repo for this reason.
    – Omar Mir
    Commented Dec 15, 2013 at 21:40
  • What does public plugin have to do with someone's IIS infrastructure then? It is extremely hard (if at all possible) to implement custom loader in public-grade code.
    – Rarst
    Commented Dec 15, 2013 at 22:34

1 Answer 1

3

Add a rewrite endpoint to give your plugin a public URL-

function wpa_my_endpoint(){
    add_rewrite_endpoint( 'my_api', EP_ROOT );
}
add_action( 'init', 'wpa_my_endpoint' );

After you flush rewrite rules, you'll have the URL available for the scheduler to ping.

http://example.com/my_api/do/something/

Then catch those requests on the parse_query action to invoke your plugin action:

function wpa_parse_query( $query ){
    if( isset( $query->query_vars['my_api'] ) ){
        include( plugin_dir_path( __FILE__ ) . 'stuff/things.php');
        exit;
    }
}
add_action( 'parse_query', 'wpa_parse_query' );
5
  • I'll try it and let you know ASAP
    – Omar Mir
    Commented Dec 16, 2013 at 4:46
  • How would I print the URL on the screen? Check if they are using "pretty" URLs or classic and show it - or is there a function in WP?
    – Omar Mir
    Commented Dec 16, 2013 at 6:01
  • you can check the contents of the option permalink_structure, if it's empty pretty permalinks are off. you can use home_url to print the front end URL, and add_query_arg if pretty permalinks are off.
    – Milo
    Commented Dec 16, 2013 at 7:23
  • Is it possible to have conflicting endpoints? For example, if I have a plugin that creates the endpoint aerh and another plugin decides it wants to use aerh or the wordpress site editor creates a page call aerh won't that mess with my endpoint?
    – Omar Mir
    Commented Dec 20, 2013 at 16:27
  • it is indeed possible to create conflicts, but you can probably make it sufficiently unique that it's unlikely to conflict.
    – Milo
    Commented Dec 20, 2013 at 19:08

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.