Skip to content

Instantly share code, notes, and snippets.

@jayj
Created December 19, 2011 22:21
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 jayj/1499184 to your computer and use it in GitHub Desktop.
Save jayj/1499184 to your computer and use it in GitHub Desktop.
Jayj Quicktag 1.1.1
<?php
/**
* Plugin Name: Jayj Quicktag
* Plugin URI: http://jayj.dk/plugins/jayj-quicktag/
* Description: Allows you to easily add custom quicktags to the editor. Requires at least WordPress 3.3 to work
* Author: Jesper J
* Author URI: http://jayj.dk
* Version: 1.1.1
* License: GPLv2 or later
*/
register_activation_hook( __FILE__, 'jayj_quicktag_install' );
register_uninstall_hook( __FILE__, 'jayj_quicktag_uninstall' );
/**
* Set up default options on install
*
* @since 1.0.0
* @uses apply_filters() Calls 'jayj_quicktag_defaults' filter on the defaults array
*/
function jayj_quicktag_install() {
$defaults = array( 'buttons' => array( array(
'text' => 'Example',
'title' => 'Example Title',
'start' => '<example>',
'end' => '</example>'
)
) );
add_option( 'jayj_qt_settings', apply_filters( 'jayj_quicktag_defaults', $defaults ) );
}
/**
* Uninstall function
*
* Remove Quicktags from the database
*
* @since 1.0.0
*/
function jayj_quicktag_uninstall() {
if ( ! defined( 'WP_UNINSTALL_PLUGIN' ) )
exit();
delete_option( 'jayj_qt_settings' );
}
/**
* Add options page
*
* @since 1.0.0
*/
function jayj_quicktag_add_options_page() {
add_options_page( 'Jayj Quicktag', 'Jayj Quicktag', 'manage_options', __FILE__, 'jayj_quicktag_options_page' );
}
add_action( 'admin_menu', 'jayj_quicktag_add_options_page' );
/**
* Register the options page
*
* @since 1.0.0
*/
function jayj_quicktag_register_setting() {
register_setting( 'jayj_quicktag_options', 'jayj_qt_settings', 'jayj_quicktag_options_validate' );
}
add_action( 'admin_init', 'jayj_quicktag_register_setting' );
/**
* The Quicktags Options page
*
* @since 1.0.0
*/
function jayj_quicktag_options_page() { ?>
<div class="wrap">
<?php screen_icon( 'options-general' ); ?>
<h2>Jayj Quicktag Options</h2>
<br />
<form action="options.php" method="post">
<?php
/**
* Insert imported Quicktags
*
* @since 1.1.0
*/
if ( isset( $_POST['jayj-import-quicktags-save'] ) ) :
$options = get_option( 'jayj_qt_settings' );
$data = maybe_unserialize( stripslashes_deep( $_POST['jayj-import'] ) );
// Merge the old and the new Quicktags
if ( ! empty( $data ) )
$imported['buttons'] = array_merge( (array) $options['buttons'], $data['buttons'] );
// Succes or error message
if ( ! empty( $data ) && update_option( 'jayj_qt_settings', $imported ) )
echo '<div class="updated"><p><strong>Quicktags succesfully imported</strong></p></div>';
else
echo '<div class="error"><p><strong>Error: Quicktags could not be imported</strong></p></div>';
endif;
?>
<?php
settings_fields( 'jayj_quicktag_options' );
$options = get_option( 'jayj_qt_settings' );
?>
<table class="widefat jayj-quicktags-table">
<thead>
<tr>
<th scope="col">Button Label *</th>
<th scope="col">Title Attribute</th>
<th scope="col">Start Tag(s) *</th>
<th scope="col">End Tag(s)</th>
</tr>
</thead>
<tbody>
<?php
if ( isset( $options['buttons'] ) ) :
// Loop through all the buttons
for ( $i = 0; $i < count( $options['buttons'] ); $i++ ) :
if ( ! isset( $options['buttons'][$i] ) )
break;
?>
<tr valign="top">
<td><input type="text" name="jayj_qt_settings[buttons][<?php echo $i; ?>][text]" value="<?php echo esc_attr( $options['buttons'][$i]['text'] ); ?>" /></td>
<td><input type="text" name="jayj_qt_settings[buttons][<?php echo $i; ?>][title]" value="<?php echo esc_attr( $options['buttons'][$i]['title'] ); ?>" /></td>
<td><textarea class="code" name="jayj_qt_settings[buttons][<?php echo $i; ?>][start]" rows="2" cols="25"><?php echo esc_textarea( $options['buttons'][$i]['start'] ); ?></textarea></td>
<td><textarea class="code" name="jayj_qt_settings[buttons][<?php echo $i; ?>][end]" rows="2" cols="25"><?php echo esc_textarea( $options['buttons'][$i]['end'] ); ?></textarea></td>
</tr>
<?php endfor; endif; ?>
<!-- Empty -->
<?php $i = isset( $i ) ? $i : 0; ?>
<tr valign="top" class="alternative">
<td><input type="text" name="jayj_qt_settings[buttons][<?php echo $i; ?>][text]" title="Label of the Quicktag" value="" /></td>
<td><input type="text" name="jayj_qt_settings[buttons][<?php echo $i; ?>][title]" title="Title attribute of the Quicktag" value="" /></td>
<td><textarea class="code" name="jayj_qt_settings[buttons][<?php echo $i; ?>][start]" rows="2" cols="25" title="Start Tag(s)"></textarea></td>
<td><textarea class="code" name="jayj_qt_settings[buttons][<?php echo $i; ?>][end]" rows="2" cols="25" title="End Tag(s)"></textarea></td>
</tr>
</tbody>
</table>
<p>To delete a Quicktag, just leave the "Button Label" empty</p>
<?php submit_button( __( 'Save Changes' ) ); ?>
</form>
<!-- Export/Import metaboxes -->
<style type="text/css">
.jayj-quicktags-postbox { font-size: 13px; float: left; width: 48%; margin-right: 3%; }
.jayj-quicktags-postbox-last { margin-right: 0; }
.jayj-quicktags-postbox .hndle { cursor: pointer; }
.jayj-quicktags-postbox textarea, .jayj-quicktags-table input, .jayj-quicktags-table textarea { width: 95%; }
.jayj-quicktags-postbox li { list-style: disc; margin-left: 40px; }
</style>
<form action="" method="post" name="jayj-import-quicktags">
<div id="poststuff"><div class="metabox-holder">
<!-- Export function -->
<div class="postbox closed jayj-quicktags-postbox">
<div class="handlediv" title="<?php esc_attr_e( 'Click to toggle' ); ?>"><br /></div>
<h3 class="hndle"><span>Export Quicktags</span></h3>
<div class="inside">
Export your saved Quicktags data by highlighting this text and either
<ul>
<li>Copy/paste it into a blank .txt file. Then save the file for importing into another install of WordPress later.</li>
<li>Or you could just paste it into <code>Jayj Quicktag > Import Quicktags</code> on another install of WordPress.</li>
</ul>
<textarea rows="10" cols="60" onclick="this.focus(); this.select();"><?php echo esc_textarea( serialize( $options ) ); ?></textarea>
</div>
</div> <!-- .postbox -->
<!-- Import function -->
<div class="postbox closed jayj-quicktags-postbox jayj-quicktags-postbox-last">
<div class="handlediv" title="<?php esc_attr_e( 'Click to toggle' ); ?>"><br /></div>
<h3 class="hndle"><span>Import Quicktags</span></h3>
<div class="inside">
<p>To import your Quicktags, copy and paste the content from "Export Quicktags" into this textarea and press the "Import Quicktags" button below.</p>
<textarea rows="10" cols="60" name="jayj-import"></textarea>
<?php submit_button( 'Import Quicktags', 'secondary', 'jayj-import-quicktags-save' ); ?>
</div>
</div> <!-- .postbox -->
</div></div>
</form>
</div> <!-- .wrap -->
<?php
} // End function wpaq_options_page
/**
* Validate the saved options
*
* This will make sure that options with an empty "Button Label" value will be removed
*
* @since 1.0.0
* @param array $input The options saved on the options page
* @returns array The options saved, without the empty ones
*/
function jayj_quicktag_options_validate( $input ) {
// Don't save empty inputs
for ( $i = 0; $i < count( $input['buttons'] ); $i++ ) :
if ( empty( $input['buttons'][$i]['text'] ) ) {
unset( $input['buttons'][$i] );
$input['buttons'] = array_values( $input['buttons'] );
// In rare circumstances the empty inputs has to be removed again
if ( empty( $input['buttons'][$i]['text'] ) )
unset( $input['buttons'][$i] );
$input['buttons'] = array_values( $input['buttons'] );
}
endfor;
return $input;
}
/**
* Loads the JavaScript files required for managing the meta boxes on the plugin settings
* page, which allows users to toggle the metaboxes
*
* @since 1.1.0
* @todo Find a way to save the open/close state
*/
function jayj_quicktag_settings_page_enqueue_scripts( $hook_suffix ) {
if ( $hook_suffix == 'settings_page_jayj-quicktag/jayj-quicktag' )
wp_enqueue_script( 'postbox' );
}
add_action( 'admin_enqueue_scripts', 'jayj_quicktag_settings_page_enqueue_scripts' );
/**
* Loads the JavaScript required for toggling the meta boxes on the plugin settings page.
*
* @since 1.1.0
*/
function jayj_quicktag_settings_page_load_scripts() { ?>
<script type="text/javascript">
jQuery(document).ready( function($) {
$( '.if-js-closed' ).removeClass( 'if-js-closed' ).addClass( 'closed' );
postboxes.add_postbox_toggles( 'settings_page_jayj-quicktag/jayj-quicktag' );
});
</script><?php
}
add_action( "admin_head-settings_page_jayj-quicktag/jayj-quicktag", 'jayj_quicktag_settings_page_load_scripts' );
add_filter( 'jayj_qt_add_anyway', '__return_true' );
/**
* Add the quicktags to editor
*
* @since 1.0.0
*/
function jayj_quicktag_editor() {
// Check if the wp_editor() function has been called. If it hasn't, don't include the Quicktags javascript
if ( ! did_action( 'before_wp_tiny_mce' ) )
return;
// Get the options
$options = get_option( 'jayj_qt_settings' );
if ( count( $options['buttons'] ) > 0 ) : ?>
<!-- Jayj Quicktags -->
<script type="text/javascript">
//<![CDATA[
if (typeof(QTags) != 'undefined') {
<?php
$i = 0;
// Loop through each button
foreach ( $options['buttons'] as $btn ) :
$title = ( isset( $btn['title'] ) ) ? $btn['title'] : '';
?>
<?php // Self-closing tag ?>
<?php if ( empty( $btn['end'] ) ) { ?>
QTags.addButton(
'jayj_qtag_<?php echo intval( $i ); ?>',
'<?php echo esc_attr( $btn['text'] ); ?>',
'<?php echo $btn['start']; ?>',
'', '',
'<?php echo esc_attr( $title ); ?>'
);
<?php } else { ?>
QTags.addButton(
'jayj_qtag_<?php echo intval( $i ); ?>',
'<?php echo esc_attr( $btn['text'] ); ?>',
'<?php echo $btn['start']; ?>',
'<?php echo $btn['end']; ?>',
'',
'<?php echo esc_attr( $title ); ?>'
);
<?php } // endif
$i++;
endforeach;
?>
// A video button with parameters
QTags.VideoButton = function() {
QTags.TagButton.call(this, 'Video', 'video', '', '', 'm' );
};
QTags.VideoButton.prototype = new QTags.TagButton();
QTags.VideoButton.prototype.callback = function(e, c, ed) {
// The different parameters and their default value
var src = prompt( 'Enter URL for video', 'http://' ),
width = prompt( 'Enter width for video', 640),
height = prompt( 'Enter height for video', 360 ),
thumbnail = prompt( 'Enter URL for thumbnail', 'http://' );
if ( src && width && height ) {
// Start start
this.tagStart = '<video src="' + src + '" width="' + width + '" height="' + height + '">';
// Add thumbnail
if ( thumbnail )
this.tagStart = this.tagStart + '<img src="' + thumbnail + '" alt="" />';
// End tag
this.tagStart = this.tagStart + '</video>';
QTags.TagButton.prototype.callback.call(this, e, c, ed);
}
};
edButtons[150] = new QTags.VideoButton();
}
//]]>
</script>
<!-- // Jayj Quicktags --><?php
endif;
}
add_action( 'admin_print_footer_scripts', 'jayj_quicktag_editor', 100 );
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment