自动化WordPress中的每周后台功能 [英] Automate a weekly background function in WordPress

查看:51
本文介绍了自动化WordPress中的每周后台功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为一个在其网站上列出名人的客户开发WordPress网站.每个名人资料都显示一张社交媒体统计信息表,该表通过使用相应用户句柄的相关API进行抓取.

I'm developing a WordPress site for a client who is listing celebrities on her site. Each celebrity profile shows a table of social media stats which is grabbed via the relevant API using the respective user handle.

例如,通过Twitter API检索Twitter关注者计数.

For example the Twitter follower count is retrieved via the Twitter API.

一切正常,但是页面加载时间很痛苦.

Everything works as it should but the page load times are painful.

我现在开始认为,服务器每周一次在后台进行API查询并相应地更新数据库会更有意义,这样我们就不必在每次页面加载时都查询API.

I'm now beginning to think it would make much more sense for the server to make the API queries in the background once a week and update the database accordingly, that way we're not querying the API on every page load.

我将如何构建这样的东西?

How would I go about building such a thing?

我能够编写一个函数,该函数将使所有名人都在获取各种数据,但是我该如何自动执行并确保将数据输入到数据库中.

I'm able to write a function that will loop though all the celebrities grabbing the various data but how do I automate this and ensure the data is entered into the database.

我显然不是在寻找完整的答案,而只是在寻求有关该方法的指导,因为这不是我以前做过的事情,并且我无法在不知道相关术语/名称的情况下进行搜索.有点鸡/蛋.

I'm obviously not looking for a complete answer just some guidance on the approach as it's not something I've ever done before and I'm unable to search for something without knowing the relevant terms/names. It's a bit chicken/egg.

推荐答案

安排cron事件!

一种可持续的方法是编写一个插件,该插件在激活/停用时可以安排cron的计划/clear_schedules.

A sustainable way to do that is to write a plugin that schedules/clear_schedules the cron when activated/deactivated.

这是您可以使用的插件.

Here's a plugin that you could use.

您需要将其放置在文件(例如custom_cron_event.php)中,然后将其通过FTP传输到/plugins文件夹中,或者将其放置在文件中并进行压缩(ZIP),以通过/wp-admin界面.

You will need to either place this in a file (such as custom_cron_event.php) and FTP it to your /plugins folder, or place it in a file and compress (ZIP) the file to upload it to your website through the /wp-admin interface.

您可以安装 Cron Manager 之类的插件,以验证cron事件是否为预定的." custom_cron_event"(或您为函数选择的任何名称),如果您已激活插件并且可以正常工作,则应将其列为计划的cron.

You can install a plugin like Cron Manager to verify that the cron event is scheduled. "custom_cron_event" (or whatever name you choose for your function) should be listed as a scheduled cron if you've activated the plugin and it is working correctly.

<?php
/*
Plugin Name: Custom Plugin
Plugin URI: 
Description: Adds function on cron
Author: 
Version: 1.0
*/

/*  
 * When this plugin is activated, schedule
 */ 
    function activate_custom_cron(){
        wp_schedule_event( time(), 'daily', 'do_custom_cron_event');
    }
    register_activation_hook(__FILE__, 'activate_custom_cron');

/*  
 * When this plugin is deactivated, clear_schedule cron
 */ 
    function deactivate_custom_cron(){  
        wp_clear_scheduled_hook('do_custom_cron_event');    
    }
    register_deactivation_hook(__FILE__, 'deactivate_custom_cron');


/*  
 * Function
 */ 
    function custom_cron_event() {

    /*
     * This is where your function goes
     */

    }   
    add_action( 'do_custom_cron_event', 'custom_cron_event' );

?>

我回答了类似的问题在这里.

这篇关于自动化WordPress中的每周后台功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆