Home  >  Article  >  CMS Tutorial  >  How to achieve real-time refresh in wordpress

How to achieve real-time refresh in wordpress

藏色散人
藏色散人Original
2019-07-15 09:35:003304browse

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(&#39;publish_post&#39;, &#39;refresh_front_page&#39;, 0);    // 发布或者更新日志时候刷新首页
    add_action(&#39;edit_post&#39;, &#39;refresh_front_page&#39;, 0);       // 有新留言或者留言被删除的时候刷新首页
    add_action(&#39;delete_post&#39;, &#39;refresh_front_page&#39;, 0);     // 删除日志时候刷新首页
    add_action(&#39;publish_phone&#39;, &#39;refresh_front_page&#39;, 0);   // 通过 email 发布日志之后刷新首页
      
    function refresh_front_page(){
        $front_page_id = get_option(&#39;page_on_front&#39;);       // 获取显示首页的页面 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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn