Currently a math student, Jérémy is a passionate guy who is interested in many fields, particularly in the high tech world for which he covers the news everyday on some blogs, and web development which takes much of his free time. He loves learning new things and sharing his knowledge with others.

Jérémy's articles

  1. A Guide to Updating WordPress, Plugins and Themes

    WordPress is a great CMS and continues to grow in popularity. Like anything though, it’s not perfect. We all have different needs and WordPress out of the box cannot satisfy all of them. That’s why plugins and themes exist.

    If you use WordPress, chances are that you have installed plugins and themes to satisfy your particular needs. There are thousands of plugins and themes publicly available on WordPress.org and other third party websites but installing a plugin, a theme, or even WordPress itself, is just the first step.

    WordPress is a living project. Developers work hard on this CMS to provide us with a reliable platform and we can say the same thing for many of the plugins or themes available. To use new features or to prevent potential bugs, keeping WordPress, your themes and plugins up to date is important and that’s what we’ll cover in this article.

    Why Update?

    WordPress and its plugins and themes are like any other software installed on your computer, or like any other application on your devices. Periodically developers release updates which provide new features or fix known bugs.

    Maybe new features is not something that you necessarily want? In fact, maybe you are satisfied with the functionality you currently have and you don’t need more? Well, you are still likely to be concerned about bugs.

    Software bugs can come in many shapes and sizes. A bug could be enormous such as preventing users from using a plugin or it could be very small and only concern a certain part of a theme for example. In some cases, bugs can even be serious security holes.

  2. Including JavaScript in Plugins or Themes, the Right Way

    At the time of writing, more than 33,000 plugins and 2,600 themes are available on WordPress.org, and we can probably add more which are not present on this platform. They can do a lot of different things and, often, they use JavaScript to provide some features.

    JavaScript and WordPress

    Basically, including JavaScript code into a theme or a plugin is not difficult: what WordPress produces is nothing else than HTML so using the script tag with code directly into it or into a file linked thanks to the src attribute might be enough – if you were alone, developing in isolation.

    The fact is you are not alone. People using WordPress can install plugins or themes next to yours, and the developers of these tools can also use JavaScript. Moreover, WordPress itself is using JavaScript. The problem is, if everyone includes their scripts without attention, the resulting page will be unnecessarily heavy.

  3. Unleash the Power of the WordPress Shortcode API

    WordPress introduced the Shortcode API in its version 2.5. This API allows developers to add some more or less complex features in their plugins or themes without the user having to insert any HTML code.

    The advantage of the Shortcode API is that developers do not need to use regular expressions to detect if the user included their shortcodes into their posts: WordPress automatically detects the registered shortcodes.

    In this tutorial, we will learn how to use the Shortcode API. We will create our own shortcodes, see how to use classic and unnamed attributes and also how to handle content.

    How Does the Shortcode API Work?

    Before using the Shortcode API, it is important to know how it works for the developer and for the end user.

    First, the developer registers their shortcode with the WordPress function add_shortcode(). Then, WordPress will automatically detect when this shortcode is used and call the corresponding function which will replace the shortcode with specific text or HTML code.

    For the user, it is really simple: they insert the shortcode into their post and this shortcode will be replaced each time the post is displayed.

    Shortcodes can be more or less complex. For example, we can find simple shortcodes like [contact] inserting a contact form in place of the tag. Other shortcodes can be filled with some content, like [hello]Me[/hello], others again can use attributes, like [sayhello to="You"]. These attributes can even be unnamed: [sphere 42] is for example a shortcode using unnamed attributes, added by the WP Photo Sphere plugin. Finally, some shortcodes can combine all of that, so this API can really make some powerful stuff.

    Shortcodes Without Attributes

    Before creating more complex shortcodes, we will begin with some examples without attributes. As usual, the code presented here should be put in a file of your project (e.g. functions.php for a theme).

    Create a Shortcode

    WordPress provides us with a specific function to register a shortcode: add_shortcode(). This function requires two parameters which are the shortcode’s name and the function to call each time this shortcode is found.

    [php]add_shortcode(‘myshortcode’, ‘replace_my_shortcode’);
    [/php]

    Basically, the callback function only requires one thing: the content replacing the shortcode must be indicated in the return instruction.

    [php]function replace_my_shortcode() {
    return ‘Hello World!';
    }
    [/php]

    This example works. If you write a post and insert into it the shortcode [myshortcode], it will be replaced by the text Hello World!. So it works. But it is limited and not very useful.

    That’s why using the content of the shortcode can be a good idea. The question is: how to retrieve this content if, for example, the user wrote [myshortcode]You[/myshortcode]?

    To get this content, we don’t need to change anything in the add_shortcode() call, but we will add two parameters to our replace_my_shortcode() function.

    [php]function replace_my_shortcode($atts, $content = ”) {
    return ‘Hello ‘ . $content . ‘!';
    }
    [/php]

    In the first parameter, we can retrieve the attributes used by the author of the post, but we will see that in the next part. Here, we are more interested in the second parameter which contains the text between the two tags of our shortcode.

    If the user included some content in the shortcode, it will be in the $content variable. Otherwise, this variable will be an empty string so we can detect this case with a condition using, for example, the empty() function.

    [php]function replace_my_shortcode($atts, $content = ”) {
    if (empty($content))
    return ‘Hello World!';
    else
    return ‘Hello ‘ . $content . ‘!';
    }
    [/php]

    Shortcodes in Other Shortcodes

    We know how to create a shortcode and how to manage its content. But what if this content contains other shortcodes? For example, assume that we have two shortcodes: [name] which displays the author’s name, and [myshortcode] with the callback function above. Then, if the user writes [myshortcode][name][/myshortcode], the result will be “Hello [name]!”, which is surely not what we wanted here.

    Asking WordPress to parse shortcodes present in the content of another shortcode is possible and can be done with the do_shortcode() function, as you can see in the code below.

  4. Adding a Media Button to the WordPress Content Editor

    Inserting a media file into a post with WordPress is not very complicated thanks to the dedicated button present by default. However, if you develop a plugin using media files, this button won’t help your users insert the code needed by your functions.

    Fortunately, it is possible to add you own media button, so you can make it do whatever you want, properly.

    In this tutorial, we will review how to add a media button in the right place, how to open the media window when the user clicks on it and, finally, how to get the selected items in order to insert them simultaneously into both visual and HTML editors.

    Add a Media Button

    Adding our media button is the first and easiest part. As with customising many things in WordPress, we will use actions to put our button in the right place.

    Begin by creating a new function in the dedicated file of your plugin or theme (e.g. functions.php). For this tutorial, I will use a function named add_my_media_button(). Hook your function to the media_buttons action which is, as its name suggests it, the action called when WordPress is displaying the media buttons.