How to override searchform.php using a plugin - wordpress

I am trying to build a search form that looks a bit different from the default WP search box. I can edit the searchform.php for that, but I want it to be in form of a plugin, so that I can easily enable it and disable it at will. But the problem is that if WP finds searchform.php, it will use the form in that file, so no tricks like add_filter, add_action will work here. So, what I want to ask from folks here is: does there wxist some way by which I can achieve the above. i.e override the code of searchform.php
Also on a different note, if I name the search box to anything other than "s", then the code goes to index.php, instead of search.php. This, I have verified by putting debug echo's and other wierd statements.
What could be the possible reason for this?
Thanks in advance for your help.

Okay, I found a hack. Not very good though, but works for me. So what I did is, in the activation filter of my plugin, I wrote the code to rename the original searchform.php (the one residing in the current theme folder) to searchform_orig.php This way WP does not find searchform.php and so renders the searchform which I have hooked to the filter. Similarly, on deactivation part, I rename the file back to searchform.php. May not be ideal , but is working on y systems that I have tested. Though I would be interested in knowing loopholes/caveats in this approach. Marking it as an answer ;-)

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.

Drupal know from where things are showed

I have a strange question but I don't find any hint about that (if it's possible), for a drupal 7 website I have to modify some content of a page in the backoffice, but I really don't know from where some content of this page is created (a table, similar to a view table but not a view table).
I just want to know if there is any way to show which php function the page use to finally be showed. I know there is something like that for the theme (drupal theme debug) but I don't find something for my case.
Any idea ?
You need PHP profiler to check all functions called on page, there's a module for Drupal7 for XHProf integration. But I would suggest you to use your browsers inspector as mentioned by 2pha before. For example if there's a form on the page just use the form ID to find it. Custom classes are very useful in these cases, parts of the html codes etc. In your case search for table headers...
The code you are looking for is most probably in custom modules and the
general suggestion is to keep you custom modules in separated folder from contributed ones.

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;

WordPress - Overriding a function in a plugin

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.

Drupal Hooks (hook_form_alter)

I’ve a question about Hooks. Being kind of new to Drupal I haven’t had much experience with hooks but a friend of mine suggested to get familiar with it and learn it to solve one of my problems on my site.
The thing is, that I have a module fbconnect which I use for users to connect via Facebook and use their profile picture at Facebook, on my site.
Everything is working, I’ve two checkboxes which and the connection and profile images works quite well but the problem is that above the two checkboxes I want a descriptive text to appear.
The only way I so far have to put in this text is to create a “description” field to the first checkbox. Unfortunately, it chooses to display this text beneath the checkbox so now it look kind of strange with a checkbox, a 3-4 lines descriptive text to the entire Facebook function, and then another checkbox.
If hooks are the right way to go to solve this problem, how do I actually do it and where do I actually insert the hook? I can imagine that it is the hook_form_alter function I need to have and in my fbconnect module the function fbconnect_form_alter exists but where I go from here I really have no idea.
I’ve tried to read up and see some instruction videos about hooks but I’m still puzzled about this apparently very nice feature in Drupal.
I'm using Drupal 6 for this site.
Any help or advice would be very much appreciated.
It sounds like you might also need to look at the Theme system in Drupal. In particular, take a look at theme_checkbox. From glancing at the code, there seems to be a label that is rendered after the actual checkbox. In your custom theme function or theme file you can try changing the order of the two.
In drupal, a "hook" is the way to interact with some piece of code.
In you have a hook_bar() hook, and is your module named 'foo' implements foo_bar(), then this function is executed.
In your case, you'll need to create a module, and impletements hook_form_alter()
You can find a tutorial there that show you how to add a checkbox. If you need to add some text, you can use the same method, except instead of adding a checkbox, of course, you just add a textfield
Here is an awesome beginner's video about adding custom hooks with the example of hook_form_alter from Drupalcon Chicago 2011 which is perfect for this situation and will hopefully help you in this. As a newbie to Drupal it surely helped me and I would highly recommend watching other videos. Thanks to the drupal community for posting these.
http://chicago2011.drupal.org/sessions/introduction-module-development

Resources