This website uses cookies to allow us to see how the site is used. If you continue to use this site, we assume that you are okay with this. If you want to use the sites without cookies, please see our privacy policy.

Automatically deactivate plugin when parent plugin is deactivated

There’s a plugin I maintain called Amazon S3 and CloudFront. It checks if the Amazon Web Services plugin is activated and if not, deactivates itself. Here’s the code:

function as3cf_check_required_plugin() {
    if ( class_exists( 'Amazon_Web_Services' ) || !is_admin() || ( defined( 'DOING_AJAX' ) && DOING_AJAX ) ) {

    require_once ABSPATH . '/wp-admin/includes/plugin.php';
    deactivate_plugins( __FILE__ );

    $msg = sprintf( __( 'Amazon S3 and CloudFront has been deactivated as it requires the <a href="%s">Amazon&nbsp;Web&nbsp;Services</a> plugin.', 'as3cf' ), '' ) . '<br /><br />';

    if ( file_exists( WP_PLUGIN_DIR . '/amazon-web-services/amazon-web-services.php' ) ) {
        $activate_url = wp_nonce_url( 'plugins.php?action=activate&amp;plugin=amazon-web-services/amazon-web-services.php', 'activate-plugin_amazon-web-services/amazon-web-services.php' );
        $msg .= sprintf( __( 'It appears to already be installed. <a href="%s">Click here to activate it.</a>', 'as3cf' ), $activate_url );
    else {
        $install_url = wp_nonce_url( self_admin_url( 'update.php?action=install-plugin&plugin=amazon-web-services' ), 'install-plugin_amazon-web-services' );
        $msg .= sprintf( __( '<a href="%s">Click here to install it automatically.</a> Then activate it. ', 'as3cf' ), $install_url );

    $msg .= '<br /><br />' . __( 'Once it has been activated, you can activate Amazon&nbsp;S3&nbsp;and&nbsp;CloudFront.', 'as3cf' );

    wp_die( $msg );

add_action( 'plugins_loaded', 'as3cf_check_required_plugin' );

Neatly Wrapping Custom Post Type Queries in a Custom Class that Extends WP_Query

Let’s say you have a custom post type book that you almost always want to order by title. In other words, when querying for books, the default should be to order them by title. Also, you would almost never use paging. Instead of repeating these query arguments over and over, you can wrap them in a class that extends WP_Query.

class My_Book_Query extends WP_Query {

	function __construct( $args = array() ) {

		$args = wp_parse_args( $args, array(
			'post_type' => 'book',
			'orderby' => 'title',
			'order' => 'ASC',
			// Turn off paging
			'posts_per_page' => -1,
			// Since, we won't be paging,
			// no need to count rows
			'no_found_rows' => true
		) );

		parent::__construct( $args );


Then when querying for books, simply call this new class instead of WP_Query. And feel free to use any query arguments you normally would.

$r = new My_Book_Query( array(
	'order' => 'DESC'
) );

while ( $r->have_posts() ) {
	echo '<br />';


You could also add methods to the class and hook them up to WP_Query filters like posts_where, posts_join, etc. All nicely wrapped up in a class.

Update: I’ve expanded on this and posted a longer, more involved example on my blog. Check it out ».

Disable the WordPress Public Search

Sometimes you just might not need the search functionality for your website but, just by not enabling it doesn’t mean the content is not searchable, If for any reason you need to disable the WordPress public search use this function.

function my_disable_public_search( $q ) {
	if ( $q->is_admin ) {
		return $q;

	if ( $q->is_search ) {
		unset( $_GET['s'] );
		unset( $_POST['s'] );
		unset( $_REQUEST['s'] );
		$q->set( 's', '' );
		$q->is_search = false;

add_action( 'parse_query', 'my_disable_public_search', 5 );