Skip to content

Instantly share code, notes, and snippets.

@alexpgates
Last active November 6, 2015 17:05
Show Gist options
  • Save alexpgates/53e2bfff0ce72e18c037 to your computer and use it in GitHub Desktop.
Save alexpgates/53e2bfff0ce72e18c037 to your computer and use it in GitHub Desktop.
Simple WP Page Migrations
<?php
/**
* Plugin Name: wp-page-migrate
* Description: Edit the plugin to define your pages. Activate this plugin to add the pages to your WP install.
* Version: 0.0.1
* Author: Alex P. Gates
*
* This is a simple plugin that, when activated, creates pages on a WordPress site if they don't already exist.
* This is useful for keeping local / staging / production in sync and for keeping pages in sync when working
* with lots of custom page templates with multiple people on the same project.
*
* There is no settings page for this plugin.
* To use it, just define your pages below using WP's array structure for the wp_insert_post() function.
* You are free to use any parameters that can be passed to wp_insert_post(), so you can define pages or posts,
* post authors, published dates, parent pages, etc.
* See the full list here: https://codex.wordpress.org/Function_Reference/wp_insert_post
*
* After you've defined your pages, just activate the plugin. The plugin should be deactivated immediately.
* Check log.txt for a report of which pages were created and which pages already exist.
*
* The thought behind this is very (very) loosly based on database migration workflows of frameworks such as Laravel.
* Overall, I find this approach much easier to manage than imports / exports or manually creating WP pages across all your environments.
**/
function wp_page_migration_activate(){
$pages = array();
$pages[] = array(
'post_type' => 'page',
'post_title' => 'About',
'post_name' => 'about',
'post_content' => 'About page content',
'post_status' => 'publish'
);
$pages[] = array(
'post_type' => 'page',
'post_title' => 'Contact',
'post_name' => 'contact',
'post_content' => 'Contact page content',
'post_status' => 'publish'
);
$pages[] = array(
'post_type' => 'page',
'post_title' => 'Terms of Service',
'post_name' => 'terms-of-service',
'post_content' => 'Terms of Service Page',
'post_status' => 'publish'
);
$pages[] = array(
'post_type' => 'page',
'post_title' => 'Privacy Policy',
'post_name' => 'privacy-policy',
'post_content' => 'Privacy policy content',
'post_status' => 'publish'
);
$pages[] = array(
'post_type' => 'page',
'post_title' => 'Search',
'post_name' => 'search',
'post_content' => '',
'post_status' => 'publish'
);
foreach($pages as $page){
$check = get_page_by_title($page['post_title']);
if(!isset($check->ID)){
$insert = wp_insert_post($page);
$log = date('Y-M-D', time()).' need to make '.$page['post_title'].' ---- '.$insert;
}else{
$log = date('Y-M-D', time()).' '.$page['post_title'].' exists!';
}
// Write to the log
$file = '../wp-content/plugins/wp-page-migrate/log.txt';
$current = file_get_contents($file);
$current .= $log."\n";
file_put_contents($file, $current);
}
}
register_activation_hook( __FILE__, 'wp_page_migration_activate' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment