Skip to content

Instantly share code, notes, and snippets.

@sohan5005
Last active August 23, 2017 21:30
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save sohan5005/258bc7c8cc9e942efa8af0be38323f21 to your computer and use it in GitHub Desktop.
Save sohan5005/258bc7c8cc9e942efa8af0be38323f21 to your computer and use it in GitHub Desktop.
A class that can can be used to have some default sections to King Composer for WordPress
<?php
if( class_exists( 'KC_Section_Importer' ) ) {
return;
}
class KC_Section_Importer {
/**
* Define the post_type & taxonomy to use
*/
public $taxonomy = 'kc-section-category';
public $type = 'kc-section';
/**
* Instance factory
*/
public static $_instance;
public static function getInstance() {
if( !( self::$_instance instanceof self ) ) {
self::$_instance = new self;
}
return self::$_instance;
}
/**
* constructor
*/
public function __construct() {
}
/**
* the importer
*/
public function import() {
if( !post_type_exists('kc-section') ) {
return;
}
$template = require_once plugin_dir_path( __FILE__ ) . 'template.php';
foreach( $template as $cat_name => $sections ) {
$cat_slug = $this->makeslug( $cat_name );
$term = get_term_by( 'slug', $cat_slug, $this->taxonomy );
if( $term === false ) {
$term = wp_insert_term( $cat_name, $this->taxonomy, array(
'slug' => $cat_slug,
) );
$term_id = $term['term_id'];
} else {
$term_id = (int)$term->term_id;
}
foreach( $sections as $section ) {
$section_name = $section['title'];
$post = get_page_by_title( $section_name, 'OBJECT', $this->type );
if( is_null( $post ) ) {
$post = array(
'post_title' => $section_name,
'post_content' => $section['content'],
'post_status' => 'publish',
'post_type' => $this->type,
'tax_input' => array(
$this->taxonomy => array( $term_id ),
),
'meta_input' => array(
'kc_data' => array(
'mode' => 'kc',
'classes' => '',
'max_width' => '',
'thumbnail' => esc_url( $section['thumbnail'] ),
'css' => '',
),
),
);
$id = wp_insert_post( $post );
}
}
}
}
/**
* Converts a text to slug
*/
public static function makeslug( $text ) {
$text = preg_replace( '~[^\pL\d]+~u', '-', $text );
if( function_exists( 'iconv' ) ) {
$text = iconv( 'utf-8', 'us-ascii//TRANSLIT', $text );
}
$text = preg_replace( '~[^-\w]+~', '', $text );
$text = trim( $text, '-' );
$text = preg_replace( '~-+~', '-', $text );
$text = strtolower( $text );
return $text;
}
}
<?php
/**
* The function is hooked on admin_init
*
* That means these sections cannot be removed. If someone deletes any of these sections, they will be added automatically again
*
* You can modify them to run at activation only
*/
add_action( 'admin_init', 'myplugin_add_sections' );
function myplugin_add_sections() {
require_once plugin_dir_path( __FILE__ ) . 'class-kc-section-importer.php';
$importer = KC_Section_Importer::init();
$importer->import();
return;
}
<?php
/**
* This file should return array of sections
*/
return array(
// array of category
'My Category' => array(
array(
'title' => __( 'Hero section', 'textdomain' ), // Title
'thumbnail' => plugin_dir_url( __FILE__ ) . 'thumbs/hero.jpg', // Thumbnail file
'content' => '[kc_row _id="923527" use_container="no"][my_hero_section][/kc_column][/kc_row]', // the content of section
),
// .. more section
),
// .. more categories
);
@tuongpgjz
Copy link

Yeah! Awesome Sohan,

Welcome you to KingComposer page builder world

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment