Exciting News! Flipper Code is now WePlugins! Same commitment to excellence, brand new identity.

How to use pre_untrash_post filter in WordPress

Sandeep Kumar Mishra
Sandeep Kumar Mishra
January 26, 2023
5 minutes read

Hey there! As a fellow WordPress enthusiast, let me walk you through the pre_untrash_post filter in a friendly and simple way.

Basically, this filter lets you decide whether a post should be untrashed or not. It’s like having a gatekeeper for the untrashing process. To use the pre_untrash_post filter, you first need to register it using add_filter. You can do this in your theme’s functions.php file or, even better, in a custom plugin. This way, your customizations remain intact even after a theme update.

In the examples below, I’ll show you how we can use this filter in real-world scenarios. Let’s dive in!

Example 1: Basic Usage

Here’s a simple example of how to use the pre_untrash_post filter. This function will be called every time a post is untrashed.

	function weplugins_modify_pre_untrash_post_defaults($untrash, $post, $previous_status) {
		// Example: log untrash events
		error_log("Post {$post->ID} is being untrashed from status: $previous_status");
		return $untrash;
	}
	add_filter("pre_untrash_post", "weplugins_modify_pre_untrash_post_defaults", 10, 3);
	

Example 2: Conditional Untrashing

Let’s say you only want to untrash posts if they were previously published. You can do that like this:

	function weplugins_conditional_untrash($untrash, $post, $previous_status) {
		if ($previous_status === 'publish') {
			return true; // Allow untrashing
		}
		return false; // Disallow untrashing
	}
	add_filter("pre_untrash_post", "weplugins_conditional_untrash", 10, 3);
	

Example 3: Removing the Filter

Sometimes, you might need to remove the filter after adding it. Here’s how you can do that:

	remove_filter("pre_untrash_post", "weplugins_modify_pre_untrash_post_defaults", 10, 3);
	

Remember to use the same callback function name, priority, and number of arguments while removing the hook callback.

Contact Us

If you need any help or customization regarding this hook, feel free to Contact Us. We’re here to help!

Sandeep Kumar Mishra

Sandeep Kumar Mishra

Sandeep Kumar Mishra writes about WordPress and Artificial Intelligence, offering tips and guides to help you master your website and stay updated with the latest tech trends.

Explore the latest in WordPress

Trying to stay on top of it all? Get the best tools, resources and inspiration sent to your inbox every Wednesday.