I'm trying to perform a batch insertion of multiple users and posts using the wp_insert_user() and wp_insert_post() functions. The problem is, I can't find any clear instructions as to where I should call these functions from. Everything talks about "hooks", but I'm not wanting to hook anything; I'm wanting to be able to e.g. visit a particular URL in a web browser and have the import take place. Should I place the calls in a special theme file? In the root directory of my WordPress installation? How can I trigger the function call to take place in the correct context?


I don't know why you would want to exclude hooks as a possible solution, because I still think this is the best option

OPTION 1 - without hooks

To make this work, the specific URL you want to use must exist and must not return a 404. I probably think your best option here would be to create a private page and add your code directly into a specified template which will be used for that page. When you then visit that particular page, your code will execute.

IMHO, this is really bad solution though

OPTION 2 - Using hooks

In my opinion, this will be the best option


Add your code in a plugin and hook your code to the register_activation_hook hook. This will ensure that when the plugin is activated, your code will run and not after that again on every page load after that


Add your code inside your theme and hook it to the after_theme_switch hook. This will run as soon as you activate your theme.


With the use of the conditional tags, you can specifically target a specific page and then hook your function in order to execute your code when you visit that specific URL. You can make use of the wp hook here as by the time that wp executes, the conditional tags is set. You can also use the template_redirect hook which is the prefered hook by many.

Between these three options, they are the best route to take IMHO.


You should build your system such that if you accidently or knowingly run the code twice, it would do nothing on second run. Probably the best option would be is to save something in options and then check for a specific value before executing your code.


add_action( 'wp', function () // Can also use template_redirect as $tag
    // Make sure we target a specific page, if not our page, bail
    if ( !is_page( 'my selected page' ) ) // Use any conditional tag here to your specific needs

    // Chech if our custom option exist with a specific value, if yes, bail
    if ( true == get_option( 'my_custom_option' ) )

     * This is where you should do all your work as we are on the selected page
     * and our option does not exist with our prefered value. Just a few notes here 
     * which you should consider
     * - Before inserting users, make sure that the user does not exist yet
     * - Before inserting posts make sure as to not duplicate posts

     // Run all your code here to insert posts and users

     * Create and update our option with the value of `true`. 
     * This will ensure that our code will be executed once
    update_option( 'my_custom_option', 'true' );
  • I think I'll go with the "without hooks" option as this is just a one-time thing and not really worth the extra effort. Also it's OK if it runs more than once as I'll need to run it a few times for testing and I can make sure that I've cleared the database between runs. Jan 25 '16 at 8:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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