Bird
Raised Fist0
Wordpressframework~10 mins

Caching strategies in Wordpress - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to enable page caching in WordPress using a plugin.

Wordpress
<?php
// Activate caching plugin
add_action('init', function() {
    if (!function_exists('[1]')) {
        return;
    }
    [1]();
});
Drag options to blanks, or click blank then click option'
Awp_cache_init
Bstart_cache
Ccache_start
Denable_cache
Attempts:
3 left
💡 Hint
Common Mistakes
Using a non-existent function name.
Confusing caching initialization with cache clearing.
2fill in blank
medium

Complete the code to clear the cache when a post is updated.

Wordpress
<?php
add_action('save_post', function($post_id) {
    if (wp_is_post_revision($post_id)) {
        return;
    }
    [1]();
});
Drag options to blanks, or click blank then click option'
Aclear_cache
Bwp_cache_clear_cache
Cwp_cache_flush
Dflush_cache
Attempts:
3 left
💡 Hint
Common Mistakes
Using a function that does not exist in WordPress.
Trying to clear cache with a function meant for starting cache.
3fill in blank
hard

Fix the error in the code to set cache expiration time correctly.

Wordpress
<?php
function set_cache_expiration() {
    $expiration = [1] * 60; // minutes to seconds
    wp_cache_set('cache_expiration', $expiration);
}
Drag options to blanks, or click blank then click option'
A600
B'10'
C10 * 60
D10
Attempts:
3 left
💡 Hint
Common Mistakes
Using a string instead of a number.
Multiplying by 60 twice.
4fill in blank
hard

Fill both blanks to create a transient cache with expiration.

Wordpress
<?php
set_transient('[1]', $data, [2]);
Drag options to blanks, or click blank then click option'
A'my_cache_key'
B3600
C60
D'cache_data'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a variable instead of a string for the key.
Setting expiration time too low or as a string.
5fill in blank
hard

Fill all three blanks to retrieve cached data safely.

Wordpress
<?php
$cache = get_transient([1]);
if ([2] === false) {
    $cache = fetch_data();
    set_transient([3], $cache, 3600);
}
Drag options to blanks, or click blank then click option'
A'my_cache_key'
B$cache
Dnull
Attempts:
3 left
💡 Hint
Common Mistakes
Using different keys for get and set.
Checking the wrong variable for cache miss.

Practice

(1/5)
1. What is the main purpose of caching in WordPress?
easy
A. To speed up the website by saving copies of pages or data
B. To increase the size of the website database
C. To delete old posts automatically
D. To change the website theme dynamically

Solution

  1. Step 1: Understand caching concept

    Caching stores copies of pages or data to avoid repeated processing.
  2. Step 2: Identify caching benefit

    By saving copies, the website loads faster for visitors.
  3. Final Answer:

    To speed up the website by saving copies of pages or data -> Option A
  4. Quick Check:

    Caching = Speed up site [OK]
Hint: Caching saves copies to load pages faster [OK]
Common Mistakes:
  • Thinking caching deletes content
  • Believing caching increases database size
  • Confusing caching with theme changes
2. Which of the following is the correct way to clear the WordPress cache using a plugin?
easy
A. Change the site URL in settings
B. Click the 'Clear Cache' button in the caching plugin settings
C. Delete all posts from the dashboard
D. Deactivate all plugins except caching

Solution

  1. Step 1: Identify cache clearing method

    Caching plugins provide a 'Clear Cache' button to remove saved copies.
  2. Step 2: Recognize incorrect options

    Deleting posts, changing URL, or deactivating plugins do not clear cache properly.
  3. Final Answer:

    Click the 'Clear Cache' button in the caching plugin settings -> Option B
  4. Quick Check:

    Clear cache = Use plugin button [OK]
Hint: Use plugin's clear cache button to refresh content [OK]
Common Mistakes:
  • Deleting posts thinking it clears cache
  • Changing site URL to fix cache
  • Deactivating unrelated plugins
3. Consider this code snippet in a WordPress theme's functions.php file:
function get_cached_data() {
  $cache_key = 'my_data';
  $data = wp_cache_get($cache_key);
  if ($data === false) {
    $data = 'Expensive data';
    wp_cache_set($cache_key, $data, '', 3600);
  }
  return $data;
}

echo get_cached_data();

What will this code output on the first call?
medium
A. An error message
B. false
C. null
D. Expensive data

Solution

  1. Step 1: Check cache retrieval

    On first call, wp_cache_get returns false because cache is empty.
  2. Step 2: Set and return data

    Since cache is empty, 'Expensive data' is set and returned.
  3. Final Answer:

    Expensive data -> Option D
  4. Quick Check:

    First call returns new data [OK]
Hint: First cache get returns false, so data is set and returned [OK]
Common Mistakes:
  • Assuming cache returns null instead of false
  • Expecting error on missing cache
  • Thinking cache returns old data on first call
4. You added caching to your WordPress site, but after updating content, visitors still see old pages. What is the likely problem?
medium
A. The site URL is incorrect
B. The theme is incompatible with WordPress
C. Cache was not cleared after content update
D. The database is corrupted

Solution

  1. Step 1: Understand caching effect

    Caching saves old page copies to speed up loading.
  2. Step 2: Identify update issue

    If cache is not cleared after content changes, old pages show to visitors.
  3. Final Answer:

    Cache was not cleared after content update -> Option C
  4. Quick Check:

    Old pages = Cache not cleared [OK]
Hint: Always clear cache after content updates [OK]
Common Mistakes:
  • Blaming theme for caching issues
  • Assuming database corruption causes old pages
  • Thinking site URL affects caching
5. You want to improve your WordPress site's speed by caching database query results for 10 minutes. Which approach is best?
hard
A. Use wp_cache_set() with a 600-second expiration after fetching query results
B. Disable all caching plugins and rely on browser cache only
C. Manually save query results in a text file and read it each time
D. Increase PHP memory limit to store all queries in RAM

Solution

  1. Step 1: Identify caching method for query results

    wp_cache_set() stores data in WordPress object cache with expiration time.
  2. Step 2: Set expiration for 10 minutes

    600 seconds equals 10 minutes, so setting expiration to 600 caches results properly.
  3. Step 3: Evaluate other options

    Disabling plugins loses caching benefits; manual file saves are inefficient; increasing memory doesn't cache queries automatically.
  4. Final Answer:

    Use wp_cache_set() with a 600-second expiration after fetching query results -> Option A
  5. Quick Check:

    Cache query with wp_cache_set + 600s [OK]
Hint: Use wp_cache_set with expiration for timed caching [OK]
Common Mistakes:
  • Disabling caching plugins thinking it helps
  • Trying manual file caching instead of built-in cache
  • Increasing memory without caching logic
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard