### File Snippet URL Example
Source: https://perfmatters.io/docs/code-snippets
This example shows how a snippet is loaded as a file, including the snippet file name and version string. This method allows for better caching.
```url
snippetname.js?ver=xxxxxxxxx
```
--------------------------------
### Manual MU Plugin File Installation Path
Source: https://perfmatters.io/docs/mu-mode
If the `perfmatters_mu.php` file cannot be copied automatically, manually place it in the `/public/wp-content/mu-plugins/` directory via SFTP.
```path
/public/wp-content/mu-plugins/
```
--------------------------------
### Inline Script Tag Example
Source: https://perfmatters.io/docs/code-snippets
This is an example of an inline script tag for Perfmatters code snippets. It loads as part of the HTML page and includes the snippet ID, name, and type.
```html
```
--------------------------------
### PHP Parse Error Example
Source: https://perfmatters.io/docs/php-parse-error-syntax-error-unexpected
This is an example of the fatal error message encountered when a plugin is not compatible with the server's PHP version.
```php
Plugin could not be activated because it triggered a fatal error.
Parse error: syntax error, unexpected '?' in /www/wp-content/plugins/perfmatters/inc/functions.php on line xxxx
```
--------------------------------
### Add Multiple Preload Resources
Source: https://perfmatters.io/docs/filters
This example demonstrates adding various resource types (CSS, JS, font, image) to the preloads list using the `perfmatters_preloads` filter. Note the 'priority' option for images.
```PHP
add_filter('perfmatters_preloads', function($preloads) {
$preloads[] = array(
'as' => 'style',
'url' => 'https://example.com/style.css'
);
$preloads[] = array(
'as' => 'script',
'url' => 'https://example.com/script.js'
);
$preloads[] = array(
'as' => 'font',
'url' => 'https://example.com/font.woff2'
);
$preloads[] = array(
'as' => 'image',
'url' => 'https://example.com/image.jpg',
'priority' => 'high'
);
return $preloads;
});
```
--------------------------------
### Delay Inline JavaScript Example
Source: https://perfmatters.io/docs/delay-javascript
To delay inline JavaScript, enter a unique partial string found within the script tags. This example shows how to delay a script containing 'lazyLoadInstance'.
```html
```
--------------------------------
### Get Perfmatters Plugin Options with WP-CLI
Source: https://perfmatters.io/docs/clean-uninstall
Use this command to list available options for the Perfmatters plugin when using WP-CLI. This can help in understanding what data might be affected by a clean uninstall.
```bash
wp perfmatters get-options
```
--------------------------------
### Enable Brotli Compression in Nginx
Source: https://perfmatters.io/docs/enable-text-compression
Include this configuration in your Nginx file to enable Brotli compression. Ensure the ngx_brotli module is installed. Customize compression level, MIME types, and minimum length.
```nginx
http {
# Enable Brotli compression
brotli on;
# Set compression level (1-11, higher is better compression but slower)
brotli_comp_level 6;
# Specify MIME types to compress
brotli_types text/plain text/css application/javascript application/json application/xml text/html text/xml;
# Optional: Minimum file size for compression (in bytes)
brotli_min_length 256;
# Optional: Enable Brotli only for specific HTTP versions (e.g., HTTP/2)
# brotli_http_version 1.1;
# Optional: Add Vary header for browser compatibility
brotli_static on; # Serve pre-compressed .br files if available
add_header Vary Accept-Encoding;
}
```
--------------------------------
### Force Delay All JS on Single Posts
Source: https://perfmatters.io/docs/filters
Use `perfmatters_delay_js_behavior` to control the Delay JS behavior. This example forces the 'all' behavior on single posts.
```php
add_filter('perfmatters_delay_js_behavior', function($behavior) {
if(is_singular('post')) {
$behavior = 'all';
}
return $behavior;
});
```
--------------------------------
### Enable Brotli Compression in Apache
Source: https://perfmatters.io/docs/enable-text-compression
Add this configuration to your Apache config file to enable Brotli compression. Ensure the mod_brotli module is installed. Adjust quality, file types, and minimum length as needed.
```apache
# Enable Brotli compression
BrotliCompressionEnabled on
# Set compression quality (0-11, higher is better compression but slower)
BrotliCompressionQuality 5
# Specify file types to compress
AddOutputFilterByType BROTLI_COMPRESS text/html text/plain text/xml text/css text/javascript application/javascript application/json application/xml
# Optional: Set minimum file size for compression (in bytes)
BrotliCompressionMinLength 256
# Optional: Define browser support
# Remove Brotli for older browsers that don't support it
BrowserMatch "MSIE" no-brotli
Header append Vary User-Agent env=no-brotli
```
--------------------------------
### Example of PHP Error Handling in functions.php
Source: https://perfmatters.io/docs/add-php-to-wordpress
Demonstrates WordPress's built-in safeguard that can automatically roll back changes if a PHP error is detected after updating the `functions.php` file, preventing a site crash.
```php
add_filter( 'perfmatters_lazyload_fade_in_speed', function( $speed ) {
return 200 // Missing semicolon here
} );
```
--------------------------------
### Programmatically Exclude CSS from Minification
Source: https://perfmatters.io/docs/minify-css-wordpress
Use the `perfmatters_minify_css_exclusions` filter to programmatically exclude specific CSS files from minification. This example excludes 'example.css' only on single post pages.
```php
add_filter('perfmatters_minify_css_exclusions', function($exclusions) {
if(is_single()) {
$exclusions[] = 'example.css';
}
return $exclusions;
});
```
--------------------------------
### Modify Preload Tag HTML with Media Queries
Source: https://perfmatters.io/docs/filters
Use the `perfmatters_preloads_ready` filter to intercept and modify the generated preload tag HTML. This example adds a media query to preloads, excluding the first one.
```PHP
add_filter('perfmatters_preloads_ready', function($preloads) {
if(!empty($preloads) && is_array($preloads)) {
foreach($preloads as $index => $preload) {
if($index < 1) {
continue;
}
$new_preload = preg_replace('#media=([\\"]).+?\1#', '', $preload);
$new_preload = str_replace('
```
--------------------------------
### perfmatters_minify_js
Source: https://perfmatters.io/docs/filters
Control the Minify JavaScript feature, for example, to disable it on specific pages.
```APIDOC
## perfmatters_minify_js
### Description
Controls the Minify JavaScript feature. You can use this to disable JavaScript minification on specific pages or conditions.
### Method
`add_filter`
### Parameters
- **$bool** (boolean) - `true` to enable minification, `false` to disable.
### Code Example
```php
add_filter('perfmatters_minify_js', function($bool) {
if(is_singular('page')) {
return false;
}
return $bool;
});
```
```
--------------------------------
### Preload and Fetch Priority for Above-the-Fold Images
Source: https://perfmatters.io/docs/image-optimization
Use 'preload' and 'fetchpriority="high"' attributes to prioritize loading of critical images like logos or hero images. This helps decrease Largest Contentful Paint (LCP) times.
```html
```
--------------------------------
### perfmatters_minify_css
Source: https://perfmatters.io/docs/filters
Control the Minify CSS feature, for example, to disable it on specific pages.
```APIDOC
## perfmatters_minify_css
### Description
Controls the Minify CSS feature. You can use this to disable minification on specific pages or conditions.
### Method
`add_filter`
### Parameters
- **$bool** (boolean) - `true` to enable minification, `false` to disable.
### Code Example
```php
add_filter('perfmatters_minify_css', function($bool) {
if(is_singular('page')) {
return false;
}
return $bool;
});
```
```
--------------------------------
### Enable Perfmatters Cloudflare Early Hints
Source: https://perfmatters.io/docs/early-hints
In the Perfmatters plugin settings, enable 'Show Advanced Options', navigate to the 'Preloading' menu, and toggle on 'Cloudflare Early Hints'.
```text
Show Advanced Options
Preloading
Cloudflare Early Hints
```
--------------------------------
### Perfmatters MU Plugin File Not Found Warning
Source: https://perfmatters.io/docs/mu-mode
This warning indicates an issue with the `perfmatters_mu.php` file in the mu-plugins directory. Try toggling MU mode off and on again, or manually copy the file via SFTP.
```text
**Perfmatters Warning:** MU plugin file not found.
```
--------------------------------
### Disable Lazy Loading on Pages with perfmatters_lazyload
Source: https://perfmatters.io/docs/filters
Manipulate the lazy loading feature. This example disables all lazy loading specifically on pages.
```php
add_filter('perfmatters_lazyload', function($lazyload) {
if(is_singular('page')) {
return false;
}
return $lazyload;
});
```
--------------------------------
### Control Lazy Elements with perfmatters_lazy_elements
Source: https://perfmatters.io/docs/filters
Manipulate the lazy elements feature. This example disables lazy elements entirely on single posts.
```php
add_filter('perfmatters_lazy_elements', function($bool) {
if(is_single()) {
return false;
}
return $bool;
});
```
--------------------------------
### Import Settings with WP-CLI
Source: https://perfmatters.io/docs/wp-cli
Import Perfmatters settings from an exported JSON file using this command. Ensure the filepath is correct.
```bash
wp perfmatters import-settings
```
--------------------------------
### WordPress Shortlink Tag
Source: https://perfmatters.io/docs/remove-shortlink-wordpress
This is the default shortlink tag that appears in every WordPress installation. It provides a short URL for pages and posts.
```html
```
--------------------------------
### Enable Cloudflare Early Hints
Source: https://perfmatters.io/docs/early-hints
Toggle on the Early Hints feature in your Cloudflare account under 'Speed → Settings → Content Optimization'. Ensure your hosting provider's Cloudflare integration also supports this if applicable.
```text
Speed → Settings → Content Optimization
```
--------------------------------
### Disable Unused CSS on Search Results
Source: https://perfmatters.io/docs/filters
This example uses the `perfmatters_remove_unused_css` filter to disable the unused CSS removal feature on search result pages.
```PHP
add_filter('perfmatters_remove_unused_css', function($boolean) {
if (is_search()) {
return false;
}
return $boolean;
});
```
--------------------------------
### Comment Out Excluded Directories
Source: https://perfmatters.io/docs/remove-unused-css
Demonstrates how to comment out specific directory exclusions by prefixing them with '--'. This allows for easy toggling of exclusions during troubleshooting.
```plaintext
--/themes/
/plugins/
/wp-includes/
/uploads/
/cache/
```
--------------------------------
### Import Perfmatters Settings via WP-CLI
Source: https://perfmatters.io/docs/import-export
Use this WP-CLI command to import Perfmatters settings from a JSON file. Ensure you have a backup of your WordPress site before importing, as this action will overwrite existing settings.
```bash
wp perfmatters import-settings
```
--------------------------------
### Activate Perfmatters License with WP-CLI
Source: https://perfmatters.io/docs/wp-cli
Activate your Perfmatters license key using WP-CLI. Ensure the license key is entered in the plugin's settings first. Supports optional key input and multisite URL specification.
```bash
wp perfmatters activate-license
```
```bash
wp perfmatters activate-license []
```
```bash
wp perfmatters activate-license [] --url=
```
--------------------------------
### Control jQuery Deferral
Source: https://perfmatters.io/docs/filters
The `perfmatters_defer_jquery` filter allows manipulation of jQuery deferral. This example prevents jQuery from being deferred on single posts of any post type.
```php
add_filter('perfmatters_defer_jquery', function($bool) {
if(is_singular()) {
return false;
}
return $bool;
});
```
--------------------------------
### Multiple DNS Prefetch Tags for Common Resources
Source: https://perfmatters.io/docs/dns-prefetching
Implement DNS prefetching for various external resources like CDNs, Google Fonts, and analytics services. This helps speed up the loading of these assets.
```html
```
--------------------------------
### Exclude File Types from CDN
Source: https://perfmatters.io/docs/filters
Use the `perfmatters_cdn_extensions` filter to prevent specific file types from being rewritten to the CDN. This example excludes JavaScript files.
```php
add_filter('perfmatters_cdn_extensions', function($extensions) {
if(($key = array_search('js', $extensions)) !== false) {
unset($extensions[$key]);
}
return $extensions;
});
```
--------------------------------
### Add Favicon to WordPress Header
Source: https://perfmatters.io/docs/wordpress-add-code-to-header-footer
Example of adding a favicon link to the header. This code should be placed in the 'Add Header Code' box within Perfmatters.
```html
```
--------------------------------
### Disable script when URL contains '/category/'
Source: https://perfmatters.io/docs/regex
This Regex disables a script when the URL specifically contains '/category/'. It provides a more precise match than the previous example.
```regex
/\/category\//
```
--------------------------------
### Test with JS Features Off
Source: https://perfmatters.io/docs/test-perfmatters-off
Append this query string to a URL to disable Delay JS, Defer JS, and Minify JS features.
```url
?perfmattersjsoff
```
--------------------------------
### Programmatically Exclude JS from Minification
Source: https://perfmatters.io/docs/minify-javascript-wordpress
Use the `perfmatters_minify_js_exclusions` filter to programmatically exclude specific JavaScript files from minification, for example, only on single post pages.
```php
add_filter('perfmatters_minify_js_exclusions', function($exclusions) {
if(is_single()) {
$exclusions[] = 'example.js';
}
return $exclusions;
});
```
--------------------------------
### Enable MU Debug Mode
Source: https://perfmatters.io/docs/mu-mode
Append `?perfmatters&mu_mode=off` to your URL to force the page to load with MU settings disabled, allowing access to Script Manager if disabling a plugin breaks your site.
```url
https://yourdomain.com/**?perfmatters&mu_mode=off**
```
--------------------------------
### Conditionally Disable Perfmatters Output Buffer
Source: https://perfmatters.io/docs/filters
The `perfmatters_allow_buffer` filter allows selectively disabling the Perfmatters output buffer. This example disables it for URIs containing 'endpoint/'.
```php
if(strpos($_SERVER['REQUEST_URI'], 'endpoint/')) {
add_filter('perfmatters_allow_buffer', '__return_false');
}
```
--------------------------------
### Exclude a Script from Delay on Desktop
Source: https://perfmatters.io/docs/delay-javascript
Use the `perfmatters_delayed_scripts` filter to conditionally exclude a specific script from being delayed. This example prevents 'name_of_your_script' from being delayed on non-mobile devices.
```php
add_filter('perfmatters_delayed_scripts', function($scripts) {
if(($key = array_search('name_of_your_script', $scripts)) !== false) {
if(!wp_is_mobile()) {
unset($scripts[$key]);
}
}
return $scripts;
});
```
--------------------------------
### Default WordPress wlwmanifest Link
Source: https://perfmatters.io/docs/remove-wlwmanifest-link-wordpress
This is the default link tag that appears in every WordPress installation. It is used by Windows Live Writer and is generally unnecessary for most users.
```html
```
--------------------------------
### Activate Perfmatters Plugin with WP-CLI
Source: https://perfmatters.io/docs/wp-cli
Use this command to activate the Perfmatters plugin on your WordPress site via the command line.
```bash
wp plugin activate perfmatters
```
--------------------------------
### Limit Preload Critical Images on Pages
Source: https://perfmatters.io/docs/filters
The `perfmatters_preload_critical_images` filter lets you control the number of images preloaded. This example limits preloading to two images specifically on pages.
```PHP
add_filter('perfmatters_preload_critical_images', function($critical_images) {
if(is_page()) {
return 2;
}
return $critical_images;
});
```
--------------------------------
### Activate Perfmatters License Key with PHP Constant
Source: https://perfmatters.io/docs/activate-php-constant-wp-config
Add this code to your wp-config.php file, typically below the WP_DEBUG constant, to activate your Perfmatters license. The license deactivates if the key is removed.
```php
define('PERFMATTERS_LICENSE_KEY', '****');
```
--------------------------------
### Conditionally Disable Custom Login URL
Source: https://perfmatters.io/docs/filters
Disable the custom login URL functionality based on specific request URIs. This example excludes requests containing '?excludeme=true'.
```php
add_filter('perfmatters_login_url', function($bool) {
if(strpos($_SERVER['REQUEST_URI'], '?excludeme=true') !== false) {
return false;
}
return $bool;
});
```
--------------------------------
### Perfmatters MU Plugin Version Mismatch Warning
Source: https://perfmatters.io/docs/mu-mode
This warning suggests the `perfmatters_mu.php` file may not have been updated correctly. Attempt to reinstall the MU plugin through Script Manager or manually copy the file.
```text
**Perfmatters Warning:** MU plugin version mismatch.
```
--------------------------------
### Selectively Turn Off CDN Rewrite
Source: https://perfmatters.io/docs/filters
The `perfmatters_cdn` filter can manipulate the CDN rewrite feature. This example turns off CDN rewrite selectively for singular 'page' post types.
```php
add_filter('perfmatters_cdn', function($cdn) {
if(is_singular('page')) {
return false;
}
return $cdn;
});
```