Actions and Filters

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.

For using the hooks and filters, we recommend installing our separate plugin, named  WP STAGING Hooks, or using the hooks and filters on this page in a mu-plugin.
Otherwise, not all actions might work as expected.

WP STAGING Hooks Plugin

You can download the WP STAGING Hooks plugin that contains most of the filters mentioned in this article for easy usage on your website:
https://github.com/WP-Staging/wp-staging-hooks

Increase WP STAGING Request Time and PHP’s Maximum Execution Time

Usually, the WP STAGING backup and restore works even on minor web hosts. If you have a huge database or website with millions of rows, the PHP default execution time limit of 30 seconds may be too low. To make the backup creation/restore work, set the PHP value  max_execution_time= 120 in your php.ini.

Also, increase the apache value Timeout 120 or set the Nginx directive fastcgi_read_timeout 120;

After doing that, add the filter below into a mu-plugin like mu-plugin/wp-staging-optimizer.php.

​add_filter('wpstg.resources.executionTimeLimit', function(){
return 100;
});

Ensure that the input value is lower than the actual PHP maximum allowed execution time. This is necessary so that WP STAGING request time never exceeds the PHP execution time.

Change Staging Site Title

function wpstg_get_title(){
   return 'DEV';
}
add_filter('wpstg_staging_site_title', 'wpstg_get_title');

Change Path to Cache Folder

To change the folder that is used to store temporary backup files to the global PHP temp folder or to somewhere else, you can use the code below:

​add_filter('wpstg.directory.cacheDirectory', function(){
        return trailingslashit(sys_get_temp_dir()) . 'wpstagingcache';​
​})

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 fails 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 Rows From Cloning

This example will exclude rows from the wp_options table depending on the option name or exclude posts from the wp_posts table based on the post_title and post_status and their post metadata.

These operators are available:

['=', '>', '>=', '<', '<=', '<>', '!=', 'LIKE', 'NOT LIKE']

function queryCloningRows($filters) {
	$filters = [
// Clone only options where option_name is not 'wpstg_is_staging_site'
		'wp_options' => [
			'option_name' => [
				'operator' => '<>',
				'value' => 'wpstg_is_staging_site'
			]
		],
// Clone only posts where post_title LIKE 'Hello%' AND post_status = 'publish'
		'wp_posts' => [
			'post_title' => [
				'operator' => 'LIKE',
				'value' => 'Hello%'
			],
			'post_status' => 'publish'
		],
// will filter postmeta depending upon filtered data in wp_posts, see above wp_posts filter
                'wp_postmeta' => [
			'join' => [
				'table' => 'wp_posts',
				'primaryKey' => 'ID',
				'foreignKey' => 'post_id',
			]
		]
	];
	
	return $filters;
}

add_filter('wpstg.cloning.database.queryRows', 'queryCloningRows');

Exclude Custom Post Type From Cloning

This example will exclude posts under the custom post type “coupon” along with its postmeta data as well:

These operators are available:

['=', '>', '>=', '<', '<=', '<>', '!=', 'LIKE', 'NOT LIKE']

function queryCloningRows($filters) {
	$myFilters = [
	// Clone only posts which do not belong to post_type coupon
		'wp_posts' => [
			'post_type' => [
				'operator' => '<>',
				'value' => 'coupon'
			]
		],
                // will filter postmeta depending upon filtered data in wp_posts, see above wp_posts filter
                'wp_postmeta' => [
			'join' => [
				'table' => 'wp_posts',
				'primaryKey' => 'ID',
				'foreignKey' => 'post_id',
			]
		]
	];

	return array_merge($filters, $myFilters);
}

add_filter('wpstg.cloning.database.queryRows', 'queryCloningRows');

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 Files

/**
* Cloning: Exclude files from cloning
* Example: You can use a wildcard pattern like *.log to exclude all log files
*/
function wpstg_clone_excluded_files($default)
{
    $files = array('custom-file', '*LOG-*', '*.logs');
    return array_merge($default, $files);
}

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 Rows From Pushing. Copy Only Specific Rows

This example will exclude certain rows from wp_options table depending on the option name, or exclude posts from wp_posts table based on the post_title and post_status along with their postmeta data as well.

These operators are available:

['=', '>', '>=', '<', '<=', '<>', '!=', 'LIKE', 'NOT LIKE']

function queryPushingRows($filters) {
	$filters = [
		// will only push options where option_name is not 'wpstg_is_staging_site'
		'wp_options' => [
			'option_name' => [
				'operator' => '<>',
				'value' => 'wpstg_is_staging_site'
			]
		],
		// will only push posts where post_title LIKE 'Hello%' AND post_status = 'publish'
		'wp_posts' => [
			'post_title' => [
				'operator' => 'LIKE',
				'value' => 'Hello%'
			],
			'post_status' => 'publish'
		],
                // will filter postmeta depending upon filtered data in wp_posts, see above wp_posts filter
                'wp_postmeta' => [
			'join' => [
				'table' => 'wp_posts',
				'primaryKey' => 'ID',
				'foreignKey' => 'post_id',
			]
		]
	];
	
	return $filters;
}

add_filter('wpstg.pushing.database.queryRows', 'queryPushingRows');

Exclude Certain Post Type From Pushing

This example will exclude posts with the custom post type “coupon” along with its postmeta data as well:

These operators are available:

['=', '>', '>=', '<', '<=', '<>', '!=', 'LIKE', 'NOT LIKE']

function queryPushingRows($filters) {
	$myFilters = [
		// Push only posts where post_type is not 'coupon'
		'wp_posts' => [
			'post_type' => [
				'operator' => '<>',
				'value' => 'coupon'
			]
		],
                // will push postmeta depending upon filtered data in wp_posts, see above wp_posts filter
                'wp_postmeta' => [
			'join' => [
				'table' => 'wp_posts',
				'primaryKey' => 'ID',
				'foreignKey' => 'post_id',
			]
		]
	];

	return array_merge($filters, $myFilters);
}

add_filter('wpstg.pushing.database.queryRows', 'queryPushingRows');

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' );

Backup

Exclude file extension from backup

Allow users to exclude specific file extensions from being backup exported.

function wpstg_backup_files_ignore_extensions($default_excluded){
return array_merge($default_excluded, ['zip', 'gz']);
}
add_filter( 'wpstg.export.files.ignore.file_extension', 'wpstg_export_files_ignore_extensions');

Exclude files larger than the given size from backup

Allow users to exclude files bigger than a given size from being backup exported.

function wpstg_backup_files_ignore_files_bigger_than($default){
return 10 * MB_IN_BYTES;
}

add_filter( 'wpstg.export.files.ignore.file_bigger_than', 'wpstg_backup_files_ignore_files_bigger_than');

Exclude files with particular extensions larger than the given file size from backup

Allow users to exclude files with extensions larger than the given size from being exported.

function wpstg_backup_files_ignore_files_w_extension_bigger_than($default){
$ignoreFiles = [
    'gz' => 10 * MB_IN_BYTES, 
    'tar' => 20 * MB_IN_BYTES
];
return array_merge($default, $ignoreFiles);
}

add_filter( 'wpstg.export.files.ignore.file_extension_bigger_than', 'wpstg_backup_files_ignore_files_w_extension_bigger_than');

 

Exclude directories from being included in a backup

function wpstg_backup_exclude_directories($excludedDirectories){
        $customExcludedDirectories = [
            'var/www/example.com/htdocs/wp-content/cache'
        ];
        return array_merge($excludedDirectories, $customExcludedDirectories);
    }

add_filter( 'wpstg.backup.exclude.directories', 'wpstg_backup_exclude_directories');

 

Exclude files from wp-content folder from being restored

Other files not contained in plugins, uploads or themes can be excluded from the backup restore by using this filter.
Note: Existing files in the wp-content folder will still be deleted before running the restore process.

 

    /**
     * Backup: Exclude files from being restored
     */
    function wpstg_backup_restore_exclude_other_files($excludedFiles){
        $customExcludedFiles = [
            'wp-content/db.php',
            'wp-content/object-cache.php',
        ];
        return array_merge($excludedFiles, $customExcludedFiles);
    }
add_filter( 'wpstg.backup.restore.exclude.other.files', 'wpstg_backup_restore_exclude_files');

Restore Backup and Keep Existing Media Files, Plugins, or Themes

You can use the filters below to keep existing files:

// Keep all plugins
add_filter('wpstg.backup.restore.keepExistingPlugins', function(){return true;});

// Keep all mu-plugins
add_filter('wpstg.backup.restore.keepExistingMuPlugins', function(){return true;});

// Keep all themes
add_filter('wpstg.backup.restore.keepExistingThemes', function(){return true;});

// Keep all media files in wp-content/uploads folder.
add_filter('wpstg.backup.restore.keepExistingMedia', function(){return true;});

// Keep all other files in wp-content folder.
add_filter('wpstg.backup.restore.keepExistingOtherFiles', function(){return true;});

Updated on May 4, 2022