Hello,
Currently, this script seems to only work when using the visual editor.
Would it be possible to modify this script making it usuable for the hmtl editor as well?
<?php
/**
* Plugin Name: WP Boilerplate Shortcode
*/
define('WP_BOILERPLATE_SHORTCODE_VERSION', '1.0.6');
WPBoilerplateShortcode::onload();
function the_boilerplate($key, $by = 'path', $args = array()) {
if (in_array($by, array('path', 'title', 'id')))
$args["by$by"] = $key;
echo WPBoilerplateShortcode::shortcode($args);
}
class WPBoilerplateShortcode {
static $this_term;
static function onload() {
add_action('init', array(__CLASS__, 'init'));
add_action('admin_init', array(__CLASS__, 'admin_init'));
add_action('add_meta_boxes', array(__CLASS__, 'add_meta_boxes'), 10, 2);
add_shortcode('boilerplate', array(__CLASS__, 'shortcode'));
add_action('wp_ajax_load_boilerplate_content', array(__CLASS__, 'load_boilerplate_content'));
register_activation_hook(__FILE__, array(__CLASS__, 'activation'));
register_uninstall_hook(__FILE__, array(__CLASS__, 'uninstall'));
}
static function activation() {
$post_types = get_post_types(array('publicly_queryable' => true));
self::register_post_type_and_taxonomy();
$terms = get_terms('associated-post-type', array('hide_empty' => false));
$existing_terms = array();
foreach ($terms as $term) {
$existing_terms[$term->name] = true;
}
foreach ($post_types as $post_type)
if (!isset($existing_terms[$post_type_object->label])) {
$post_type_object = get_post_type_object($post_type);
wp_insert_term($post_type_object->label, 'associated-post-type', array(
'slug' => $post_type,
'description' => "Used to Associate a Boilerplate with the [{$post_type_object->label}] post_type."
));
}
}
static function admin_init() {
wp_enqueue_script('jquery');
wp_enqueue_script('wp-boilerplate-script', plugins_url('/js/wp-boilderplate-script.js', __FILE__), array('jquery'), WP_BOILERPLATE_SHORTCODE_VERSION);
}
static function load_boilerplate_content() {
if (empty($_POST['boilerplate_id'])) {
$content = null;
} else {
$post = get_post($_POST['boilerplate_id']);
$content = htmlspecialchars(apply_filters('the_content', $post->post_content));
}
echo json_encode(array(
'result' => (!is_null($content) ? 'success' : 'No Content Available!'),
'content' => $content
));
exit;
}
static function add_meta_boxes($post, $metabox) {
global $typenow;
$post_type_object = get_post_type_object($typenow);
self::$this_term = get_term_by('name', $post_type_object->label, 'associated-post-type');
if (self::$this_term) {
add_meta_box('insert-boilerplatediv', 'Boilerplates to Insert', array(__CLASS__, 'insert_boilerplate_meta_box'), $typenow, 'side', 'high', array('taxonomy' => 'associated-post-type'));
}
}
static function insert_boilerplate_meta_box($post_id) {
global $typenow;
$query = new WP_Query(array(
'post_type' => 'boilerplate',
'taxonomy' => 'associated-post-type',
'term' => self::$this_term->slug
));
$options = array();
foreach ($query->posts as $post) {
$options[] = '<option value="' . $post->ID . '">' . "{$post->post_title}</option>";
}
$options = implode("\n", $options);
$html = <<<HTML
<div id="insert-boilerplates" class="insert-boilerplate-section">
<select id="boilerplates">
<option value="0">Select a Boilerplate: </option>
$options
</select>
<a id="insert-boilerplate" href="#insert-boilerplate" class="button">Insert</a>
</div>
<div class="clear"></div>
HTML;
echo $html;
}
static function register_post_type_and_taxonomy() {
if (function_exists('register_post_type')) {
register_post_type('boilerplate', array(
'labels' => self::make_post_type_labels('Boilerplate'),
'exclude_from_search' => true,
'publicly_queryable' => true,
'public' => true,
'show_ui' => true,
'query_var' => 'boilerplates',
'rewrite' => array(
'slug' => 'boilerplates'
),
'supports' => array('title', 'editor', 'revisions')
));
register_taxonomy('associated-post-type', 'boilerplate', array(
'hierarchical' => true,
'labels' => self::make_post_type_labels('Associated Post Type'),
'query_var' => 'associated-post-type',
'rewrite' => array(
'slug' => 'associated-post-types'
),
'public' => true,
'show_ui' => true
));
}
}
static function make_post_type_labels($singular, $plural = false, $args = array()) {
if ($plural === false)
$plural = $singular . 's';
elseif ($plural === true)
$plural = $singular;
$defaults = array(
'name' => _x($plural, 'post type general name'),
'singular_name' => _x($singular, 'post type singular name'),
'add_new' => _x('Add New', $singular),
'add_new_item' => __("Add New $singular"),
'edit_item' => __("Edit $singular"),
'new_item' => __("New $singular"),
'view_item' => __("View $singular"),
'search_items' => __("Search $plural"),
'not_found' => __("No $plural Found"),
'not_found_in_trash' => __("No $plural Found in Trash"),
'parent_item_colon' => ''
);
return wp_parse_args($args, $defaults);
}
function init() {
self::register_post_type_and_taxonomy();
}
static function shortcode($args = array()) {
$default = array(
'bypath' => '',
'bytitle' => '',
'byid' => '',
'id' => '',
'class' => 'boilerplate',
'title' => '',
'showtitle' => false,
'titletag' => 'h3'
);
$args = (object) array_merge($default, $args);
if (!empty($args->byid)) {
$page = get_page($args->byid);
} else if (!empty($args->bypath)) {
$page = get_page_by_path($args->bypath, OBJECT, 'boilerplate');
} else if (!empty($args->bytitle)) { // "bytitle" will only work if this patch is accepted: http://core.trac.wordpress.org/ticket/12743
$page = get_page_by_title($args->bytitle, OBJECT, 'boilerplate');
} else {
$page = null;
}
if (is_null($page))
$value = '[ERROR: No "bytitle", "bypath" or "byid" arguments where provided with the boilerplate shortcode' . ' or the values provided did not match an existing boilerplate entry.]';
else {
if (!empty($args->title)) {
$title = $args->title;
$showtitle = true;
} else {
$title = $page->post_title;
$showtitle = $args->showtitle;
}
$value = (!$showtitle ? '' : "\n<{$args->titletag}>$title</{$args->titletag}>\n");
$div_id = (empty($args->id) ? '<div' : '<div id="' . $args->id . '"');
$content = apply_filters('the_content', $page->post_content);
$class_html = (empty($args->class) ? '' : " class=\"{$args->class}\"");
$value = <<<HTML
$div_id{$class_html}>$value
{$content}
</div>
HTML;
}
return $value;
}
}
/**
* Plugin Name: WP Boilerplate Shortcode
*/
jQuery(document).ready(function($) {
$("#insert-boilerplate").click(function(){
var boilerplateId = $("#boilerplates").val();
if (boilerplateId==0) {
alert('Select a boilerplate from the dropdown before clicking the "Insert" button.');
} else {
var data = {
action: 'load_boilerplate_content',
boilerplate_id: boilerplateId
};
$.post(ajaxurl, data, function(response) {
response = eval('(' + response + ')');
if (response.result!='success') {
alert("Unexpect error attempting to insert boilerplate: " + response.result);
} else {
var content = $("<div/>").html(response.content).text(); // HTML decode
tinyMCE.activeEditor.setContent(tinyMCE.activeEditor.getContent()+content);
}
});
}
return false;
});
});
Christianto answers:
Hi,
If you use HTML editor, that mean you use the original textarea that has been initialized with tinyMCE function. You can add your additional content/shortcode to it with ordinary jQuery html() function targeted to id 'content'.
hope this code works on your site..
/**
* Plugin Name: WP Boilerplate Shortcode
*/
jQuery(document).ready(function($) {
$("#insert-boilerplate").click(function(){
var boilerplateId = $("#boilerplates").val();
if (boilerplateId==0) {
alert('Select a boilerplate from the dropdown before clicking the "Insert" button.');
} else {
var data = {
action: 'load_boilerplate_content',
boilerplate_id: boilerplateId
};
$.post(ajaxurl, data, function(response) {
response = eval('(' + response + ')');
if (response.result!='success') {
alert("Unexpect error attempting to insert boilerplate: " + response.result);
} else {
var content = $("<div/>").html(response.content).text(); // HTML decode
$('#content').html(function(){ return $(this).html()+content});
tinyMCE.activeEditor.setContent(tinyMCE.activeEditor.getContent()+content);
}
});
}
return false;
});
});
cor comments:
Thanks Christianto, much appreciated!