Hot answers tagged

36

Object cache everywhere WordPress tries to reduce the number of database queries as much as possible. For example, anytime you get a meta field or a taxonomy field, before querying the database, WordPress looks if that that was already queried and stored in cache, and returns it from there instead of querying the database. The "cache job" is done via ...


35

WordPress, by default, does a form of "Object Caching" but its lifetime is only a single page load. Options are actually a really good example of this. Check out this answer for more info. The summary: A page starts All options are loaded with a simple SELECT option_name, option_value from $wpdb->options statement Subsequent requests for those options (...


23

The mix up is usually because these extensions are about two unrelated technologies: opcode caching and key-value data store. For WordPress you prefereably want both. Opcode caching is really the "normal" way to run PHP (and lack of it is essentially crippled shared hosting way). Data store can *(and should) be used by WordPress object cache plugins for ...


19

You don't have to use @import. It's best not to, actually. Using an enqueued approach is probably better all around. Here's the relevant part of twentythirteen's code: function twentythirteen_scripts_styles() { ... // Loads our main stylesheet. wp_enqueue_style( 'twentythirteen-style', get_stylesheet_uri(), array(), '2013-07-18' ); ... } add_action(...


18

Should I use Transient API at all here? No. In a stock WordPress install transients are stored in the wp_options table, and only cleaned up during core upgrades. Suppose you have 50,000 posts, that's 50,000 additional rows in the options table. Obviously they're set to autoload=no, so it's not going to consume all your memory, but there's another caveat. ...


17

It's because you're retrieving it via URL, but filemtime() requires a path. Use get_stylesheet_directory() instead. That returns a path: function pro_styles() { wp_enqueue_style( 'child-style', get_stylesheet_directory_uri() .'/child-style.css', array(), filemtime(get_stylesheet_directory() .'/child-style.css'), 'all' ); } add_action( 'wp_enqueue_scripts', ...


16

Today I run a test over my db to explore the speed difference between accessing a key from options, custom table & transients. I ran the test for 1000 times and following is the time taken to run 1000 get operations: Keep in mind that the options table is used for both options and transients on most systems, and that table has been optimised, with ...


13

The wp_cache_*() functions are non-persistent caches which means that they only last during the current page request. This would be beneficial if you're requesting the same information multiple times during page load. For example, if you're showing recent posts in the header, content section, and sidebar ( calling the same code 3 times to retrieve recent ...


12

Catching the weather API remote data The msg, you're showing in your question is basically the result from the weather API. And it says, that there's no data available for your location. The first thing you want to do is some research in the Codex and the "WP HTTP API". The right/WP way to grab remote data After you've learned about the WP HTTP API, you'...


12

Not All WP Code Is Public Code If you are going to release something public, then all the things kovshenin said are perfectly valid. Things are different if you are going to write private code for yourself or your company. External Object Cache Is A Big Benefit, In Any Case To set a external persistent object cache is very recommended, when you can. All ...


11

To flush a single page by post id in w3tc v0.9.3 I found this worked: if (function_exists('w3tc_pgcache_flush_post')){ w3tc_pgcache_flush_post($post_id); }


11

@dalbaeb's comment eventually lead to insightful discussions and a feasible solution. Thanks a lot! I believe the reason my child theme CSS was loaded using 'ver=<parent-theme-version> was because I had followed the WP Codex on child themes 1:1. My functions.php contained this: add_action('wp_enqueue_scripts', 'theme_enqueue_styles'); function ...


11

At the moment, it is not possible. When 'pre_get_posts' runs, is too late to stop WP_Query to perform a query. WordPress itself, when you try to query a taxonomy that does not exists, adds AND (0 = 1) to the WHERE clause of the SQL query, to ensure it returns no results very quickly... There's a trac ticket with a patch that will probably lands in core ...


10

It sounds like you need to change the URLs for the old images. The easiest & quickest way of doing that is to do a find and replace on the MySQL database. You can use PHPMyAdmin (usually provided by your web host) or other MySQL editing software such as Sequel Pro to make changes to your MySQL database without touching the WordPress admin area. This ...


9

I couldn't get toscho's answer to work for the current version of WordPress (4.7.2), and that seems to be because the TinyMCE init array has a cache_suffix set to 'wp-mce-' . $tinymce_version. So instead, you can just overwrite that with the tiny_mce_before_init filter, like so: function wpse33318_tiny_mce_before_init( $mce_init ) { $mce_init['...


9

I would not be storing 50+ meg as a transient. I would look into storing it in the filesystem somehow, or creating my own specific db table to store the data. Consider that if you're storing the transient in the database, then every time you pull it, that's 50 meg of data that has to be sent from the DB to the webserver for processing. And that's 50 meg of ...


9

My previous answer is overly complicated and potentially doesn't respect the parent theme's dependency chain (see note in other answer). Here's another much simpler take that should work much better: function use_parent_theme_stylesheet() { // Use the parent theme's stylesheet return get_template_directory_uri() . '/style.css'; } function ...


9

How to trigger an oembed cache regeneration The default cache time is 24 hours and we can adjust it with the oembed_ttl filter. But as you've noticed, expired cache is not enough to trigger a cache regeneration. The reason is this line in the WP_Embed class: if ( $this->usecache || $cached_recently ) { so to trigger a regeneration, we need both ...


9

This is directly related to, and a consequence of WordPress.com VIP At VIP, we deal with sites that range in the hundreds of millions of page views per week. As a result, situations that can slow down your site are much more noticeable at that scale than on a small shared host, but this still impacts a site with moderate traffic in the region of 100 or so ...


8

After searching for a simple solution many times i decided to find something that works! so... after thinking about it i found a great way to override caching while developing new websites... (and its easy). What we need is to tell wp this is a new CSS version like this... Before changes: wp_enqueue_style( 'maincss', get_template_directory_uri() ...


8

Is this ok or should I just store it in an array form so I can just directly access it without using json_decode? In your case store it directly means store it serialized. Because $data is an array and cannot be stored as is, but converted to a string. Even if you access it directly like: $data = get_transient('amazon_items'); print_r( $data[0] ); the ...


7

Yeah, use 'nopaging' => true http://codex.wordpress.org/Class_Reference/WP_Query#Pagination_Parameters $Poz = array( 'posts_per_page' => 3, 'orderby' => 'date', 'order' => 'DESC', 'update_post_term_cache' => false, 'update_post_meta_cache' => false, 'nopaging' => true, ); $the_query = new WP_Query( $Poz )...


7

Caching the WordPress Menu also gives you a performance boost. Especially if you have a lot of Pages or a giant Menu Structure, this should be considered. Do it in 2 easy steps. At first, create a function that gets or creates the menu, instead of calling wp_nav_menu directly. function get_cached_menu( $menuargs ) { if ( !isset( $menuargs['menu'] ) ) {...


7

You could delete an existing cache for your option before you call get_option(): $GLOBALS['wp_object_cache']->delete( 'your_option_name', 'options' ); $value = get_option( 'your_option_name' );


7

Have you looked at WP_Object_Cache? If you suspect there is unwanted caching happening in the code that generates the admin panel then you might be able to use functions from the WP_Object_Cache to clear it. WP_Object_Cache is WordPress' class for caching data which may be computationally expensive to regenerate, such as the result of complex database ...


7

Yes, social counts are a great use case for using transients. Aside from the slow page loading, as you mentioned, this will also help prevent you from blowing through API limits if the external requests are being made on every page load. Let's say you have it set to cache for 30 minutes and the check for cache expiration is occurring when the data is ...


7

Thanks to @chrisguitarguy's answer, you can control the http headers sent by Wordpress via the "send_headers" hook. Here is the function I added to my theme's functions.php file, and that solved the issue with the Varnish server. function varnish_safe_http_headers() { header( 'X-UA-Compatible: IE=edge,chrome=1' ); session_cache_limiter(''); ...


6

Forgot I could go this route: $wpdb->query( "DELETE FROM `$wpdb->options` WHERE `option_name` LIKE ('_transient_galleries_%')" );


6

For development I would advise to always work with WP_DEBUG set to true and do the following: $key = 'transient_key'; if( !WP_DEBUG && ( false !== ($transient = get_transient($key)) ){ /* Generate transient manually */ $expiration = 24*60*60;//How long to keep for set_transient($key,$transient, $expiration); } In general - it should be ...


Only top voted, non community-wiki answers of a minimum length are eligible