Prevent WordPress from deleting a post

At first glance it seems as though there are no hooks or filters in WordPress for preventing a post/page deletion. But you can do it by filtering user_has_cap (short for user has capability). This is a very powerful filter, and you can use it to block almost anything in WordPress. It has three parameters:

  • $allcaps (an array of all the capabilities, each one set to true or false)
  • $caps (an array of the capabilities being requested by the current operation)
  • $args (an array of arguments relevant to this operation).
  • When a post is being deleted, $args is set to array ('delete_post', $user_id, $post_id). The capabilities required to allow the deletion are stored in the array $caps, and will vary depending on what type of post is being deleted (e.g. ‘delete_published_posts’). Each capability in $caps corresponds to an item in $allcaps. To prevent the post being deleted, all we need to do is modify $allcaps by setting one of the values listed in $caps to false (e.g. $allcaps[$caps[0]] = false).

    As an example, the following code prevents the last published page of a site being deleted.

    add_filter ('user_has_cap', 'wpcs_prevent_last_page_deletion', 10, 3);
    
    function wpcs_prevent_last_page_deletion ($allcaps, $caps, $args) {
    	global $wpdb;
    	if (isset($args[0]) && isset($args[2]) && $args[0] == 'delete_post') {
    		$post = get_post ($args[2]);
    		if ($post->post_status == 'publish' && $post->post_type == 'page') {
    			$query = "SELECT COUNT(*) FROM {$wpdb->posts} WHERE post_status = 'publish' AND post_type = %s";
    			$num_posts = $wpdb->get_var ($wpdb->prepare ($query, $post->post_type));
    			if ($num_posts < 2)
    				$allcaps[$caps[0]] = false;
    		}
    	}
    	return $allcaps;
    }
    

Using the non-minified version of jQuery in WordPress

If you’re developing in WordPress, you’ll probably have define('SCRIPT_DEBUG', true); in your wp-config.php file, which ensures that WordPress does not load minified javascript files. Unfortunately, because of it’s large size a non-minified version of jQuery is not included in WordPress, which means debugging jQuery related issues can be a real pain. Thankfully, it’s easy to fix, and the code below can be easily be added to your custom developer plugin. The code simply checks which version of jQuery is currently registered, then deregisters it and re-registers Google’s unminified version.

if (defined('SCRIPT_DEBUG') && SCRIPT_DEBUG) {
	add_action ('wp_enqueue_scripts', 'wpcs_init');
	add_action ('admin_enqueue_scripts', 'wpcs_init');
}

function wpcs_init() {
	global $wp_scripts;
	if (isset($wp_scripts->registered['jquery']->ver)) {
		$jquery_version = $wp_scripts->registered['jquery']->ver;
		wp_deregister_script ('jquery');
		wp_register_script ('jquery', "http://ajax.googleapis.com/ajax/libs/jquery/{$jquery_version}/jquery.js");
	}
}

How to make sure the correct domain is used in WPML

If you use the WPML multilingual plugin for WordPress, and have each language on a different domain, you may have noticed that WPML does not ensure that all URLs in your page point to the correct domain. You might notice, for example, that images are … [Continue reading]

Adding a favicon to your site (including at the root)

Adding a favicon is an important finishing touch to building your site. If your theme doesn't support favicons, you can add a favicon by adding the following code to your functions.php : If your theme already supports favicons, chances are … [Continue reading]

Don’t edit child themes – use grandchild themes!

Child themes are the easiest way to style WordPress sites. Rather than create a site from scratch, you can create a theme that shares most of its code and styling with a parent theme. The benefits of creating a child theme instead of editing the main … [Continue reading]

Synchronize a menu with your page hierarchy

WordPress's menu feature is a fantastic way of creating a hierarchical menu, but unfortunately you still have to keep your pages in their own, separate hierarchy, using a horribly cumbersome interface, or a special plugin. The code snippet below will … [Continue reading]

Automatically add Lorem Ipsum text to blank WordPress pages

If you're creating a new Wordpress theme, it can be helpful to have some filler text in your pages. The snippet below adds a few paragraphs of lorem ipsum text to any blank post or page by filtering the_content. The text is retrieved from the … [Continue reading]

Changing the settings for Genesis Reponsive Slider to allow multiple slideshows

Out of the box, the Genesis Responsive Slider can only be used in one way on your site because it's options are set in an options panel. However, you can hook in to every setting, to create different sliders on different pages (although this code … [Continue reading]

Caching a downloaded file

It's often useful to download external files such as RSS feeds, Twitter feeds and so on. But you'll also want those files cached, so that you're not downloading them on every page view. Thankfully, WordPress provides two functions that make … [Continue reading]

How to remove deprecated widgets from the Genesis Framework

The latest version of Genesis has deprecated two widgets, but they still annoyingly show up in the widgets panel even if you're not using them. Thankfully it's easy to remove them using WordPress's unregister_widget function. Just place the following … [Continue reading]