Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Migrating widgets to a custom post type.
<?php
/**
* Plugin Name: Widget to CPT
* Plugin URI: http://plugish.com
* Description: A simple how-to for migrating widgets to a CPT
* Author: JayWood
* Author URI: http://plugish.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 );
/**
* Now we grab the number ( key ) from the widget
* So ctup-ads-widget-1 for example will give us just the number 1
*/
$widget_key = end( $widget_array );
/**
* The above grabbed key is associated with the array keys in the
* widget options, so we use that one here.
*/
$widget_data = $widget_options[ $widget_key ];
/**
* Now that we have all our widget data
* we build up the insertion arguments
* for wp_insert_post()
*/
$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