Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6853955
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:34:26+00:00 2026-05-27T01:34:26+00:00

My environment: wordpress –> myplugin in myplugin I use wp-loaded action to trigger my

  • 0

My environment:
wordpress –> myplugin
in myplugin I use wp-loaded action to trigger my cronfunc to run
my cronfunc pseudo-code:

add_action('wp_loaded','cronfunc');

    function cronfunc()
    {
        if(defined('IN_MY_CRON_FUNC'))
            return;
        define('IN_MY_CRON_FUNC',true);
        if nowtime - get_option('last_do_work_time') > 3600:
            update_option('last_do_work_time',nowtime);
            run cronjob
    }

The problem is sometimes cronfunc will run twice!
Somebody tell me why?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T01:34:27+00:00Added an answer on May 27, 2026 at 1:34 am

    I can’t seem to get it to load twice on a manually set plugin. Here is the code:

    <?php
    /*
    Plugin Name: test
    Plugin URI: http://localhost
    Description: nothing really
    Version: 00.001
    Author: somebody
    Author URI: http://localhost
    */
    add_action('wp_loaded','cronfunc');
    function cronfunc() {
        $myFile = (pathinfo(__FILE__));
        $myFile = $myFile['dirname']."/testFile.txt";
        $now = microtime(true);
        if ( $now - get_option('last_do_work_time') > 3 ) { #I set to 3 seconds to test
            $fh = fopen($myFile, 'a+') or die("can't open file");
            fwrite($fh, microtime(true)." - ".$now - get_option('last_do_work_time')."-\n");
            fclose($fh);
            update_option('last_do_work_time',$now);
        }
    }
    ?>
    

    Saved as test.php and put into a folder in plugins and activated it. Does it happen a lot or just once in a while? It’s possible that they are getting past IF before being updated again. If that is the case, you may need to lock the option and wait or skip if it’s already being run. You might need to use semaphores or create something like it using the database… maybe you could use wordpress options to define a kind of mutex or something. DEFINE() isn’t going to help, because if it’s run again side by side, then they won’t affect each other.

    here is probably a bad way to do it, but it might work for now:

    <?php
    /*
    Plugin Name: test
    Plugin URI: http://localhost
    Description: nothing really
    Version: 00.001
    Author: somebody
    Author URI: http://localhost
    */
    
    function wplock_release($id='check_cronfunc') {
        delete_option($id);
    }
    # not the best, but might work
    function wplock_aquire($id='check_cronfunc') {
        $guid = com_create_guid(); #maybe just need uniqid or something, but just testing stuff
        # check if anyone else has lock and set, not sure if we can force lines inside php
        if ( get_option($id) !== false
            || !update_option($id,$guid)
            || get_option($id) != $guid )
        { 
            return false;
        }
        register_shutdown_function(wplock_release($id)); # probably don't need this
        return true; # maybe just return true or something
    }
    
    
    add_action('wp_loaded','cronfunc');
    function cronfunc() {
        $myFile = (pathinfo(__FILE__));
        $myFile = $myFile['dirname']."/testFile.txt";
        $now = microtime(true);
        if ( wplock_aquire() ) {
            if ( $now - get_option('last_do_work_time') < 3 ) { # less than 3 seconds
                wplock_release();
                return;
            }
            wplock_release();
        } else {
            return;
        }
    
        $fh = fopen($myFile, 'a+') or die("can't open file");
        fwrite($fh, microtime(true)." - ".$now - get_option('last_do_work_time')."-\n");
        fclose($fh);
        update_option('last_do_work_time',$now);
    }
    ?>
    

    EDIT: put the lock around the option and returned if can’t get… a little sloppy, but just for the idea

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I load the Wordpress environment in a script, so I can use
Is it possible to use two different databases on WordPress depending the environment where
Environment is VS2008, .Net 3.5 The following C# code (note the specified encoding of
Environment: C# projects, Visual studio 2008, C#, .Net 3.5, MSBuild Objective: Run my own
I want to use php in console mode and create an environment to test
I've got WordPress installed on an add-on domain ( shared hosting environment), with a
I am trying to follow the setup on http://hide1713.wordpress.com/2009/01/30/setup-perfect-python-environment-in-emacs/ I have steps 1 -
In order to gain more experience in Wordpress I delved into its code base
I want to set up a production and development environment for a wordpress website.
Sometime recently my WordPress plugin started failing in my DEV environment (on two different

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.