Software Developer

Run Code Only Once in WordPress

You may have a function or some code that you only want to run one time on your WordPress site. You may notice that functions hooked on to admin_init can be fired twice.

Here is quick way to run your code only once. This will guarantee that it runs one, and only one, time.

Mandatory step: Insert your code or function call starting on line 9.

Code block 1

/**
 * Run code only once
 */
function my_run_only_once() {

    if ( get_option( 'my_run_only_once_01' ) != 'completed' ) {
 
        // PLACE YOUR CODE BELOW THIS LINE
         
 
 		
 
 
 
        update_option( 'my_run_only_once_01', 'completed' );
    }
}
add_action( 'admin_init', 'my_run_only_once' );

This also works if you change the hook on line 18 from admin_init to any hook you need.

Yes, this adds an option to your WordPress database. This is not a big deal for a one time thing. If you ever remove the above code, then you can afterward delete the option like this:

delete_option( 'my_run_only_once_01' );

If you want to run your code again, then delete the option with that line. You would do this if, for example, you change the code that you inserted into line 9. Or you replace the code on line 9 with some other code that you also want to run only once.

Beware though, if you leave this delete_option line on your server, then it will keep on deleting the option and so make your code from Code Block 1 run on every load. Again, don’t delete the option until you’ve removed the whole Code Block 1.

What I do to avoid this issue, if I need to run something again, is that instead of deleting the option, I rename the option. You can rename the option by changing my_run_only_once_01 on lines 6 and 15 to my_run_only_once_02. Later, if you need to run something else just once, you can change it to my_run_only_once_03.

Don’t delete those options until after you’ve removed the entire code snippet from Code Block 1.

By

We've 5 Responses

  1. July 3rd, 2018 at 5:51 pm

    This will come in very handy when we want to change for example a capability for a role..which of course only should run once because the change will add into the database.
    (We use to do this when we install a new clean instance of WordPress and want to change a lot of caps on Roles which where set by core)

    Thank you for the code.

    avatar
  2. October 16th, 2018 at 6:42 am

    It’d probably be better to just set a truthy value instead of text to compare, like:

    if( ! get_option( 'my_run_only_once_01' ) ) ... update_option('my_run_only_once_01', 1)
    avatar
  3. June 13th, 2019 at 2:09 pm

    Thanks a lot, that’s exactly what I was looking for… I needed to insert terms in a custom taxonomy on plugin init but only once. Until now when I deleted a term it was automatically recreated. Now everything works like expected.

    avatar

Leave a Reply to zaus Cancel reply

Your email address will not be published. All comments will be moderated.

Please wrap code in "code" bracket tags like this:

[code]

YOUR CODE HERE 

[/code]