What is your best practice to execute one-time scripts?

The Problem

We all have been in a situation like this, and a lot of questions on this site need a solution like this. You either have to update a database, insert a lot of data automatically, convert meta_keys, or something similar.

Of course, in a running system based on best practises this should not happen.

But as it does, I would love to hear your personal solution to this problem, and why you chose yours.

The Question

How do you implement one-time scripts in your (running) WordPress install?

The issue here is mainly because of the following reasons:

  • Scripts that insert data should not run more than one time
  • Scripts that require a lot of ressources should not run at a time when they can not be monitored
  • They should not be run by accident

The Reason I ask

I have got my own practise, I am going to post it in the answers. As I do not know if it is the best solution out there, I’d like to know about yours. Also, this is a question that is asked a lot of times in context of other questions, and it would be great to have a ressource collecting the ideas.

looking forward to learning from you 🙂

1
12

I for myself use a combination of:

  • one file dedicated to the one-time script
  • using a transient to stop the script from accidentally running more than once
  • using capability-management or user-control to ensure the script is just run by me.

Structure

I use a file (onetime.php) in my include-folder inc, which is included in the functions.php, and deleted from there after the use.

include( 'inc/onetime.php' );

The file for the script itself

In my onetime.php my function f711_my_onetime_function() is placed. As it could be any function. I assume your script is tested and works correctly.

To achieve the control over the execution of the script, I use both

Capability control

To stop other users from accidentally executioning my script:

if ( current_user_can( 'manage_options' ) ) // check for administrator rights

or

if ( get_current_user_id() == 711 ) // check if it is me - I prefer restricting the execution to me, not to all admins.

a transient

to stop myself from accidentally executing the script more than once.

$transient="f711_my_onetime_check";
if ( !get_transient( $transient ) ) // check if the function was not executed.

The file for executing the script in my function f711_my_onetime_function() would look like that:

$transient="f711_my_onetime_check";
if ( get_current_user_id() == 711 && !get_transient( $transient ) ) {

    set_transient( $transient, 'locked', 600 ); // lock function for 10 Minutes
    add_action( 'wp_footer', 'f711_my_onetime_function' ); // execute my function on the desired hook.

}

function f711_my_onetime_function() {
    // all my glorious one-time-magic.
}

The reason I set the transient immediately after the check if it exists is that I want the function to be executed after the script has been locked from beeing used twice.

If I need any output from my function, I either print it out as a comment in the footer, or sometimes I even filter the content.

The lockouttime is set to 10 Minutes, but can be adjusted to your needs.

Cleanup

After the successful execution of my script I delete the include from the functions.php, and remove the onetime.php from the server. As I used a timeout for the transient, I do not need to clean the database, but of course you could also delete the transient after you removed the file.

Leave a Comment