Skip to content

FAQ: Developers

maxicus edited this page Mar 26, 2018 · 6 revisions

When are you going to translate this plugin into more languages?

When you help us! :-) Localization is tricky and we're happy to work with those willing to support the WordPress community. We will most likely follow the trends of WordPress itself to determine which languages are addressed first.

How do I implement page fragment caching?

First you need to define W3TC_DYNAMIC_SECURITY in your wp-config.php file.

define('W3TC_DYNAMIC_SECURITY', 'somesecurestring');

Edit your templates with the following syntax to ensure that dynamic features remain so. Replace W3TC_DYNAMIC_SECURITY with content of the constant or use echo to print constant:

  • Example 1: <!-- mfunc mysecurestring any PHP code --><!-- /mfunc mysecurestring -->
  • Example 2: <!-- mfunc mysecurestring -->any PHP code<!-- /mfunc mysecurestring -->
  • Example 3: <!--MFUNC mysecurestring -->echo rand();<!--/mfunc mysecurestring -->
  • Example 4: <!-- mclude mysecurestring path/to/file.php --><!-- /mclude mysecurestring -->
  • Example 5: <!-- mclude mysecurestring -->path/to/file.php<!-- /mclude mysecurestring -->

Be aware that WordPress functions will not be available.

How can I prevent caching directly in my templates etc?

Several constants are available for these purposes:

  • define('DONOTCACHEPAGE', true); Disables page caching for a given page.
  • define('DONOTCACHEDB', true); Disables database caching for given page.
  • define('DONOTMINIFY', true); Disables minify for a given page.
  • define('DONOTCDN', true); Disables content delivery network for a given page.
  • define('DONOTCACHEOBJECT', true); Disables object cache for a given page.

How can I flush the cache without using the WP Admin interface?

It's possible to empty the entire cache or simply purge the cache of a single post / page:

  • Purge the entire cache of html content:
if (function_exists('w3tc_flush_posts')) {
    w3tc_flush_posts();
}
  • Purge a single post / page by passing it's ID:
if (function_exists('w3tc_flush_post')) {
	w3tc_flush_post($post_id);
}

How do I programmatically modify user agent groups?

There are two ways change user agent groups, functions or filter.

Using functions:

$group_config = w3tc_get_user_agent_group($group_name); //name of the group
$group_config['theme'] = 'newtheme';
$group_config['redirect'] = '';
$group_config['agents'] = array(escaped_string, [...]);
$group_config['enabled'] = true;

w3tc_save_user_agent_group($group_name, $group_config['theme'], $group_config['redirect'], $group_config['agents'], $group_config['enabled']);

Using filter:

function my_w3tc_mobile_groups($w3tc_groups) {
    // any operations
    // clear all groups example
    $w3tc_groups = array();
    // delete all groups and add new example
    $w3tc_groups = array(....);
    // merge groups example:
    $w3tc_groups = array_merge($w3tc_groups, array(
        'good_browsers' => array(
                    'theme' => 'good_theme/good_theme',
                    'enabled' => true,
                    'redirect' => '',
                    'agents' => array('firefox', 'chrome')
                    ),
        'bad_browsers' => array(
                    'theme' => 'bad_theme/bad_theme',
                    'enabled' => true,
                    'redirect' => '',
                    'agents' => array('msie', 'opera')
                    )
    ));
    return $w3tc_groups;
}
add_filter('w3tc_mobile_groups', 'my_w3tc_mobile_groups');

How do I programmatically modify referrer groups?

There are two ways change referrer groups, functions or filter.

Using functions:

$group_config = w3tc_get_referrer_group($group_name); //name of the group
$group_config['theme'] = 'newtheme';
$group_config['redirect'] = '';
$group_config['referrers'] = array(escaped_string, [...]);
$group_config['enabled'] = true;
w3tc_save_referrer_group($group_name, $group_config['theme'], $group_config['referrer'], $group_config['referrers'], $group_config['enabled']);

Using filters:

function my_w3tc_referrer_groups($w3tc_groups) {
    // any operations
    // clear all groups example
    $w3tc_groups = array();

    // delete all groups and add new example
    $w3tc_groups = array(....);

    // merge groups example:
    $w3tc_groups = array_merge($w3tc_groups, array(
    'search_referrers' => array(
                        'theme' => 'search_theme',
                        'enabled' => true,
                        'redirect' => '',
                        'referrers' => array('google\.com')
                        ),

    'other_referrers' => array(
                        'theme' => 'video_theme',
                        'enabled' => true,
                        'redirect' => '',
                        'referrers' => array('youtube\.com')
                        )
    ));

    return $w3tc_groups;
}
add_filter('w3tc_referrer_groups', 'my_w3tc_referrer_groups');</pre>

How do I remove the W3 Total Cache HTML comment?

Define APP_REQUEST in wp-config.php for example:

if ($_SERVER['REQUEST_URI'] == '/my-specific-page/') {
    define('APP_REQUEST', 'true');
}

How can I issue delete and reload APC commands over HTTP/WP-CLI?

Functions required for deleting and reloading over HTTP is found in inc/w3-total-cache-api.php.

Call w3tc_opcache_flush_file( true ) to reload file.
Call w3tc_opcache_flush( true ) to flush all files.

To do it locally with WP-CLI: Call wp w3-total-cache opcache_flush_file local file1 Call wp w3-total-cache opcache_flush local

How do I add new extensions?

Refer to w3-total-cache/extensions/GenesisAdmin.php to see how to create an extension by example.

// Adding a basic function. See GenesisAdmin.php for how to setup settings screen.
add_filter('w3tc_extensions', 'custom_extension');
function custom_extension($extensions) {
        $extensions['some.theme'] = array (
            'name' => 'Custpom Framework Extension',
            'author' => 'Bluw Widgets',
            'description' => 'Adds specific caching capabilities.',
            'author_uri' => 'http://example.com',
            'extension_uri' => 'http://example.com',
            'extension_id' => 'some.theme',
            'settings_exists' => false,
            'version' => '0.1',
            'enabled' => true,
            'requirements' => array(),
            'path' => 'your-plugin-folder/extensions/CustomExtension.php'
        );
}