How to use hooks in child theme to override a plugin? - php

I want to add some HTML code to a plugin but I want to do that in a child theme, the function that I wanna override it is in a file named as admin-functions.php
I want to add extra buttons to booked_render_custom_fields function
admin-functions.php:
function booked_render_custom_fields($calendar = false) { ?>
<button class="button">Text</button>;
<?php }
any suggestion? thanks

you can't really override a function like that. you can only use the filters and hooks that the plugin implements. but you got some options:
a) make a copy of the plugin, change it's name and make the changes you need. after that, it wont get updates anymore. you'll have to implement them manually.
b) you could add just one line to the original plugin and define a filter or hook to which you subscribe in your functions.php . so you only have to fix this one line after updating the plugin.
c) since the newer versions of wordpress, you have the possibilites to run your own code after any shortcode. so you could add or inject your own html into the output of the shortcode. https://developer.wordpress.org/reference/hooks/do_shortcode_tag/

Related

override a function wrapped with if (!function_exists)

I'm using "Advanced Scripts plugin" to modify a function of other plugin, the fuction I'm trying to modify is wrappd with if( !function_exists('some_function') ).
the function inside the plugins is like this
if( !function_exists('send-invoice') ){
function send-invoice(){
//The Plugin Invoice
}
}
This is what I did
function send-invoice(){
//My Custom Invoice
}
add_action('init', 'send-invoice');
How can I make sure that my code runs before the plugin codes?
The plugin load before the theme, I tried plugin-loaded hook but nothing changed
You can to use the anonymous function for example:
add_action('init', function() {
//code here
});
More detail is here
Or use another hook muplugins_loaded:
function send-invoice(){
//My Custom Invoice
}
add_action('muplugins_loaded', 'send-invoice');
If nothing else work for you you can create a custom plugin, name it something like "aaamyplugin" and just insert there a single .php file with the function you are trying to override. This is the easiest (not cleanest) way to make sure your code overrides the plugin functions.
The reason for this is because Wordpress plugin loading order is simply alphabetical, that means that everything named before the plugin you are trying to override, get loaded first.
The cleanest way would be to look into the source code of the plugin to understand how it does what it does. Like: when does it load that file that contains the function you are trying to override? That's the important question to answer if you want to go with clean way

What is the cleanest way to override a Wordpress plugins' hook instantiated with $this

I am trying to modify a Wordpress plugins' method without changing the plugin core code itself. I need to set some status flag in a different part of the website.
The method is defined in the plugin class like so:
add_action('plugins_action_name', [$this, 'local_method_name']);
The plugin method does not offer any actions or filters to hook into.
If this wasn't Wordpress but plain PHP I would write my own class, which extends the plugins' original class but has it's own method by the same name - but in Wordpress this gets me nowhere since I just move the problem from one plugin file to the next.
Is there a way to remove the plugins action altogether? And then redefine my own version of it?
Since the add_action has been called with $this - how would I remove it in some functions.php file?
Thanks
UPDATE
Indeed I want to learn how to handle different scenarios. Not expecting a one size fits all solution, but I do need to know how to avoid core changes, as each core-change complicates my deploy/update process exponentially.
Sure I will add some more details. My example plugin is WooCommerce Germanized Pro which comes with a WC_GZDP_Download_Handler::download() method which I needed to modify, in order to set some sort of "Warehouse downloaded the invoice" flag for each order. Which is later used to highlight new orders, filter orders that have been handed over to fulfilment etc.
There seems to be a Invoice Helper class which does in fact have a singleton pattern.
my modification in the download class is trivial:
public static function download( $pdf, $force = false ) {
... /* original code above, now my modifications */
if ($check_some_codition) {
$invoice = $pdf->get_invoice();
update_post_meta($invoice->get_id(), 'some-flag', 'some-value');
}
/* then resume original function (return the file to the browser) */
self::out( $filename, $file, $force );
}

Wordpress filter not being added

I have a plugin that uses apply_filters like this:
$additional_fields = apply_filters('attachment_meta_add_fields', $additional_fields);
In my theme's functions.php, I do:
function addAttachmentMeta($additionalFields) {
return $addtionalFields;
}
add_filter( 'attachment_meta_add_fields', 'addAttachmentMeta', 1, 1 );
But the function addAttachmentMeta never runs.
How can I alter my apply or add filter statements to make it so that addAttachmentMeta gets called?
Edit:
This is a custom plugin that I wrote based off tutorials on how to add additional attachment meta fields. The whole source is here: http://pastebin.com/7NcjDsK5. As I mentioned in the comments, I know this is running and working because I can add additional fields in this plugin file, but not by using the filters because the filter doesn't get added.
I can see var_dumps before and after the apply_filters statement, but the function I've pointed to with add_filter never gets called.
According to the order WordPress' core loads, function.php gets called after all plugins are loaded and executed.
You need to make sure the apply_filters() in your plugin runs AFTER your add_filter() is called. Otherwise at the point where your filters are 'applied', add_filter() simply hasn't been called yet.
What you could do is use a hook to make that part of your plugin run after functions.php has loaded. You could use the add_action('after_setup_theme', 'function_name') hook.
Wrap the last three lines of your plugin file inside a function and execute it after functions.php runs.
function addAttachmentMeta() {
$additional_fields = array();
$additional_fields = apply_filters('attachment_meta_add_fields', $additional_fields);
$am = new Attachment_Meta( $additional_fields );
}
add_action('after_setup_theme', 'addAttachmentMeta');

WordPress hooks for executing right before any action or page loading

I'm quite new to WP. Task is to develop a plugin for oauth authentication on one of not popular openID providers. I did the same for CodeIgniter project, but WP is a CMS and is little bit complex for me to understand. In Codeigniter i check authorisation before each action. In WP i need a hook which uses for it... before each page printing, or maybe.. it would be right to say before each action in terms of frameworks. What is this hook's name?
Last hook before loading the template is template_redirect
You can use it like this:
function my_function(){
// your code goes here
}
add_action( "template_redirect", "my_function" );
You can use init hook. It will be performed before element or HTML code. It's also useful to manage POST and GET variables. The syntax is something like this:
function yourfunction() {
dosomething();
}
add_action('init', 'yourfunction');
A list of all available hooks can be found here: https://codex.wordpress.org/Plugin_API/Action_Reference
Information about Hooks: https://codex.wordpress.org/Plugin_API#Hooks.2C_Actions_and_Filters
Other hooks must be suggested and will be added in a future release if is a good suggestion.
Or you'd have to edit the core files ;)
You mean a hook when all wordpress function will available but before any output including headers sent?
Well hook your function on init. That will call when visiting site. If you want this hook only for admin area then it is admin_init.

Can I override a PHP Function that is called in another file/plugin?

First of, I have read "Override default php function" and namespaces doesn't fulfil my need. I have also looked into PHP.net's override_function() but that can't help me with my problem either.
I am using a Wordpress plugin called Jigoshop as an eCommerce solution but in some cases I cannot remove the actions I need to apply my own structure to a 'single product' page. I do not want to edit the plugin files themselves as a plugin update may negate and remove my previous changes. Essentially, I want to control the output through my /themes/mytheme/functions.php file.
Has anyone come across this before whereby the original function is contained in a file I do not want to edit for that same 'updating' reason?
Thanks
EDIT (2012-11-21):
I have a custom function in my functions.php file like so:
function prepare_jigoshop_wrappers() {
remove_action('jigoshop_before_main_content', 'jigoshop_breadcrumb', 20);
add_action('jigoshop_before_main_content', 'custom_jigoshop_breadcrumb', 10);
}
add_action('wp_head', 'prepare_jigoshop_wrappers');
This essentially allows me to apply my own structure & configuration. For other default functions, it is a little more difficult. For example, the 'quantity selector', 'Add to Cart' button and 'stock availability' all are contained within a function called jigoshop_template_single_summary in the jigoshop_template_actions.php file calling the likes of _title, _price, _excerpt, _meta, _sharing & _add_to_cart.
The order these are displayed, I cannot seem to change. Therefore, I want to essentially redefine function jigoshop_template_single_summary() {...}
You may want to look into PECL extension. It may do what you need, docs on PHP.NET.
runkit_function_redefine
(PECL runkit >= 0.7.0)
I don't know this plugin very well but probably this is what you Need Theming with Jigoshop
Override some functions in php(also in common..) is never a good idea. If the plugin doesn't provide a way to hook into it's functionality is sometimes better to use an alternative plugin or rewrite the core code..

Categories