Skip to content

Instantly share code, notes, and snippets.

@JayWood
Last active September 16, 2016 05:19
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 JayWood/d54efae845806894492c7febd280fe7f to your computer and use it in GitHub Desktop.
Save JayWood/d54efae845806894492c7febd280fe7f to your computer and use it in GitHub Desktop.
A class to convert the Zerif Light 'Our Focus' widgets to a CPT
<?php
/**
* Plugin Name: Widget to CPT
* Plugin URI: http://webdevstudios.com
* Description: A simple how-to for migrating widgets to a CPT
* Author: JayWood
* Author URI: http://webdevstudios.com
* Version: 0.1.0
*/
class JW_Widget_to_CPT {
/**
* Instance of JW_Widget_to_CPT
* @var JW_Widget_to_CPT
*/
public static $instance = null;
public static function init() {
if ( null == self::$instance ) {
self::$instance = new self();
}
return self::$instance;
}
public function hooks() {
// All hooks here.
add_action( 'init', array( $this, 'migrate' ) );
add_action( 'init', array( $this, 'register_post_type' ) );
}
public function register_post_type() {
$labels = array(
'name' => _x( 'Focus', 'post type general name', 'your-plugin-textdomain' ),
'singular_name' => _x( 'Focus', 'post type singular name', 'your-plugin-textdomain' ),
'menu_name' => _x( 'Focus', 'admin menu', 'your-plugin-textdomain' ),
'name_admin_bar' => _x( 'Focus', 'add new on admin bar', 'your-plugin-textdomain' ),
'add_new' => _x( 'Add New', 'book', 'your-plugin-textdomain' ),
'add_new_item' => __( 'Add New Focus', 'your-plugin-textdomain' ),
'new_item' => __( 'New Focus', 'your-plugin-textdomain' ),
'edit_item' => __( 'Edit Focus', 'your-plugin-textdomain' ),
'view_item' => __( 'View Focus', 'your-plugin-textdomain' ),
'all_items' => __( 'All Focus', 'your-plugin-textdomain' ),
'search_items' => __( 'Search Focus', 'your-plugin-textdomain' ),
'parent_item_colon' => __( 'Parent Focus:', 'your-plugin-textdomain' ),
'not_found' => __( 'No focus found.', 'your-plugin-textdomain' ),
'not_found_in_trash' => __( 'No focus found in Trash.', 'your-plugin-textdomain' )
);
$args = array(
'labels' => $labels,
'description' => __( 'Description.', 'your-plugin-textdomain' ),
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'focus' ),
'capability_type' => 'post',
'has_archive' => true,
'hierarchical' => true,
'menu_position' => null,
'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments', 'page-attributes' )
);
register_post_type( 'focus', $args );
}
public function migrate() {
if ( get_option( 'zl_has_imported', false ) ) {
return;
}
$widgets = wp_get_sidebars_widgets();
$widget_set = $widgets['sidebar-ourfocus'];
$widget_options = get_option( 'widget_ctup-ads-widget' );
foreach ( $widget_set as $index => $widget ) {
// Grab the ID of the widget
$widget_array = explode( '-', $widget );
$widget_key = end( $widget_array );
$widget_data = $widget_options[ $widget_key ];
$insert_args = array(
'post_type' => 'focus',
'post_status' => 'publish',
'menu_order' => $index,
'post_title' => $widget_data['title'],
'post_content' => $widget_data['text'],
'meta_input' => array(
'link' => $widget_data['link'],
'image_uri' => $widget_data['image_uri']
),
);
wp_insert_post( $insert_args );
}
update_option( 'zl_has_imported', true );
}
}
function jw_widget_to_cpt() {
return JW_Widget_to_CPT::init();
}
add_action( 'plugins_loaded', array( jw_widget_to_cpt(), 'hooks' ) );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment