6

I need to add a relatively small bit of functionality to a Wordpress plugin and I have the option to either edit the plugin directly, or scrap the plugin completely and repurpose PHP code from another site to do the same thing. Repurposing non-Wordpress PHP code for such a small change seems silly and I have the few lines of PHP I would use when editing the plugin. My question is, is it a good idea to edit a plugin?

I know that when editing a theme, if the edits are done directly to the old code, when the theme is updated the changes will be lost. Does this happen with plugins too? Is there a way around this?

For those wondering how complicated the changes are, I'll explain briefly. The client I have wants a form submission to go to a dealer and the user. But on certain circumstances depending on the user's email address (if it ends in ".gov" or a few other endings... yes I know this isn't the best idea and I tried to convince the client but they were adamant) the client wants the email to go to someone else. Naturally this could be cured with a few small PHP lines in the plugin but the plugin doesn't have the functionality for "conditional recipients" based on content sent by the user. So I'd like to either add that functionality just for this site (if it can be done quickly) or I'd like to replace the plugin with straight php which seems like it would cause more problems than it would fix initially. Time is of the essence. Thank you so much for your help!

2
  • 1
    Have you contacted the plugin author? Maybe they have a github you can send a pull request to? I for one have always been stoked when someone contributes to any open source project I've done. That said, especially if this is for a paying client and the functionality is relatively light (as described), you should probably just roll your own. Commented Jul 29, 2014 at 17:22
  • If the plugin includes filter hooks you can use them in your child theme to change the default output. Commented Jul 30, 2014 at 3:27

3 Answers 3

5

The most important question which you must ask yourself is Am I the author of the theme or plugin?. If your answer is yes, and you are responsible for the up keep of the plugin or theme, go ahead and change it and modify as you like.

If your answer is no, then you should not make any changes to it, not even a small thing as a simple dot .. The same goes for WordPress core files. The simple reason being, as you've stated, if the theme/plugin/core is updated, you will lose whatever you have done to the theme/plugin/core.

I would recommend that you create your own small functionality plugin with this extra functionality. It is quick and easy, and will save you many tears later as it will not be overwritten unless you do it yourself.

0
5

In general you don't want to modify plugins, because you will loose your modifications next time you update them. It's a little bit different for themes, where you can set up a child theme.

If the plugin is using the wp_mail() function to send out the emails, you might try to use the wp_mail filter to change the recipients.

Try to write your own plugin to overcome the problem.

Here's an example of a such a plugin (untested):

/* Plugin Name: Change recipients for .gov emails */

add_filter( 'wp_mail', 
    function ( $args ) {

        // Check if the recipent's email ends with .gov         
        if( 'vog.' === strtolower( substr( strrev( $args['to'] ), 0, 4 ) ) )
        {
            // Replace it with a new email address:
            $args['to'] = '[email protected]';
        }

        return $args;
    }
);

where you might need some additional hooks provided by the plugin.

2
  • Thank you very much, birgire! This is exactly what I needed. I gave the check to the other post for answering first since you both said pretty much the same thing.
    – Rambo8000
    Commented Jul 29, 2014 at 17:30
  • 2
    Glad to hear it helps. I've already upvoted the other answer as well and it deserves the check ;-)
    – birgire
    Commented Jul 29, 2014 at 17:33
2

I think those of us who have been working with Wordpress for a few years have been guilty of doing this at one stage or another. I have had to make modifications to a plugin that was no longer being supported by the original author and had some compatibility issues with later versions of Wordpress.

There are situations where editing an existing plugin you're not the original author of is accepted:

  • The plugin is no longer being maintained or supported
  • The plugin has some glaring security issues that could compromise your server/site and you don't want to wait for an update and need the plugin

These are really the only two situations I can think of where editing a plugin is acceptable. In an ideal world, all Wordpress plugin authors would use the hooks/filters functionality in Wordpress so we can easily extend them and change things without editing plugin files, but as we all know, there are a lot of poorly written plugins out there without support for hooks or filters.

If you need to add some more functionality to a plugin my first piece of advice would be to build your own plugin and write the functionality yourself. However, there are instances where you might want to remove a Javascript library and include your own in the plugin, but the author decide to inline the includes and not use Wordpress functions. This is where situations like this can get tricky, because you will have to edit a file or two.

In a situation where you need to edit plugin files (and this is a last case scenario after exploring all other options), you should keep a modifications file in the plugin directory, I have a file called "CHANGES.txt" and simply list the files changed, the line number and if I have time, I'll list what I did. You can then diff the files if you need to update and see what changed.

The last resort method isn't ideal, it has its flaws and goes against the laws of programming quite heavily. But, as I mentioned, sometimes editing plugins files is needed if the plugin is poorly written or doesn't easily allow overriding/extending in your theme or custom plugin.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.