Home > Article > CMS Tutorial > How to achieve real-time refresh in wordpress
How does wordpress refresh the homepage in real time
WP Super Cache is the best recommended WordPress caching plug-in. The entire page directly generates HTML files, so Apache does not need to parse PHP scripts. By using this plug-in, your WordPress blog will be significantly faster.
But if the WordPress homepage displays not the latest log, but a page, and this page contains a real-time update part, then there will be a problem, then this page will not be updated in real time.
Here is a trick that allows you to use the static cache plug-in of WP Super Cache to speed up and update the homepage in real time. WP Super Cache edits the cache through the wp_cache_post_edit function. The parameter is the ID of the log or page.
So we first get the ID of the page used to display the homepage, and then refresh the homepage when the log is updated or deleted, or when there is a new message (if the homepage does not contain the latest message, this is not necessary).
The approximate code is as follows:
<?php add_action('publish_post', 'refresh_front_page', 0); // 发布或者更新日志时候刷新首页 add_action('edit_post', 'refresh_front_page', 0); // 有新留言或者留言被删除的时候刷新首页 add_action('delete_post', 'refresh_front_page', 0); // 删除日志时候刷新首页 add_action('publish_phone', 'refresh_front_page', 0); // 通过 email 发布日志之后刷新首页 function refresh_front_page(){ $front_page_id = get_option('page_on_front'); // 获取显示首页的页面 ID wp_cache_post_edit($front_page_id); // 刷新该页 } ?>
Copy this code to the theme’s functions.php.
Of course, if you want to refresh a page, you can also use wp_cache_post_edit to refresh it. The parameter is the ID of the page.
For more WordPress technical articles, please visit WordPress Tutorialcolumn!
The above is the detailed content of How to achieve real-time refresh in wordpress. For more information, please follow other related articles on the PHP Chinese website!