Chapter 4
IN THIS CHAPTER
Avoiding problems with plugin modifications
Removing plugin features
Modifying plugin output
Changing shortcode names
WordPress has more than 55,000 plugins on its website, so you’d think that you could find a plugin to do everything you could possibly need on your WordPress website. But not even the best plugins can meet the needs of every user.
This chapter explores the idea of tweaking an existing plugin to meet your specific needs. With a little bit of programming knowledge and some determination, modifying existing plugin code is very possible. Although you won’t become a full-fledged developer overnight, making changes in existing plugins can definitely get the mental gears spinning on how you can do more and more with your programming knowledge.
The examples in this chapter are simple, offering a basic introduction to modifying plugins. The next chapter goes into much more depth with regard to plugin development.
Before you start modifying plugins, you should do the following:
Display error messages. By default, most WordPress sites hide error messages. You want to see those messages when developing plugins, however, because they can provide valuable feedback.
Find the wp-config.php
file (it's in the main WordPress installation on your web server), and change WP_DEBUG define
(scroll to the bottom of the file) from false
to true
to display error-message information.
For more details about
WP_DEBUG
, see the WordPress Codex page on the topic: https://codex.wordpress.org/WP_DEBUG
.
It’s important to know that when you start making changes, you’re on your own. You can no longer update the plugin to gain access to bug fixes or new features. To update to a newer version of the plugin, you have to download the latest version of the code and modify it again to make your desired changes.
Because WordPress supports updating plugins automatically, you want to ensure that you don’t accidentally update a modified plugin and lose your modifications. To prevent such a situation from happening, do the following:
https://wordpress.org/plugins/new-directory-name
, where new-directory-name
is replaced by the name you want to use for your directory. Note that after you change the name of the directory, the plugin needs to be activated again.One of the easiest modifications to make is removing something from an existing plugin.
The All in One SEO Pack plugin, available at https://wordpress.org/plugins/all-in-one-seo-pack
, provides a large number of settings that control the SEO features for each post and page. Suppose that you manage this site, and several editors and authors frequently ask questions about the Preview Snippet (see Figure 4-1). Some of them complain that they don’t like the snippet displayed where it is because it takes up valuable space in the Post Edit screen. You want to edit the plugin to remove the snippet.
FIGURE 4-1: All in One SEO Pack post- and page-specific settings before the Preview Snippet is modified.
Follow these steps to remove the Preview Snippet:
Search the plugin’s files for the words Preview Snippet.
Search for this phrase because it’s a unique string inside the plugin’s code. After digging around in the files, you find the section responsible for this form in the plugin’s aioseop_class.php
file inside the /all-in-one-seo-pack/
folder.
Remove line 324.
This line (the section of the code that includes help text for the Preview Snippet) looks like this:
'snippet' => '#preview-snippet',
In the section of code that adds the snippet preview setting, you can find the lines of code that looks like this:'snippet' => array(
'name' => __( 'Preview Snippet', 'all-in-one-seo-pack' ),
'type' => 'custom',
'label' => 'top',
'default' => '<script>
jQuery(document).ready(function() {
jQuery("#aiosp_title_wrapper").bind("input", function() {
jQuery("#aiosp_snippet_title").text(jQuery("#aiosp_title_wrapper input").val().replace(/<(?:.|\n)*?>/gm, ""));
});
jQuery("#aiosp_description_wrapper").bind("input", function() {
jQuery("#aioseop_snippet_description").text(jQuery("#aiosp_description_
wrapper textarea").val().replace(/<(?:.|\n)*?>/gm, ""));
});
});
</script>
<div class="preview_snippet">
<div id="aioseop_snippet"><h3><a>%s</a></h3><div><div>
<cite id="aioseop_snippet_link">%s</cite></div>
<span id="aioseop_snippet_description">%s</span></div></div></div>',
),
To make the modification properly, you must remove all of those lines.
Save the modification, and upload the change to the server.
The editor page looks like Figure 4-2.
FIGURE 4-2: The All in One SEO Pack post- and page-specific settings after the Preview Snippet is removed.
After making this change, load your website in your browser to make sure that it loads with no error messages and that nothing is broken due to this modification. Save a variety of settings for the SEO feature, and ensure that the modifications still take effect after the settings are saved.
Matt Mullenweg, co-founder of WordPress, developed the Hello Dolly plugin. Anyone who follows the development of WordPress knows that Mullenweg is a huge jazz fan. How do we know this? Every single release of WordPress is named after some jazz great. One of the most recent releases of the software, for example, is named Tipton, after jazz great Billy Tipton Parker; another release was named Coltrane, after the late American jazz saxophonist and composer John Coltrane.
Knowing this, it isn't surprising that Mullenweg developed a plugin named Hello Dolly. Here’s the description of this plugin that you see on the Plugins page of your Dashboard:
This is not just a plugin, it symbolizes the hope and enthusiasm of an entire generation summed up in two words sung most famously by Louis Armstrong: “Hello, Dolly.” When activated, you will randomly see a lyric from “Hello, Dolly” in the upper right of your admin screen on every page.
Is it necessary? No. Is it fun? Sure!
The Hello Dolly plugin is included with WordPress and is easy to modify. If you don’t have this plugin installed on your site, you can find it on the WordPress website at https://wordpress.org/plugins/hello-dolly
.
In the hello.php
file of the plugin is a variable named $lyrics
that stores the lyric lines of the “Hello, Dolly” song. By replacing this text with your own text, you can change the random selection of a “Hello, Dolly” lyric line to anything you desire.
You can replace the $lyrics
variable with new text such as this, for example:
$lyrics = "I love WordPress For Dummies.
There's a plugin for that.";
After the modification is in place, the Hello Dolly plugin says either I love WordPress For Dummies
or There's a plugin for that
. Figure 4-3 shows the result of this modification. Notice the message toward the top-right corner of the screen.
FIGURE 4-3: The modified Hello Dolly plugin declares that it loves WordPress For Dummies.
When a string (a portion of code that’s contained inside quotes) is used only for output, it’s typically safe to modify the text without causing any bugs or other issues in the plugin. By changing a string’s text, you can change the output of the plugin without much effort.
Sometimes, shortcodes have hard-to-remember names. This problem typically occurs because the plugin author is trying to avoid creating conflicts with other plugins by using the same shortcode name. Although this practice helps prevent code conflicts, looking up a shortcode’s name each time you want to use it can be annoying.
The Posts in Page plugin (available at https://wordpress.org/plugins/posts-in-page
) shows how shortcodes can be hard to remember. It comes with two shortcodes: ic_add_post
and ic_add_posts
. From a developer standpoint, these names make sense because the plugin author is IvyCat. Thus, the initials ic
are used to prefix each shortcode name, ensuring that the shortcode names are unique. From a user standpoint, however, the naming scheme just causes frustration.
A nice feature of shortcodes is the fact that the code that handles the shortcode can be connected to multiple names, so you can add extra names for a shortcode rather than change an old name to a new name. Adding a second name is helpful because it prevents any existing uses of the old shortcode name from breaking after the change.
Searching the plugin's files for add_shortcode
— the function that creates new shortcodes — shows that the shortcodes are created in the posts_in_page.php
file. The two lines of code on lines 37 and 38 that create the current shortcodes are as follows:
add_shortcode( 'ic_add_posts', array( &$this, 'posts_in_page' ) );
add_shortcode( 'ic_add_post', array( &$this, 'post_in_page' ) );
The new name for ic_add_posts
will be show-posts
. The new name for ic_add_post
will be show-post
. To add these shortcodes, copy and paste the original two add_shortcode
function calls and then modify each shortcode name to the new name. After you make the changes, the section of code looks like this:
add_shortcode( 'ic_add_posts', array( &$this, 'posts_in_page' ) );
add_shortcode( 'show-posts', array( &$this, 'posts_in_page' ) );
add_shortcode( 'ic_add_post', array( &$this, 'post_in_page' ) );
add_shortcode( 'show-post', array( &$this, 'post_in_page' ) );
The functionality is exactly the same as that of the ic_add_posts
shortcode; the shortcode simply has a name that's easier to remember.