PHP 脚本无法为多站点替换 WordPress cron

     2023-03-04     49

关键词:

【中文标题】PHP 脚本无法为多站点替换 WordPress cron【英文标题】:PHP script not working to replace WordPress cron for multi-site 【发布时间】:2012-08-02 16:34:08 【问题描述】:

我正在尝试将 WordPress cron 替换为我的多站点的服务器 cron。看来我必须为每个站点添加一个 cron 条目,这可能是一场管理噩梦。相反,我想运行一个为每个子站点调用 cron 的 cron 作业。下面是一个插件脚本,但出现错误 500。错误消息具有误导性且不正确,因为它说明了有关未安装 ffmpeg 的内容(没有其他错误消息)。有人可以帮我纠正这个吗?该插件位于 GitHub (https://github.com/chibani/wp_multisite_cron) 中,但我认为这会成为社区的绝佳插件。感谢您的帮助!

register_activation_hook(__FILE__, array('wp_multisite_cron','plugin_activation'));
add_action('init', array('wp_multisite_cron', 'init'));

class wp_multisite_cron

    const LANG = 'wp_multisite_cron';
    const LANG_DIR = '/lang/';

    /**
     * 
     * Plugin activation (sets default parameters)
     */
    public static function plugin_activation()
        if(!self::get_option('concurrent_crons'))
            self::update_option('concurrent_crons', 100);
    

    /**
     * 
     * The main 'loader'
     */
    public static function init() 

        //Setup the translation
        load_plugin_textdomain(self::LANG, false, dirname(plugin_basename( __FILE__ ) ) . self::LANG_DIR);

        //The multisite cron action     
        add_action('wp_ajax_wp_multisite_cron_call', array('wp_multisite_cron','cron_call'));
        add_action('wp_ajax_nopriv_wp_multisite_cron_call', array('wp_multisite_cron','cron_call'));

        // admin actions and hooks
        if (is_admin()) 
            self::admin_hooks();
        
    

    /**
     * 
     * The admin hooks
     */
    public static function admin_hooks()
        //Setting menu
        add_action('admin_menu', array('wp_multisite_cron', 'admin_menu'));
        if(is_network_admin() && self::is_ready())
            add_action('network_admin_menu', array('wp_multisite_cron', 'network_admin_menu'));
        
    

    /**
     * 
     * Set up the admin menu(s)
     */
    public static function admin_menu()
        add_options_page("WordPress multisite cron, information page", "WP multisite cron", 'manage_options', 'wp_multisite_cron_settings', array('wp_multisite_cron', "admin_settings"));
    

    /**
     * 
     * Set up the admin menu(s)
     */
    public static function network_admin_menu()
        add_submenu_page("settings.php","WordPress multisite cron, information page", "WP multisite cron", 'manage_options', 'wp_multisite_cron_settings', array('wp_multisite_cron', "admin_settings"));
    


    /**
     * 
     * The admin settings page
     */
    public static function admin_settings()
        ?>
        <div class="wrap">
            <div id="icon-options-general" class="icon32">
                <br />
            </div>
            <h2>WordPress multisite cron</h2>

            <?php if (isset($_POST['wp_multisite_cron']) && !empty($_POST['wp_multisite_cron'])) :

                if (isset($_POST['_wpnonce']) && wp_verify_nonce( $_POST['_wpnonce'], plugin_basename( __FILE__ ) )) :
                    self::delete_option('concurrent_crons');

                    foreach($_POST['wp_multisite_cron'] as $option_name=>$option_value)
                        self::update_option($option_name, $option_value);
                    
                ?>
                    <div id="setting-error-settings_updated" class="updated settings-error">
                        <p>
                            <strong><?php _e('Settings saved.')?></strong>
                        </p>
                    </div>
                <?php else: ?>
                    <div id="message" class="error fade">
                        <p><?php _e('Unable to update settings.',self::LANG)?></p>
                    </div>
                <?php endif;?>
            <?php endif;?>



            <?php if(!is_multisite()):?>
                <div class="tool-box error">
                    <h3><?php _e('Multisite',self::LANG) ?></h3>
                    <p><?php _e('Your WordPress installation is not configured as multisite',self::LANG) ?>.</p>
                    <p><?php _e("Read the WordPress' documentation about multisite to learn how to set up multisite",self::LANG)?> : <a href="http://codex.wordpress.org/Create_A_Network" target="_blank"><?php _e('here',self::LANG)?></a></p>
                </div>
            <?php endif;?>


            <?php if(!defined('DISABLE_WP_CRON') || !DISABLE_WP_CRON ):?>
                <div class="tool-box error">
                    <h3><?php _e('WordPress default cron system',self::LANG) ?></h3>
                    <p><?php _e('You must disable the WordPress cron system, to use this extension',self::LANG) ?>.</p>
                    <p><?php _e('Add the following code in your wp-config.php',self::LANG) ?> :</p>
                    <p>
                        <code>define('DISABLE_WP_CRON', true);</code>
                    </p>
                </div>
            <?php endif;?>

            <form action="" method="post">
                <table class="form-table">
                    <tbody>
                        <tr valign="top">
                            <th scope="row">
                                <label for="concurrent_crons"><?php _e('Concurrent crons',self::LANG) ?></label><br />
                                <em><?php _e("Lower it if some of your sites' cron don't run",self::LANG)?></em>
                            </th>
                            <td>
                                <input type="text" name="wp_multisite_cron[concurrent_crons]" id="concurrent_crons" value="<?php echo self::get_option('concurrent_crons') ?>" />
                            </td>
                        </tr>
                    </tbody>
                </table>
                <p class="submit">
                    <input class="button-primary" name="plugin_ok" value="<?php _e('Save settings',self::LANG) ?>" type="submit" />
                </p>
                <?php wp_nonce_field( plugin_basename( __FILE__ ), '_wpnonce' );?>
            </form>


            <?php if(self::is_ready()): ?>
                <div class="tool-box">
                    <h3><?php _e('Set-up WordPress multisite cron',self::LANG) ?></h3>
                    <p><?php _e('Add the following line in your crontab',self::LANG)?> :</p>
                    <p>
                        <code>*/15 *<?php echo "\t" ?>* * * www-data /usr/bin/wget -qO- <?php echo admin_url('admin-ajax.php','http').'?action=wp_multisite_cron_call' ?></code>
                    </p>
                </div>
            <?php endif;?>
        </div>

        <?php 
    

    /**
     * 
     * Check wether the config is ok to run real crons
     */
    public static function is_ready()
        return (is_multisite() && defined('DISABLE_WP_CRON') && DISABLE_WP_CRON);
    

    /**
     * 
     * The real cron
     */
    public static function cron_call()

        //load the blogs id
        $blogs = self::get_blogs_id();
        if(count($blogs))
            $mh = curl_multi_init();

            $length = self::get_option('concurrent_crons');
            $offset = 0;

            //Cut the 
            while($blogs_slice = array_slice($blogs,$offset,$length))

                $offset += $length;

                foreach($blogs_slice as &$blog_id)
                    switch_to_blog($blog_id);
                    $cron_url = site_url().'/wp-cron.php?doing_wp_cron';

                    //Add the url to the stack
                    $chs[$blog_id]=curl_init();
                    curl_setopt($chs[$blog_id], CURLOPT_URL, $cron_url);
                    curl_setopt($chs[$blog_id], CURLOPT_HEADER, 0);
                    curl_multi_add_handle($mh, $ch);
                

                //Launch :)
                $running = null;
                do
                    curl_multi_exec($mh, $still_running);
                while($still_running > 0);

                curl_multi_close($mh);
            
        
        die();
    

    /**
     * 
     * Get the blog_id for each active blog in the multisite
     * @return array
     */
    public static function get_blogs_id()
        global $wpdb;

        //Get the blogs' ids for blogs that are public and active
        $blogs = $wpdb->get_col('SELECT blog_id FROM '.$wpdb->blogs.' WHERE public=1 AND deleted=0');
        return $blogs;
    

    /**
     * 
     * Get a plugin's specific option
     * @param string $option_name
     */
    public static function get_option($option_name)
        return get_option('wp_multisite_cron_'.$option_name);
    

    /**
     * 
     * Set a plugin's specific option
     * @param unknown_type $option_name
     */
    public static function update_option($option_name,$option_value)
        return update_option('wp_multisite_cron_'.$option_name,$option_value);
    

    /**
     * 
     * Delete a plugin's specific option
     * @param string $option_name
     */
    public static function delete_option($option_name)
        return delete_option('wp_multisite_cron_'.$option_name);
    


    /**
     * 
     * get the plugin's path url
     */
    public static function get_plugin_url()
        return get_bloginfo('url') . '/' . PLUGINDIR . '/' . dirname(plugin_basename(__FILE__));
    

【问题讨论】:

如果错误消息表明 ffmpeg 未安装,并且您的脚本与 ffmpeg 无关,则您可能正在查看错误消息。 我刚刚注意到 arrarray_slice 中有一个额外的“r”。当我删除时,错误消失了。该 URL 似乎没有执行。到目前为止,我已将其范围缩小到函数“cron_call”。任何帮助将不胜感激! 我认为你只需要调试它。使用curl_error() 获取调用不运行的确切原因php.net/curl_error ...您的错误报告是否打开? 【参考方案1】:

在你的 cron_call 函数中,大约第 200 行,你有这个块:

//Add the url to the stack
$chs[$blog_id]=curl_init();
curl_setopt($chs[$blog_id], CURLOPT_URL, $cron_url);
curl_setopt($chs[$blog_id], CURLOPT_HEADER, 0);
curl_multi_add_handle($mh, $ch);

所以首先你在 $chs 数组中初始化你的 curl 处理程序。然后,您尝试将 $ch 变量添加到您的多处理程序中,而不是使用这些值,我认为这是 null。

通过正确的错误报告设置并检查curl_multi_add_handle 的结果,您可能会看到添加句柄操作失败。

来自php manual:

int curl_multi_add_handle ( resource $mh , resource $ch )

返回值

成功时返回 0,或 CURLM_XXX 错误代码之一。

编辑:也许我对解决方案不够清楚,但你需要更换

curl_multi_add_handle($mh, $ch);

curl_multi_add_handle($mh, $chs[$blog_id]);

【讨论】:

sql在db中替换wordpress站点(代码片段)

查看详情

WordPress 编辑器未更新文件:无法与站点通信以检查致命错误

】WordPress编辑器未更新文件:无法与站点通信以检查致命错误【英文标题】:WordPressEditornotupdatingfiles:Unabletocommunicatebackwithsitetocheckforfatalerrors【发布时间】:2019-03-1107:03:28【问题描述】:各位开发者和问题解决者您好。我在更新... 查看详情

无法在 Localhost (Xampp) Wordpress 站点中裁剪我的图标

】无法在Localhost(Xampp)Wordpress站点中裁剪我的图标【英文标题】:CannotcropmyfaviconinLocalhost(Xampp)Wordpresssite【发布时间】:2021-08-1114:11:22【问题描述】:嗨。最近我在localhostXampp服务器上做一个Wordpress项目。尽管我在裁剪图标时遇到... 查看详情

在functions.php中入队脚本后,Javascript无法在wordpress上运行

】在functions.php中入队脚本后,Javascript无法在wordpress上运行【英文标题】:Javascriptnotworkingonwordpressafterenqueuescriptinfunctions.php【发布时间】:2015-01-0319:10:52【问题描述】:我想添加一个javascript文件,但它不起作用。我正在使用Avada... 查看详情

移动 wordpress 站点后无法访问此站点

】移动wordpress站点后无法访问此站点【英文标题】:Thissitecan\'tbereachedaftermovingthewordpresssite【发布时间】:2017-08-1908:32:00【问题描述】:我使用的是WordPress,后来我备份了我的所有数据和数据库,以便将其移动到另一台计算机上... 查看详情

wordpress:如何指定原因“无法访问此站点”

】wordpress:如何指定原因“无法访问此站点”【英文标题】:wordpress:Howtospecifythecause"Thissitecan’tbereached"【发布时间】:2019-01-2614:44:14【问题描述】:我在AWSLightsail上使用了Let\'sEncryptforWordPress。我发现它已经过期了,因... 查看详情

在 phpMyAdmin db 或 WordPress 站点中添加/上传文本文件

】在phpMyAdmindb或WordPress站点中添加/上传文本文件【英文标题】:Add/UploadtextfilesinphpMyAdmindb,orWordPresssite【发布时间】:2017-01-0122:00:39【问题描述】:我不确定是否需要WordPress插件或PHP脚本。我正在尝试使用localhost/wordpress和xampp在W... 查看详情

IntersectionObserver 延迟脚本在浏览器控制台和 codePen 中工作,不在 WordPress 站点中

...ectionObserver延迟脚本在浏览器控制台和codePen中工作,不在WordPress站点中【英文标题】:IntersectionObserverdeferscriptworksinbrowserconsole&codePennotinWordPresssite【发布时间】:2021-11-1803:51:05【问题描述】:将JS加载到WordPress网站时出现问题... 查看详情

无法让简单的 jquery 脚本在 WordPress 页面上工作

】无法让简单的jquery脚本在WordPress页面上工作【英文标题】:UnabletogetsimplejqueryscripttoworkonWordPresspage【发布时间】:2015-07-0100:51:46【问题描述】:我正在尝试在我正在开发的WordPress网站上使用jQuery插件HCaptions,这样当鼠标悬停在... 查看详情

php重定向多站点wordpress(代码片段)

查看详情

php创建一个wordpress站点地图(代码片段)

查看详情

本地 WordPress 站点上的 cURL 返回:错误 6(无法解析主机)

】本地WordPress站点上的cURL返回:错误6(无法解析主机)【英文标题】:cURLonlocalWordPresssitereturns:Error6(Couldnotresolvehost)【发布时间】:2019-07-0623:18:32【问题描述】:我有一个本地WordPress安装运行在:https://catalogue3.test。请注意,所... 查看详情

WordPress/PHP 获取所有导航链接

】WordPress/PHP获取所有导航链接【英文标题】:WordPress/PHPgetallnavigationlinks【发布时间】:2021-05-0702:36:54【问题描述】:我正在开发一个小插件,我想知道如何在主导航中获取所有链接?想知道是否有可能在不知道特定主题中菜单... 查看详情

如何在自定义 PHP 脚本中调用 WordPress 函数

】如何在自定义PHP脚本中调用WordPress函数【英文标题】:HowtocallWordPressfunctionsinacustomPHPscript【发布时间】:2011-01-0616:26:05【问题描述】:我有一个PHP脚本,我想用它在WPMU中创建一个新博客。我无法调用像wpmu_create_user和wpmu_create_b... 查看详情

Wordpress 本地站点与实时站点集成

】Wordpress本地站点与实时站点集成【英文标题】:Wordpresslocalsiteintergratewithlivesite【发布时间】:2017-02-2219:49:44【问题描述】:所以我只想知道以下是否可行,以及是否有任何帮助或任何形式的帮助链接可用。我拥有/需要什么:... 查看详情

返回 Jquery 站点主页时无法访问脚本

】返回Jquery站点主页时无法访问脚本【英文标题】:CannotaccessscriptswhenreturntomainpageofJquerysite【发布时间】:2014-02-1421:53:54【问题描述】:好的,我从一开始就重新发布。我对此感到非常沮丧,我将一起放弃JQM。这不应该这么难。... 查看详情

无法通过移动浏览器访问本地主机上的 Wordpress 站点(Chrome devtools - 远程设备)

】无法通过移动浏览器访问本地主机上的Wordpress站点(Chromedevtools-远程设备)【英文标题】:Can\'taccesstoWordpresssiteonlocalhostviamobilebrowser(Chromedevtools-remotedevices)【发布时间】:2019-07-1508:56:25【问题描述】:我无法从移动浏览器访问... 查看详情

php在wordpress多站点网络中获取主站点的博客id(代码片段)

查看详情