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
Related
I'm having a problem manipulating data on a wordpress page without editing the code directly. Is there way to for example access this page https://github.com/WordPress/WordPress/blob/master/wp-admin/theme-editor.php and tell function get_files not to load up 404.php for example? I've used filters and hooks before but I feel lost in this particular case.
I have tried to hook into get_files to change the loop.
function yur_function()
unset($allowed_files["404.php"]);
}
add_action('get_files', 'yur_function');
Something like this.
Unfortunately, there is no hook in to that function so it's not really easy to modify that list of files.
get_files is defined in wp-includes/class-wp-theme.php and doesn't use apply_filters at all.
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/
I have a theme that has a plugin called Option Tree integrated in it to create a theme options panel (that plugin allows for a "theme mode" so it isn't installed as a plugin).
The Option Tree plugin allows you to access the saved data in the theme options by using this function:
$data_of_a_single_option = ot_get_option( 'name_of_option_field_to_retrieve', NULL );
I have another plugin that needs to get data saved in the theme option, but the theme (and option tree) is loaded after the plugin is, so calling the function leads to a "function does not exist" error.
Is there a way the plugin can call that function and get the data and be able to store that data in a variable?
I tried using an action like this:
function get_special_data() {
$test = ot_get_option( 'test_field', NULL );
return $test;
var_dump( $test );
}
add_action('after_theme_setup', 'get_special_data', 2);
// then just below in same plugin
$data_from_theme = get_special_data();
Because I read that Option tree is loaded after_Theme_setup with a priority of 1.
By using add_action, I can see the data (var_dump outputs it correctly), but I can't get the data from inside the plugin by calling the get_special_data() function because it sends a "ot_get_option function does not exist" error.
Is there any way to do this? Or am I trying to solve the wrong problem?
Thanks in advance!
I believe you are looking for the plugins_loaded action. That should work as long as the plugin defines that function without some other action required first to load the function.
Also, in your code example $data_from_theme = get_special_data(); is going to execute before the action callback you're defining actually fires.
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');
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.