Developers can tweak WP STAGING using its available actions and filters. This article documents the current hooks and shows example usage to customize the cloning and pushing process to your needs.
Contents
- 1 Change Staging Site Title
- 2 WP STAGING Hooks Plugin
- 3 Exclude Tables From Search & Replace operation
- 4 CLONING
- 4.1 Run an Action on the Staging Site After Cloning
- 4.2 Exclude Rows From Search & Replace in wp_options
- 4.3 Exclude Strings From Search & Replace
- 4.4 Change Search & Replace Parameters
- 4.5 Exclude Folders
- 4.6 Multisite: Exclude Folders
- 4.7 Prevent search & replace of table prefix from custom options under wp_options
- 5 PUSHING
Change Staging Site Title
Add the code below into the functions.php
in
wp-content/themes/theme-name/functions.php
function wpstg_get_title(){ return 'DEV'; } add_filter('wpstg_staging_site_title', 'wpstg_get_title');
WP STAGING Hooks Plugin
The next hooks and filters can be used by installing a separate plugin, named the WP STAGING Hooks plugin:
Download the plugin with all available WP STAGING hooks from:
https://github.com/WP-Staging/wp-staging-hooks
(Red colored items need to be changed to your use case.)
Exclude Tables From Search & Replace operation
Use this if the search & replace process eats up a lot of your available memory and the cloning or pushing process failed with a ‘memory exhausted error‘. You can also use this to improve the speed of the cloning and pushing process.
Exclude tables that do not need any search & replacement of any links! These can be tables that contain visitor stats, IP addresses or something similar. After excluding those tables you can increase the DB Search & Replace limit in WP STAGING settings to a higher value to get better performance.
function wpstg_searchreplace_excl_tables($default){ $tables = array('_posts', '_postmeta'); return array_merge($default, $tables); } add_filter('wpstg_searchreplace_excl_tables','wpstg_searchreplace_excl_tables');
CLONING
Run an Action on the Staging Site After Cloning
add_action( 'wpstg.clone_first_run', array($this, 'wpstg_execute_after_cloning' ), 10); function wpstg_execute_after_cloning() { // add your code }
Exclude Rows From Search & Replace in wp_options
function wpstg_clone_searchreplace_excl_rows($default){ $rows = array('siteurl', 'home'); return array_merge($default, $rows); } add_filter('wpstg_clone_searchreplace_excl_rows','wpstg_clone_searchreplace_excl_rows');
Exclude Strings From Search & Replace
function wpstg_clone_searchreplace_excl(){ return array('blog.localhost.com','blog1.localhost.com'); } add_filter('wpstg_clone_searchreplace_excl','wpstg_clone_searchreplace_excl');
Change Search & Replace Parameters
function wpstg_cloning_custom_params($args){ // Default values - Can be changed $args['search_for'] = array_merge( $args['search_for'], array('SEARCHSTRING', 'SEARCHSTRING') ); $args['replace_with'] = array_merge( $args['replace_with'], array('REPLACESTRING','REPLACESTRING2') ); $args['replace_guids'] = 'off'; $args['dry_run'] = 'off'; $args['case_insensitive'] = false; $args['replace_guids'] = 'off'; $args['replace_mails'] = 'off'; $args['skip_transients'] = 'on'; return $args; } add_filter('wpstg_clone_searchreplace_params', 'wpstg_cloning_custom_params');
Exclude Folders
/** * Excluded folders relative to the wp-content folder */ function wpstg_exclude_folders_custom($args){ $folders = array('plugins/wordpress-seo', 'custom-folder'); return array_merge($args, $folders); } add_filter('wpstg_clone_excl_folders', 'wpstg_exclude_folders_custom');
Multisite: Exclude Folders
** * Excluded folders relative to the wp-content folder */ function wpstg_exclude_folders_custom($args){ $folders = array('plugins/wordpress-seo', 'themes/custom-folder'); return array_merge($args, $folders); } add_filter('wpstg_clone_mu_excl_folders', 'wpstg_exclude_folders_custom');
Prevent search & replace of table prefix from custom options under wp_options
function wpstg_excl_option_name_custom($args){ $cols = array('wp_mail_smtp', 'wp_mail_smtp_version'); return array_merge($args, $cols); } add_filter('wpstg_data_excl_rows', 'wpstg_excl_option_name_custom');
PUSHING
Change Search & Replace parameters
function wpstg_push_custom_params($args){ // Default values - Can be changed $args['search_for'] = array_merge( $args['search_for'], array('SEARCHSTRING', 'SEARCHSTRING2') ); $args['replace_with'] = array_merge( $args['replace_with'], array('REPLACESTRING','REPLACESTRING2') ); $args['replace_guids'] = 'off'; $args['dry_run'] = 'off'; $args['case_insensitive'] = false; $args['replace_guids'] = 'off'; $args['replace_mails'] = 'off'; $args['skip_transients'] = 'on'; return $args; } add_filter('wpstg_push_searchreplace_params', 'wpstg_push_custom_params');
Exclude tables from pushing
function wpstg_push_excluded_tables($tables){ $customTables = array('_options', '_posts'); return array_merge($tables, $customTables); } add_filter('wpstg_push_excluded_tables','wpstg_push_excluded_tables');
This excludes the tables wp_posts
and wp_options
from being pushed to the live site.
Exclude folders from pushing
As default WP STAGING PRO does not iterate through nor does it push any of these folders to the production website:
‘cache’,
‘wps-hide-login’,
‘node_modules’,
‘nbproject’,
‘wp-staging’
You can add custom folders to this list by using the filter below:
function wpstg_push_directories_excl($default){ $dirs = array('custom-folder', 'custom-folder2'); return array_merge($default, $dirs); } add_filter('wpstg_push_excl_folders_custom','wpstg_push_directories_excl');
Exclude files from pushing
As default WP STAGING PRO does not push any of these files to the production website:
‘.htaccess’,
‘.DS_Store’,
‘.git’,
‘.svn’,
‘.tmp’,
‘desktop.ini’,
‘.gitignore’,
‘.log’,
‘wp-staging-optimizer.php’
You can add custom files to this list by using the filter below:
function wpstg_push_excluded_files($default){ $files = array('custom-file', 'custom-file2'); return array_merge($default, $dirs); } add_filter('wpstg_push_excluded_files','wpstg_push_excluded_files');
Preserve data in wp_options and exclude it from pushing
The example below preserves the value ‘siteurl’ .
Any number of additional options may be added.
function wpstg_push_options_excl($options){ $options[] = 'siteurl'; return $options; } add_filter('wpstg_preserved_options','wpstg_push_options_excl');
Run action after pushing the production site
You can use this hook to call a method on the production site after pushing a staging site to live.
function pushingComplete() { // do something } add_action( 'wpstg_pushing_complete', 'pushingComplete' );