我的操作是由WP Cron调用的,所以我试图阻止它同时运行两次.因此,我有以下使用doing_action function来判断操作是否正在运行的函数.显然,下面的代码即使在操作不运行时也不能运行.但是,当我删除doking_action判断时,代码就会运行.

function upload_to_ipfs() {

    if ( !doing_action( 'upload_file' ) ) {
        //Code to run here
    }
}
add_action( 'upload_file', 'upload_to_ipfs' );

推荐答案

您可能应该看看站点的瞬变.

<?php

function so_73821350_get_transient_name() {
    return 'so_73821350_transient_name'; // replace this with whatever you want
}

function so_73821350_upload_to_ipfs() {

    // get the existing transient
    //
    // If the transient does not exist, does not have a value, or has expired, 
    // then the return value will be false.
    $process_running = get_site_transient( so_73821350_get_transient_name() );

    if ( $process_running ) {
        // bail out in case the transient exists and has not expired
        // this means the process is still running
        return; 
    }

    // set the transient to flag the process as started
    // 60 is the time until expiration, in seconds
    set_site_transient( so_73821350_get_transient_name(), 1, 60);

    // Run the upload process here
    
    upload_function();

    // ...
    

    // delete the transient to remove the flag and allow the process to run again
    delete_site_transient( so_73821350_get_transient_name() ); 
    
}
add_action( 'upload_file', 'so_73821350_upload_to_ipfs' );

文档:

get_site_transient()

set_site_transient()

delete_site_transient()

Php相关问答推荐

SQLSTATE [42S02]:找不到基表或视图:1146 Table eshop. sessions不存在'''

自定义通知在Woocommerce我的帐户自定义端点页面上不起作用

在WooCommerce产品变体SKU字段旁边添加自定义输入字段

Laveel Livewire Get Groupby姓名列表

WooCommerce在购买后多次重定向

SendGrid响应PHP

如何在php网站代码中清理浏览器缓存

Laravel服务Provider 没有向服务类注入价值

重命名 WordPress 中现有的自定义分类法名称和标签

根据页面以及是否在促销,向 WooCommerce 中显示的价格添加前缀

PHP 解析 XML 以获取多个深度的所有字符串值

Golang - Html 模板:如何创建导航项 - 子菜单?我找不到怎么做

在 WooCommerce 管理变量产品中启用其他变体图像

使用 Composer 找不到 Google APIClient 类

ACF 更新字段功能不更新 WordPress 中的任何数据

判断类别是否存在 Shopware 6

来自命令行的 PHP 没有给出正确的结果

为什么 PDO 允许使用带命名占位符的索引数组,但仅在禁用仿真时才允许?

如何将 2021-04-08T22:25:09.335541Z 作为时间戳保存到 Laravel 中的数据库中?

如何简化 php API 中的格式化数组列表数据以提供 React Native 部分列表