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

查看:23
本文介绍了在 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 事件!

一种可持续的方法是编写一个插件,在激活/停用时调度/clear_schedules cron.

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-管理界面.

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天全站免登陆