Wordpress plug-in bug troubleshooting (this article is for you), wordpressbug

Source: Internet
Author: User

Wordpress plug-in bug troubleshooting (this article is for you), wordpressbug

This article is written to myself.

On Wednesday, I found a very strange situation when maintaining a wordpress project page of the company: after I tried to update a page on the website, in the wordpress background editor, I found that the content does not replace the URL of the image as I expected. (If the Baidu cloud plug-in is enabled for the website, the plug-in will capture the image in the article, then upload the image to Baidu cloud and replace the address in the article). However, when I checked the front-end page, I found that the page displayed normally and the image URLs on the page were also replaced. In a word, the front-end article display page is inconsistent with the content in the background editor. This bug is really strange. Next we will record the process of troubleshooting this bug to be forgotten.

1. We suspect that other plug-ins on the website conflict with Baidu cloud plug-ins.

This is actually possible because many wordpress plug-ins are installed on the website. But after thinking about it, it seems that no other plug-ins will conflict with Baidu cloud plug-ins.

2. Check the wp_posts table in the database with the technical boss.

The wp_posts table structure is as follows:

The following fields in this table are very important:

Post_status (article status) values can be: trash (Recycle Bin), publish (publish), inherit (inherit), draft (draft), and auto-draft (automatically saved draft)

Post_type (document type) values can be post, page, revision, attachement, and nav_menu_item)

Post_parent (parent Article ID ).

Post_status determines whether the record is published. post_type determines whether the record is a draft, article, page, or revision of the document. The article can be modified many times. Therefore, it has many revisions, post_parent stores the ID of the parent document of the current record (this is used only when post_status is inherit or draft, otherwise the default value is 0 ).

The debugging process is basically like this: each time you click the "Update" button, you can view the changes in the wp_posts table. The results show that:

(1) The page published on the website (assuming the ID is 1234), and the corresponding records in the database (post_status is publish and post_type is page) have been modified normally. This indicates that the content in the editor is not the 1234 record, but a revision of the article ID = 1234. Check several times.

(2) which version does it obtain? After several troubleshooting, the editor always obtains a revision before the latest revision. The exact description should be like this. Each time you click an update, there will be one revision and one auto-draft in the data table. The article content in the latest revision is the correct content that has been replaced by img src, while auto_draft is consistent with that in the editor. In the editor status, wordpress automatically saves frequently. Each time a new auto draft is generated, the previous auto draft is deleted. The editor does not obtain the content from auto draft either. In short, I didn't figure out which revision it took. But it should take the latest revision, so that it can be consistent with the post content with ID = 1234.

3. What about caching?

I started to suspect that it was a browser cache. After trying to clear the browser cache, I found that the problem still exists. That is, the cache on the server. restart the memcached: service memcached restart on the server and find that the content in the editor is normal. It is really a ghost of caching.

Solve the bug:

This bug is solved by adding the latest revision updates and cache refreshing code to the Baidu cloud plug-in to update the database.

1 // update the post_content Field 2 $ revisions = wp_get_post_revisions ($ post_id); 3 krsort ($ revisions ); 4 $ newest_id = reset (array_keys ($ revisions); 5 $ flag = $ wpdb-> update ($ wpdb-> posts, array ('Post _ content' => $ post-> post_content), array ('id' => $ newest_id )); 6 // update the post_content field of the posts data table 7 $ wpdb-> update ($ wpdb-> posts, array ('Post _ content' => $ post-> post_content ), array ('id' => $ post-> ID); 8 // refresh cache 9 wp_cache_flush ();

The following are some of the knowledge about the cache and global variables of wordpress:

(1) How is the wp_cache_flush () function defined?

 1 /** 2  * Removes all cache items. 3  * 4  * @since 2.0.0 5  * 6  * @see WP_Object_Cache::flush() 7  * @global WP_Object_Cache $wp_object_cache Object cache global instance. 8  * 9  * @return bool False on failure, true on success10  */11 function wp_cache_flush() {12     global $wp_object_cache;13 14     return $wp_object_cache->flush();15 }

Then, how is the $ wp_objecg_cache-> flush () function defined?

 1     /** 2      * Clears the object cache of all data. 3      * 4      * @since 2.0.0 5      * @access public 6      * 7      * @return true Always returns true. 8      */ 9     public function flush() {10         $this->cache = array();11 12         return true;13     }

How does this $ this-> assign a value during cache initialization? Or how is it defined in the class?

1     /**2      * Holds the cached objects.3      *4      * @since 2.0.05      * @access private6      * @var array7      */8     private $cache = array();

$ This-> cache is an array, while wp_cache_flush () Only assigns this array an empty array.

(2) what data is cached in WP_Object_Cache? How is it cached?

First, let's take a look at the instructions in the official documentation based on the instructions in the cache. php file header annotations (with annotations:

WP_Object_CacheIs WordPress 'class for caching data which may be computationally expensive to regenerate, such as the result of complex database queries. The object cache is defined inwp-includes/cache.php.

Do not use the class directly in your code when writing plugins, but use the wp_cache functions listed below.

By default, the object cache is non-persistent. this means that data stored in the cache resides in memory only and only for the duration of the request. cached data will not be stored persistently stored SS page loads unless you install a persistent caching in.

Pay attention to the red text above, to the general idea: WP_Object_Cache cache needs to repeatedly generate data that consumes computing resources, such as database query results. By default, WP_Object_Cache is not sustainable. It only exists within the time of a request (the cache will be deleted once the request ends). If you want to cross-page (cross-request) to use these caches, you need to install a sustainable cache plug-in.

(3) What cache plug-ins are installed on the website? Memecached, but not found in the plug-in list.

Then Baidu found out how wordpress enabled Memcached memory cache in this article, and found that it was installed under the wp-content directory.

It seems that wordpress still has a lot to learn.

 

Refer:

Wordpress official reference manual

How does wordpress enable Memcached memory cache?

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.