Skip to content

Instantly share code, notes, and snippets.

@rheinardkorf
Last active August 29, 2015 14:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rheinardkorf/1120521170576221cbc2 to your computer and use it in GitHub Desktop.
Save rheinardkorf/1120521170576221cbc2 to your computer and use it in GitHub Desktop.
Simple WordPress plugin for VVV to update your vvv-hosts file with your new sites, or remove sites. Running 'vagrant reload' will update local hosts file if you have the vagrant-hostsupdater plugin installed. NOTE: Don't forget to update $site_folder.
<?php
/*
Plugin Name: VVV Host Update
Plugin URI: http://rheinardkorf.com
Description: Updates the vvv-hosts file for multisite configs with subdomain enabled.
Author: Rheinard Korf
Author URI: http://rheinardkorf.com
Version: 1.0
Network: True
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License (Version 2 - GPLv2) as published by
the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
$site_folder = 'your_folder'
if ( defined( 'MULTISITE' ) && MULTISITE && defined( 'SUBDOMAIN_INSTALL' ) && SUBDOMAIN_INSTALL ){
class VVV_Host_Update {
private $path = "/srv/www/" . $site_folder . "/vvv-hosts";
function __construct() {
add_action( 'wpmu_new_blog', array( &$this, 'blog_created' ), 10, 6 );
add_action( 'delete_blog', array( &$this, 'blog_removed' ), 10, 2 );
}
function blog_created( $blog_id, $user_id, $domain, $path, $site_id, $meta ) {
$this->add_to_vvv_hosts( $domain );
}
function blog_removed( $blog_id, $drop ) {
$blog_details = get_blog_details( $blog_id );
$this->remove_from_vvv_hosts( $blog_details->domain );
}
function add_to_vvv_hosts( $site ) {
$hosts = $this->get_vvv_hosts();
$hosts = trim( $hosts );
if( ! preg_match( '/' .$site . '/', $hosts ) ) {
$hosts .= "\n{$site}\n";
}
$this->put_vvv_hosts( $hosts );
}
function remove_from_vvv_hosts( $site ) {
$hosts = $this->get_vvv_hosts();
$hosts = trim( $hosts );
if( preg_match( '/' .$site . '/', $hosts ) ) {
$hosts = str_replace( $site , '', $hosts );
}
$hosts = explode( "\n", $hosts );
$hosts = array_filter( $hosts );
$hosts = implode( "\n", $hosts );
$this->put_vvv_hosts( $hosts );
}
function get_vvv_hosts() {
return file_get_contents( $this->path );
}
function put_vvv_hosts( $hosts ) {
file_put_contents( $this->path, $hosts );
}
}
$vvv_host_update = new VVV_Host_Update();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment