Chapter 4

Modifying Existing Plugin Code

IN THIS CHAPTER

Check Avoiding problems with plugin modifications

Check Removing plugin features

Check Modifying plugin output

Check 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.

Setting the Foundation for Modifying Plugins

Before you start modifying plugins, you should do the following:

  • Set up a development site. That way, if you accidentally break your site, no harm is done.
  • 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.

    Tip For more details about WP_DEBUG, see the WordPress Codex page on the topic: https://codex.wordpress.org/WP_DEBUG.

  • Set up a non-WordPress editor. You don't want to use the editors built into WordPress. Although they’re convenient, they can introduce bugs into the PHP code that can cause your entire site to break, including your ability to edit the PHP file. Thus, you should use an editor on your own computer or on the server to modify the files so that problems can be fixed quickly without large amounts of work.

Remember All code is covered by copyright. For a plugin to be added to the WordPress repository, the code needs to use the GPLv2 license or later. Any code in the plugins on the WordPress website is available for you to use, modify, and even redistribute, the only limitation being that your modified code must use the same licensing as the original code. Book 1, Chapter 2 covers licensing and the GPL.

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:

  • Change the name of the plugin’s directory. The directory name is used as the basis for determining what to sync the plugin to for update purposes. If you change the directory name to something different, the plugin is no longer a candidate for automatic updates. Make sure that you pick a name that doesn’t exist in the Plugins page. You can verify that you have a unique name by trying to go to 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.
  • Modify the name of the plugin. Although this change isn't strictly necessary, it does help ensure that the plugin stands out. Even adding (modified) to the name helps ensure that the modified version is kept separate from the normal version of the plugin. It also serves as a reminder to you that the plugin is modified and shouldn’t be treated as a normal plugin. In other words, it’s a reminder to take care when deciding to update or delete the plugin.
  • Add your name to the listing of plugin authors. This change tells people where to send questions about the plugin. This is especially important if the modified plugin is redistributed or the plugin is used in an environment that has many users.

Remember Although I don’t typically recommend editing the core files of plugins, I’m doing so in this chapter to demonstrate how plugins are built, to introduce you to the files and code, and to get you comfortable with plugin files in general.

Removing Part of a Form

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.

Screenshot of the All in One SEO Pack plugin that provides a large number of settings that control the SEO features for each post and page.

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:

  1. 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.

  2. Remove line 324.

    This line (the section of the code that includes help text for the Preview Snippet) looks like this:

    'snippet' => '#preview-snippet',

  3. Remove lines 944 through 960.

    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(/<(?:.| )*?>/gm, ""));
    });
    jQuery("#aiosp_description_wrapper").bind("input", function() {
    jQuery("#aioseop_snippet_description").text(jQuery("#aiosp_description_
    wrapper textarea").val().replace(/<(?:.| )*?>/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.

  4. Save the modification, and upload the change to the server.

    The editor page looks like Figure 4-2.

Screenshot of the All in One SEO Pack post- and page-specific settings after the Preview Snippet is removed.

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.

Modifying the Hello Dolly Lyrics

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.";

Remember When changing the lyrics, ensure that all the lyrics text is replaced; otherwise, you can accidentally introduce an error into the code.

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.

Screenshot of the Dashboard page displaying the modified Hello Dolly plugin with recent comments from subscribers.

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.

Tip WordPress has a built-in mechanism for replacing strings in code with new strings. This feature, called localization, is typically used to change text to another language. As long as the plugin properly uses the localization functions for all its output strings, you can use this feature to change specific strings as desired, even if you simply change the words used rather than the language. This feature is discussed in Book 7, Chapter 6.

Changing a Shortcode’s Name

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.

Tip It’s possible to register additional names for a specific shortcode without modifying the shortcode’s plugin code. You can accomplish this registration by creating a custom plugin with an add_shortcode function call that connects the new shortcode name with the callback function name (the second argument of the function). Creating new plugins is discussed in Book 7, Chapter 5.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
13.58.252.8