【问题标题】:WORDPRESS add_filter('cron_schedules', ...) not working in a PHP classWORDPRESS add_filter('cron_schedules', ...) 在 PHP 类中不起作用
【发布时间】:2015-08-05 04:41:46
【问题描述】:

我试图为 WP_CRON 声明一个新的时间间隔。 当我在 PHP 文件中执行此操作时,它可以工作。 当我在 PHP 类中这样做时,它不会。

有人能看出我做错了吗?

我正在使用插件cron view 来检查声明是否有效。

如果我解决了这个问题,我认为知道为什么我的 cron 作业没有在课堂上触发但在课堂外时正常工作,这也将解决我的问题。

=> 文件 myplugin.php

function set_up_option_page() {
    add_options_page( [...]);
}
add_action( 'admin_menu', 'set_up_option_page' );

function do_some_rock() {

    $instance = My_Plugin_Class::instance();

    if ( isset($_POST['action']) && 'do-magic' == $_POST['action'] ) {
        $instance->do_stuff();
    }else{
        // Display the form.
    <?
    }
}

=> 文件 My_Plugin_Class.php

<?php

if ( ! defined( 'ABSPATH' ) ) exit;

class My_Plugin_Class {

    private static $_instance = null;

    public function __construct () {

[...] 

        add_filter( 'cron_schedules', array($this,'cron_time_intervals'));

    } 

    public function cron_time_intervals( $schedules ) {
        echo "——— cron time intervals —— ";
        $schedules['minutes_1'] = array(
            'interval' => 10*60,
            'display'   => 'Once 10 minutes'
        );

        return $schedules;
    }

    public static function instance () {
        if ( is_null( self::$_instance ) ) {
            self::$_instance = new self();
        }
        return self::$_instance;
    } // End instance ()

最好的问候。

【问题讨论】:

  • 每次用户访问你的页面时你都会实例化你的类吗?
  • 你好@lukas 我很确定是这种情况。那么我首先检查是否存在该类的现有实例。 $instance = My_Class_Name::instance( FILE, '1.0.0' ); $instance->do_things();
  • 你能粘贴整个班级吗?
  • 我现在看到了。您需要调用 add_filter( 'cron_shedules', .... );每个请求 - 总是调用 do_some_rock() 函数吗?
  • do_some_rock() 是 add_options_page( ) 的回调;我不确定它是否一直在打电话,我猜不是。那么把我的 cron 代码放在哪里更有意义呢?根据 wordpress 标准,什么是最好的?在 My_Plugin_Class、My_Plugin_Cron_class 或 myplugin.php 文件(不是类)中?问候,

标签: php wordpress cron


【解决方案1】:

我几乎可以肯定 do_some_rock() 不会每次都被调用 - 只有当有人访问该页面时。您可以将 add_filter( 'cron_schedules', array($this,'cron_time_intervals')); 从您的类构造函数移动到主插件文件 myplugin.php 并或多或少地做这样的事情

add_action( 'admin_menu', 'set_up_option_page' );
$instance = My_Plugin_Class::instance();
add_filter( 'cron_schedules', array($instance ,'cron_time_intervals'));

一个额外的问题。 My_Plugin_Class 构造函数是否包含您希望防止在每个请求期间执行的任何代码?

【讨论】:

  • 是的,你完全正确。我现在明白我的错误了。非常感谢您的帮助。
【解决方案2】:

我认为正确的方法是为我的 Cron 设置一个特定的类。 这个类在每个请求上都被实例化,这就是为什么代码不应该像我以前那样在一个具有静态实例的类中。 此外,我认为最好将 Cron 放在插件类之外,以实现逻辑和更简洁的代码目的。感谢 Lukas Pawlik 的帮助。

 if (!defined('ABSPATH')) exit;

    new My_Cron();

    class My_Cron {

        public function __construct() {
            add_filter('cron_schedules', array($this, 'cron_time_intervals'));
            add_action( 'wp',  array($this, 'cron_scheduler'));
            add_action( 'cast_my_spell', array( $this, 'auto_spell_cast' ) );
        }

        public function cron_time_intervals($schedules)
        {
            $schedules['minutes_10'] = array(
                'interval' => 10 * 60,
                'display' => 'Once 10 minutes'
            );
            return $schedules;
        }

        function cron_scheduler() {
            if ( ! wp_next_scheduled( 'cast_my_spell' ) ) {
                wp_schedule_event( time(), 'minutes_10', 'cast_my_spell');
            }
        }

        function auto_spell_cast(){
            My_Plugin_Class::instance()->launch_spell();
        }
    }

【讨论】:

【解决方案3】:

我不确定,但试试这个。

   add_filter( 'cron_schedules', array($this,'cron_time_intervals'),1);

并检查 cron_time_intervals() 函数是否调用。

【讨论】:

  • 你好@dev,这也行不通。 add_filter 似乎没有触发,因为没有调用 cron_time_intervals() 函数。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-12
  • 1970-01-01
  • 1970-01-01
  • 2019-06-29
相关资源
最近更新 更多