Home » WordPress: Run Function Only Once

WordPress: Run Function Only Once

Last updated on May 28, 2021 by

Sometimes, you might need to run the function or code only once in a lifetime in WordPress. In this tutorial, we will see the easiest way to execute the function or code only once in WordPress. So without wasting a second, let’s just jump into it.

Run Function Only Once In WordPress

There are many situations where we need to execute the function or code only once in WordPress. Some of them are as below:

  1. To create a database table only once
  2. Sometimes, we might need to create a USER only a single time
  3. Any database-related operations like insert data, update data, a new column, remove a column, etc.
  4. Assign/Remove user a role only for a single time
  5. Other operations

Execute Function Only Once

Most of the WordPress developer aware of get_option() function. This function will try to get the value of the provided key and return false if the value does not exist.

We will use the get_option() function to check the key is set and has any value if not then we will allow them to run the code and set the value using add_option() function.

We will set the value/flag in WordPress’s default prefix_options table using add_option() function if code already ran. Thus, code only executes once and never executes again. Let’s see the code:

<?php

function scratchcode_run_code_one_time() {
	if ( !get_option('run_only_once_01') ):

		// Execute your one time code here

		add_option('run_only_once_01', 1); 
	endif;
}
add_action( 'init', 'scratchcode_run_code_one_time' );

If you want to run the above code again for testing then you need to find the key `run_only_once_01` in prefix_options table and remove that record. You can also use the delete_option( 'run_only_once_01' ); function to remove the key.

Moreover, you can introduce as many keys for your one-time code forex. run_only_once_02, run_only_once_03, etc

Where To Add This Snippet?

Most Important: Add the following code to your child theme’s functions.php file. If you add custom code directly to your parent theme’s functions.php file then it will be wiped entirely when you update the theme.

If you are using a custom theme and if it doesn’t require any update then you can directly place the code into wp-content/themes/your-theme/function.php file.

Please also note that we have tested all codes in the GeneratePress child theme & Storefront child theme.

Additionally, read our guide:

  1. How To Add Back Button In Elementor
  2. 403 Error When Updating In Elementor
  3. How To Add Multiple Post Types In Posts Widget In Elementor
  4. How to Add Products Per Page Dropdown in WooCommerce
  5. “Sorry, your session has expired. Return to homepage” – WordPress WooCommerce Error
  6. How to Create a Plugin in WordPress from Scratch
  7. How to Disable Admin Bar in WordPress Without Plugin
  8. How To Send Custom Emails in WordPress
  9. How to Allow Preview of Draft Post Without Login in WordPress
  10. Import Users From CSV In WordPress Programmatically
  11. Dynamically Populate A Select Field’s Choices In ACF
  12. How To Remove WooCommerce Data After Uninstall
  13. WordPress Media Library Not Showing Images

We hope this article helped you to run functions or code only once in WordPress.

Please let us know in the comments if everything worked as expected, your issues, or any questions. If you think this article saved your time & money, please do comment, share, like & subscribe. Thank you in advance 🙂. Keep Smiling! Happy Coding!

 
 

Leave a Comment