This is my first Wordpress plugin. I would love some input from the WP/PHP/JavaScript gurus out there. It's a simple plugin that saves a post using ctrl-Q and opens a preview in a new or current window.
<?php
add_action('admin_menu', 'plugin_admin_add_page');
add_action('admin_init', 'plugin_admin_init');
add_action('admin_init', 'pass_option_value');
wp_register_script('quick_preview',plugins_url('/quick-preview/quick-preview.js'),array('jquery'));
wp_enqueue_script('quick_preview');
function pass_option_value(){ //Gets the preview option and passes it to the quick_preview script
$options = get_option('quick_preview_options');
wp_localize_script( 'quick_preview', 'quickPreviewOption', $options['window_preference'] );
}
function plugin_admin_add_page() {
add_options_page('Quick Preview', 'Quick Preview', 'manage_options', 'quick_preview', 'qp_options_page');
}
function plugin_admin_init(){
register_setting( 'quick_preview_options', 'quick_preview_options', 'qp_options_validate');
add_settings_section('plugin_main', 'Main Settings', 'plugin_section_text', 'quick_preview');
add_settings_field('plugin_window_preference', 'Window Preference', 'plugin_setting_string', 'quick_preview', 'plugin_main');
}
function plugin_section_text() {
echo '<p>Choose whether you would like the post preview to open in a new window or the current window.</p>';
}
function plugin_setting_string() {
$options = get_option('quick_preview_options');?>
<p><input id='plugin_window_preference' name='quick_preview_options[window_preference]' size='40' type='radio' value='current' <? php if($options['window_preference'] === "current"){echo ("checked='checked'");}?> /> Current Window </p>
<input id='plugin_window_preference' name='quick_preview_options[window_preference]' size='40' type='radio' value='new' <?php if($options['window_preference'] === "new"){echo ("checked='checked'");}?> /> New Window<p class="description">If popups are disabled you will need to add your site to the exception list in your broswer. </p> <?php }
function qp_options_validate($input) {
$newinput = array('window_preference' => trim($input['window_preference']));
if( $newinput['window_preference'] != "new" && $newinput['window_preference'] != "current" ) {
$newinput[ 'window_preference' ] = "";
}
return $newinput;
}
function qp_options_page() { ?>
<div class="wrap">
<div id="icon-options-general" class="icon32">
<br />
</div>
<h2>Quick Preview Options</h2>
<form action="options.php" method="post">
<?php settings_fields('quick_preview_options'); ?>
<?php do_settings_sections('quick_preview'); ?>
<p class="submit">
<input name="Submit" type="submit" class="button-primary" value="<?php esc_attr_e('Save Changes'); ?>" />
</p>
</form>
</div>
<?php } ?>
JavaScript:
jQuery(document).ready(function(){
if (document.cookie.indexOf("previewCookie") >= 0){
//expires added for IE
document.cookie="previewCookie=true; max-age=0;expires=0;path=/wp-admin/";
//quickPreviewOption is set in quick-preview.php
var previewURL = document.getElementById('post-preview');
if(quickPreviewOption === 'current'){
window.location = previewURL;
}
if(quickPreviewOption === 'new'){
window.open(previewURL,"wp_PostPreview","","true");
}
}
jQuery(document).keydown(function(e){
if((e.ctrlKey || e.metaKey) && e.which == 81){
//Find #save post if it's a draft. If post is published, #save-post doesn't exist.
if(jQuery('#save-post').length){
jQuery('#save-post').click();
}
else if(jQuery('publish').length){
jQuery('#publish').click();
}
//Sets a cookie to open the preview on page refresh. Saving a post auotmatically refreshes the page.
document.cookie = "previewCookie = true;max-age = 60;path=/wp-admin/";
}
});
});