Skip to content

Instantly share code, notes, and snippets.

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 tnchuntic/39e4ebc1c1f201ba0be6affad30008f0 to your computer and use it in GitHub Desktop.
Save tnchuntic/39e4ebc1c1f201ba0be6affad30008f0 to your computer and use it in GitHub Desktop.
Widget Block
<?php
/*
* Creating a function to create our CPT
*/
function cpt_widget_block() {
// Set UI labels for Custom Post Type
$labels = array(
'name' => _x('Widget Blocks', 'Header Banners', 'tnc'),
'singular_name' => _x('Widget Block', 'Header Banner', 'tnc'),
'menu_name' => __('Widget Blocks', 'tnc'),
'parent_item_colon' => __('Parent Widget Block', 'tnc'),
'all_items' => __('All Widget Blocks', 'tnc'),
'view_item' => __('View Widget Block', 'tnc'),
'add_new_item' => __('Add New Widget Block', 'tnc'),
'add_new' => __('Add New', 'tnc'),
'edit_item' => __('Edit Widget Block', 'tnc'),
'update_item' => __('Update Widget Block', 'tnc'),
'search_items' => __('Search Widget Block', 'tnc'),
'not_found' => __('Not Found', 'tnc'),
'not_found_in_trash' => __('Not found in Trash', 'tnc'),
);
// Set other options for Custom Post Type
$args = array(
'label' => __('cpt-widget-block', 'tnc'),
'description' => __('Page Widget Blocks', 'tnc'),
'labels' => $labels,
// Features this CPT supports in Post Editor
'supports' => array('title', 'editor', 'thumbnail', 'revisions',),
// You can associate this CPT with a taxonomy or custom taxonomy.
// 'taxonomies' => array('genres'),
/* A hierarchical CPT is like Pages and can have
* Parent and child items. A non-hierarchical CPT
* is like Posts.
*/
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_nav_menus' => true,
'show_in_admin_bar' => true,
// 'menu_position' => 5,
'can_export' => true,
'has_archive' => true,
'exclude_from_search' => false,
'publicly_queryable' => true,
'capability_type' => 'page',
);
// Registering your Custom Post Type
register_post_type('cpt-widget-block', $args);
}
/* Hook into the 'init' action so that the function
* Containing our post type registration is not
* unnecessarily executed.
*/
add_action('init', 'cpt_widget_block', 0);
class CPT_Widget_Block extends WP_Widget {
function __construct() {
parent::__construct(
'cpt_widget_block', // Base ID
'Widget Blocks', // Name
array('description' => __('Displays Widget Block'))
);
}
public function widget($args, $instance) {
// HELPER::print_array($args);
$content = $instance_block = $instance_title = '';
if(isset($instance['tnc-widget-title'])){
$instance_title = $instance['tnc-widget-title'];
}
if(isset($instance['tnc-widget-block'])){
$instance_block = $instance['tnc-widget-block'];
}
$tempPage = get_post($instance_block)->post_content;
$shortcodes_custom_css = get_post_meta($instance['tnc-widget-block'], '_wpb_shortcodes_custom_css', true);
if (!empty($shortcodes_custom_css)) {
$content .= '<style type="text/css" data-type="vc_shortcodes-custom-css">';
$content .= $shortcodes_custom_css;
$content .= '</style>';
}
$content .= ($instance_title)?'<h3 class="widget-title">'.$instance_title.'</h3>':'';
$content .= wpb_js_remove_wpautop($tempPage, true); // fix unclosed/unwanted paragraph tags in $content
echo $args['before_widget'];
echo $content;
echo $args['after_widget'];
}
public function form($instance) {
$instance_block = $instance_title = '';
$posts = get_posts(array('post_type' => 'cpt-widget-block', 'post_status' => 'publish'));
if(isset($instance['tnc-widget-title'])){
$instance_title = $instance['tnc-widget-title'];
}
if(isset($instance['tnc-widget-block'])){
$instance_block = $instance['tnc-widget-block'];
}
$form = '<p><label for="' . $this->get_field_name('tnc-widget-title') . '">Title:</label><input type="text" class="widefat" id="'.$this->get_field_id('tnc-widget-title').'" name="'.$this->get_field_name('tnc-widget-title').'" value="'.$instance_title.'" /></p>';
$form .= '<p><label for="' . $this->get_field_name('tnc-widget-block') . '">Widget Block:</label>';
$form .='<select class="widefat" id="' . $this->get_field_id('tnc-widget-block') . '" name="' . $this->get_field_name('tnc-widget-block') . '">';
foreach ($posts as $post) {
$select = ($post->ID == $instance_block) ? ' selected' : '';
$form .='<option value="' . $post->ID . '"' . $select . '>' . $post->post_title . '</option>';
}
$form .='</select>';
$form .='</p>';
$form .='<p>';
$form .='</p>';
echo $form;
}
function update($new_instance, $old_instance) {
$instance = $old_instance;
$instance['tnc-widget-title'] = strip_tags($new_instance['tnc-widget-title']);
$instance['tnc-widget-block'] = strip_tags($new_instance['tnc-widget-block']);
return $instance;
}
}
register_widget('CPT_Widget_Block');
function mf_widget_block_shortcode_func($atts) {
$a = shortcode_atts(array(
'id' => '',
'display' => '',
), $atts);
$post_content = get_post($a['id'])->post_content;
$content = '';
if($a['display']!='clean'){
$content .= '<style type="text/css" data-type="vc_shortcodes-custom-css">';
$content .= get_post_meta($a['id'], '_wpb_shortcodes_custom_css', true);
$content .= '</style>';
}
$content .= wpb_js_remove_wpautop($post_content, true); // fix unclosed/unwanted paragraph tags in $content
return $content;
}
add_shortcode('tnc-widget-block', 'mf_widget_block_shortcode_func');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment