Skip to content

Instantly share code, notes, and snippets.

@gbyat
Last active August 29, 2015 14:04
Show Gist options
  • Save gbyat/02366c64b1ae73b1a80e to your computer and use it in GitHub Desktop.
Save gbyat/02366c64b1ae73b1a80e to your computer and use it in GitHub Desktop.
Typographic Quotes in WordPress
/***********************************
* use in your theme functions.php
* or wordpress plugin
* correct quotes in multilingual sites
* http://codex.wordpress.org/Quicktags_API
***********************************/
function pptf_add_german_quotes() {
if (wp_script_is('quicktags')) {
?>
<script type="text/javascript">
QTags.addButton( 'german_quotes', '„cite“', '„', '“', '', 'German Quotes', '' );
QTags.addButton( 'french_quotes', '«&nbsp;cite&nbsp;»','« ', ' »', '', 'French Quotes', '' );
QTags.addButton( 'italian_quotes', '«cite»','«', '»', '', 'Italian Quotes', '' );
QTags.addButton( 'dechevron_quotes', '»cite«','»', '«', '', 'German Chevrons', '' );
</script>
<?php }
}
add_action('admin_print_footer_scripts', 'pptf_add_german_quotes' );
/***********************************
* visual editor quotes buttons
***********************************/
function pptf_add_mce_buttons() {
// check user permissions
if ( !current_user_can( 'edit_posts' ) && !current_user_can( 'edit_pages' ) ) {
return;
}
// check if WYSIWYG is enabled
if ( 'true' == get_user_option( 'rich_editing' ) ) {
add_filter( 'mce_external_plugins', 'pptf_add_tinymce_plugins' );
add_filter( 'mce_buttons', 'pptf_register_mce_button' );
}
}
add_action('admin_head', 'pptf_add_mce_buttons');
// Declare script for new button
function pptf_add_tinymce_plugins( $plugin_array ) {
$plugin_array['de_quotes_button'] = get_template_directory_uri() .'/js/ve_custom_buttons.js';
$plugin_array['fr_quotes_button'] = get_template_directory_uri() .'/js/ve_custom_buttons.js';
$plugin_array['it_quotes_button'] = get_template_directory_uri() .'/js/ve_custom_buttons.js';
$plugin_array['de_chevron_button'] = get_template_directory_uri() .'/js/ve_custom_buttons.js';
return $plugin_array;
}
// Register new button in the editor
function pptf_register_mce_button( $buttons ) {
array_push( $buttons, 'de_quotes_button' );
array_push( $buttons, 'fr_quotes_button' );
array_push( $buttons, 'it_quotes_button' );
array_push( $buttons, 'de_chevron_button' );
return $buttons;
}
(function() {
tinymce.create('tinymce.plugins.de_quotes_button', {
init : function(ed, url) {
ed.addButton('de_quotes_button', {
text: '„cite“',
onclick : function() {
ed.selection.setContent('„' + ed.selection.getContent() + '“');
}
});
},
createControl : function(n, cm) {
return null;
},
});
tinymce.PluginManager.add('de_quotes_button', tinymce.plugins.de_quotes_button);
tinymce.create('tinymce.plugins.fr_quotes_button', {
init : function(ed, url) {
ed.addButton('fr_quotes_button', {
text: '« cite »',
onclick : function() {
ed.selection.setContent('«&nbsp;' + ed.selection.getContent() + '&nbsp;»');
}
});
},
createControl : function(n, cm) {
return null;
},
});
tinymce.PluginManager.add('fr_quotes_button', tinymce.plugins.fr_quotes_button);
tinymce.create('tinymce.plugins.it_quotes_button', {
init : function(ed, url) {
ed.addButton('it_quotes_button', {
text: '«cite»',
onclick : function() {
ed.selection.setContent('«' + ed.selection.getContent() + '»');
}
});
},
createControl : function(n, cm) {
return null;
},
});
tinymce.PluginManager.add('it_quotes_button', tinymce.plugins.it_quotes_button);
tinymce.create('tinymce.plugins.de_chevron_button', {
init : function(ed, url) {
ed.addButton('de_chevron_button', {
text: '»cite«',
onclick : function() {
ed.selection.setContent('»' + ed.selection.getContent() + '«');
}
});
},
createControl : function(n, cm) {
return null;
},
});
tinymce.PluginManager.add('de_chevron_button', tinymce.plugins.de_chevron_button);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment