I use
First of all, I created a child theme of the theme I am using. This means getting all up in your website files and making a new directory, so if you’re not comfortable with that. Abort now! I’m joking, follow along. Be Brave. You’ll never learn anything new if you don’t try.
Alright, child themes. These let you edit your current theme without losing the modifications when your theme is updated. WordPress have a brilliant tutorial on how to do this here, but I’ll explain it briefly anyway.
You’re going to need a new directory, the name being
The path is generally /wp-content/themes/
That’s step one, done!
Now, we gotta create a
/*
Theme Name: Chocolate Cookie Child
Description: Chocolate Cookie Child Theme
Author: Tim Talbot
Author URL: http://timtalbot.co.uk
Template: chocolate-cookie
Version: 1.0.0
*/
The only required attributes here are
Alright, so far we have:
- A new directory called chocolate-cookie-child
- a style.css file within that directory
- a block comment at the top of style.css to define some required attributes, Theme Name and Template
All we need to do now is create a
<?php
add_action( 'wp_enqueue_scripts', 'my_theme_enqueue_styles' );
This is the beginning of our
Next, we’re going to implement that function and so this code follows the
function my_theme_enqueue_styles() {
$parent_style = 'chocolate-cookie-style';
wp_enqueue_style( $parent_style, get_template_directory_uri() . '/style.css' );
wp_enqueue_style( 'child-style',
get_stylesheet_directory_uri() . '/style.css',
array( $parent_style ),
wp_get_theme()->get('Version')
);
}
?>
Here we provide a
<link rel='stylesheet' id='chocolate-cookie-style-css' href='http://timtalbot.co.uk/wp-content/themes/chocolate-cookie/style.css?ver=5.3.2' type='text/css' media='all' />
Here within the ID attribute, we can see
We then feed this
Well, that’s our child theme created! Now we can move on to what we came here for, the shortcode implementation!
First of all, I added some custom CSS to the child style.css file, this dictates how my in-line code will be presented:
mycode {
/*border-radius: 5px;
-moz-border-radius: 5px;
-webkit-border-radius: 5px;*/
border: 1px dashed #969696;
background: #f5f5f5;
color: #BF4D28;
padding-left: 5px;
padding-right: 5px;
font: Monaco,Consolas,"Andale Mono","DejaVu Sans Mono",monospace;
white-space: nowrap;
}
Using a custom tag,
First of all, we call
Here is that function:
function add_my_code_tag() {
if(wp_script_is("quicktags"))
{
?>
<script type="text/javascript">
//this function is used to retrieve the selected text from the text editor
function getSel()
{
var txtarea = document.getElementById("content");
var start = txtarea.selectionStart;
var finish = txtarea.selectionEnd;
return txtarea.value.substring(start, finish);
}
QTags.addButton(
"code_shortcode",
"Inline Code",
callback
);
function callback()
{
var selected_text = getSel();
QTags.insertContent("<mycode>" + selected_text + "</mycode>");
}
</script>
<?php
}
}
Sidenote: There’s nothing worse than badly formatted code, and while writing this post I just noticed that the <code> tag didn’t retain code formatting, so I just added
Back to the matter at hand!
The very first thing we do is check if the
To create a new Quick Tags button, we call
Finally, the
I suspect we can add some elegance to this, though, so if that no text is selected, it’ll just insert the opening or closing tag. So let’s modify the
- Insert an opening <mycode> tag
- Insert a closing </mycode> tag
- Wrap selected text in <mycode> tags
I may be over-complicating things here, but here’s where I’m at:
function add_my_code_tag() {
if(wp_script_is("quicktags"))
{
?>
<script type="text/javascript">
var close = false;
//this function is used to retrieve the selected text from the text editor
function getSel()
{
var txtarea = document.getElementById("content");
var start = txtarea.selectionStart;
var finish = txtarea.selectionEnd;
return txtarea.value.substring(start, finish);
}
QTags.addButton(
"code_shortcode",
"Inline Code",
callback
);
function callback()
{
var selected_text = getSel();
if(selected_text == '') {
if(!close) {
QTags.insertContent("<mycode>");
close = true;
} else {
close = false;
QTags.insertContent("</mycode>");
}
} else {
QTags.insertContent("<mycode>" + selected_text + "</mycode>");
}
}
</script>
<?php
}
}
and now, all of that over-complicated, unnecessary, extra-work nonsense is out of the way – We can reduce the function to the following code for exactly the same functionality:
function add_my_code_tag() {
if(wp_script_is("quicktags"))
{
?>
<script type="text/javascript">
QTags.addButton(
"code_shortcode",
"Inline Code",
"<mycode>",
"</mycode>"
);
</script>
<?php
}
}
Because that very same functionality is provided to us by WordPress’s Quicktips API. And there you have it, that’s how to add a quicktags, shortcode button to your WordPress HTML editor, to make life simpler!
Sidenote: just another side note, in case you’re wondering how I type all of these < and > opening and closing tags without them disappearing, I’m using HTML Special Entities. Without spaces: