WordPress - Overriding a function in a plugin - wordpress

I've been wonder for some time what the best practice is for modifying a plugin created by a WordPress user?
For example there are a couple lines of code I want to change within the Contact Form 7 plugin. The function is called function wpcf7_ajax_json_echo() and is located in:
wp-content > plugins > contact-form-7 > includes > controller.php
Of course I could just change the code right in that file and be done, but then I'm out of luck when I want to update that plugin as my modifications will probably get written over.
I know I should be making this happen via my functions.php file, but I'm not sure how to go about achieving this. Also, that particular function is 100+ lines of code and I'm guessing I don't want to overwrite that whole function, because there is a good chance the author of the plugin may choose to update something in that function in the future.
Does anyone know the cleanest way for me to modify a few lines within that function via my functions.php file?
Thank you!

I do not recommend changing the core. However, you are in a bit of a pickle.
You can:
Update the function code directly in the plugin
Copy the current function code from the plugin and override it in functions.php
In the end, you still run into the same problem - future compatibility.
Either:
The update will overwrite your plugin changes.
Your function overwrites the plugin changes.
So, as much as I don't want to say it, I'd update the plugin directly. At least then when you upgrade, you'll know pretty quick that your change is missing. Furthermore, it's possible the plugin updates will include your change.

You could use SVN if you wanted to maintain forwards compatibility (and your host has SVN available), whilst being able to keep your own changes.
Each plugin that's on the Plugin Directory has to have an SVN repo (that's how the Directory knows if there are updates). Here's the CF7 repo.
Checkout the trunk to your /plugins/ directory inside a folder like /custom-contact-form-7/. Alter the wp-contact-form-7.php file to give it a unique name, and make the changes you want to make to customise it.
To get new updates you can just svn up to get them, and they'll merge with your changes. Though, you may have to clean up merge conflicts sometimes.
Version Control with Subversion is the place everyone starts to learn SVN, if you need it. There's also a Github repo now, if you'd like to fork that.

I definitely think you should add your updates to functions.php or to a custom plugin. It's a hassle right now, but MUCH less hassle every time you upgrade the plugin.
You'll always have to reference the changes made in updates no matter what. Even if you're able to extend the functionality without copying this file, you'll have to at least check and make sure your changes still work. And WinDiff/BBEdit's compare will make quick work of that.
So my first suggestion is to override that function.
Second suggestion:
I noticed there's some extensions (a, b, c) to this plugin; perhaps you can find out how they made their extensions and use those details to make your own. Well, that's like suggesting you make a new house in order to fix the dripping faucet, but it's an idea.

Related

Where to create gutenberg block

This Gutenberg update looks sweet, but it's immediately frustrating trying to follow any of the documentation.
I started with the very simple looking tutorial that is the first result on Google. It has code that looks to make sense for what I'm trying to do here. Great.
Except it says nothing about where to put this code to make it work.
Other tutorials are all about "download our plugin and we'll create one for you" - but I don't want to add that huge nest of code when what I'm really looking for is a fairly simple static block of code I can have users drop into a page.
Back in the day we'd just register a short code in the functions.php file and call it a day.
Can someone explain to me:
Where to put this registration code?
Where to put this JS code it references?
If I'm somehow going about this with a huge misunderstanding of how Gutenberg works?
Thanks
To actually solve this properly would need a couple of chapters of a book... but here's the very condensed answer.
Taking a very quick look at the link you shared, the code should be put in a plugin as there are calls to the function plugins_url(). So you need to first create a plugin. It's not difficult to build a simple plugin but it does take a bit of time getting used to how things need to be registered and how everything is glued together (that's why I said that a proper answer would take a couple of chapters of a book :D).
Once you have the plugin, the code should be within the plugin (or in any PHP file that is included by the plugin) and the JS file should go inside the plugin directory in a folder called step-01
There's a git repo from WordPress with some Gutenberg examples. I haven't looked to hard at them, but It might be a good idea to follow the code in there with the tutorial of how to build a plugin (and the WordPress code reference site opened too :D). If you are going to be developing things for WP, I recommend you to get Zeal or Dash to be able to browse the documentation in an easy and fast way.
Edit
I just found that you can create the scaffolding with wp cli. Wordpress has a tutorial on how to use the wp scaffold block command here.

Can I edit a WordPress plugin and preserve my changes when the plugin updates?

I'm mainly interested in adding classes to a few of the containers in one particular plugin - so I can style those elements. I've saved the .php files, and kept notes of the changes, but cannot find a solution (if any) to maybe create a child plugin that will preserve my edits.
Thanks!
Not really. That's why plugin authors are encouraged to include hooks for other plugins to customize their behavior.
You could create a diff file with your changes and use it to try re-applying your changes after an update, but you might run into difficulties if the plugin code changes substantially.

remove plugin on specific template page

I am using http://wordpress.org/extend/plugins/pdf24-post-to-pdf/ pdf generator plugin for my site, but I only want it present on certain pages.
How can I remove it from all pages except a certain template I allow?
I believe this can be done with filters, but I have googled and googled and can't seem to get anywhere.
I don't think that's possible without modifying the plugin source code. That would be a nice security issue, IMO.
Plugins are files simply included on the loading process. At least, there's no hook I could find on WordPress source code for you to interfere in this behavior. Take a look just above the plugins_loaded hook, and the wp_get_active_and_valid_plugins function.
What you can try to do, instead, is just modifying the plugin with the condition you need. In the top of the pdf24.php file, just below the standard comments, put:
if (!condition_met())
return;

How to modify js in contributed module in Drupal

I want to modify a javascript file bundled in a drupal contributed module a little bit, for example changing a specific #id to css .class etc. As far as i know it's not good to modify source code of that module for upgrade reason. So, can you pls teach me how to do that the best way?
If you can accomplish what you want in a separate .css file, that would be best.
If it's an improvement that would enhance the module, simply make the modification and submit the patch back to the community. The drupal site has full details on how to create a patch.
If the patch is very specific, see if there's a way to make it generic so that you can submit it.
If all else fails, and you have to make the change, COMMENT. It's also good to create a patch file with the change and save it outside of the contrib module directory. The whole reason to NOT make changes is that updates become a real pain. If you document every change you make, and feel confident that you can re-apply them, then you should be OK.

Wordpress Theme Developer "Workspace"

I am a Wordpress Theme Developer (Freelancer) and I make somewhere between 1-2 Themes / Week so I need a Quick way to make this happen.
Examples:
I use twentyten every time, delete the functions that I don't need, the CSS, images etc. I kinda let it "naked". (Not a quick way, maybe a personal framework?)
I search for Wordpress functions (since I can't remember all of them), on Wordpress Codex and again, not a quick way. So, do you guys think that a personal Function DB would be ok for me?
And more other stuff that I can't remember right now.
How do you guys organize your workspace?
If the themes you make are fairly similar, you might want to create that "naked" version and keep that as your base for future work.
I've created a handful of themes myself and I followed the same approach. I take that base theme and create new versions of the particular files that need to be customized.
However, if your themes vary quite a bit in form and function (being a freelancer, I'm certain they do), you may still want to create a library of functions and stylesheets rather than re-implementing everything for each project. Even if it means creating functions that call single Wordpress functions with default values, if it streamlines your workflow then it is well worth the minor overhead.
I think it's really a matter of personal preference, what works for you, what's easy to remember, what makes sense to you, etc. If you like starting with a stripped TwentyTen theme, then take that theme as a whole, strip it of everything you don't want, and then use that as your starting point for each theme you build. Certain functions that you find yourself using over and over, you could simply place in the functions.php file, in the theme folder. Then, which ever functions you end up needing for that specific theme are there, and the rest of them you can delete once you've built the theme.
First, it would be useful to develop your own theme as a starting point. Ripping stuff out of twentyten is really inefficient. If you develop one that has everything you need, you can do the second step:
Use child themes. A child theme will keep the parent untouched and you only have to change the things that you need to be different and add a new stylesheet. A big benefit is that it only consists of the stuff that is specific to that particular site. The generic stuff stays in the parent theme. Also you have the benefit of updating the parent easily and adding upgrades across multiple sites quickly.
A good starting point might be to try one of the frameworks like Thematic.
Also take a look at Automattic's toolbox theme. Twentyten is kind of messy and bulky. Toolbox is actually meant to be used more like you are using Twentyten.

Resources