Recently I have created a script to update records on my Wordpress database. I basically use wpdb to connect to another database, grap some records, and update the Wordpress database. Using only Wordpress, the initial solution that I found was put all the code on a custom template. So, when I visit a given URL, the update occurs.

This custom template do all the operations by itself, I don't even pass any argument to it. I only need to run/visit an URL.

The thing is that I don't want this script to be accessible to anyone, but at the same time, I need to run it on a scheduled cron job. So, what would be the better way to protect this script? Can anyone help me?

1 Answer 1


One approach (avoiding PHP timeouts) for a local Linux Cron Job (for exact timing) is creating a WP-CLI command (does not run in the browser), like:

<?php /* Plugin Name: WP-CLI Foo */

function foo_command( $args ) {
    // Adjust to your needs ...
    WP_CLI::success( $args[0] );

WP_CLI::add_command( 'foo', 'foo_command' );

and the corresponding command line might be like:

wp --path=/full/path/to/wp-config.php foo bar

where foo is the command and bar is the argument.

One can also use WordPress' own Cron API and there are ways to sync it with an external cron job via url, if precice run times are needed.

There's also the cron command of WP-CLI.

  • 1
    Nice approach. Never used WP-CLI before, I will give it a try. Thanks!
    – churros
    Apr 26, 2021 at 17:47

Your Answer

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

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