I'm not that new with WordPress and PHP but I don't understand a function anymore. Which function? This here:
add_filter( 'wpas_ticket_status_updated', 'test' );
function test( $post_id, $status, $updated ) {
error_log( 'updated' );
}
My plan is to log out a ticket when it's getting updated to export the log for testing purposes. So I've took a look into the developer documentation of Awesome Support and found the hook above:
https://developer.getawesomesupport.com/documentation/hooks-and-filters/wpas_ticket_status_updated/
So I've implemented it into my function.php (the normal way) and created a ticket. So the status needs to be created. After this I've changed the status of the ticket to in progress but I got nothing. Nothing about the creation and nothing about the status change.
What I'm doing wrong? I don't get it...
According to the documentation of the plugin, you are mixing filters and actions. Based on your provided code you are trying to trigger your test function once the filter wpas_ticket_status_updated is called. However, this function is not a filter but an action, therefore your hook is never to be called.
You should therefore change your code to add_action('wpas_ticket_status_updated', 'test') or find a related filter to call your desired test function.
Related
I am currently writing a plugin for WordPress, and I'm stumped with the add_action and do_action functions. I think I may be trying to use them in the wrong context, and if that's the case - could someone point me in the right direction?
I've come from hating WordPress to loving it, I never thought I'd write a wordpress plugin but now I am getting the hang of things, it's not all that different from normal PHP Development.
Anyway, if I was to have the following code in a plugin:
class aCoolPlugin {
function __construct() {
add_action('clear_auth_cookie', array( $this, 'aCoolFunction' ), 10, 2 );
}
function aCoolFunction( $arg1, $arg2 ) {
// Do something with the arguments
}
}
How would I actually run the aCoolFunction function? Now, I have tried the following:
do_action( 'init', "arg1 value", "arg2 value" );
However, before even trying to run that code I realised:
It makes no sense, since it will essentially be running the init action far too early, and;
It just doesn't work anyway!
So, from that I learnt:
I use add_action to hook into already existing functions for WP
do_action is reserved for new hooks really, can't really think of a useful situation where a hook should be called earlier, and then again later?
So, my question now is: How the heck can I pass my variables into the add_action code? The Codex doesn't say anything regarding arguments, so what are my options? or, is my logic and understanding flawed?
And what is the overall goal? The goal is to have a function with set arguments run every time a specific hook is called, and for the original hook to not be called any earlier/later than it should be
The add action does not accept any new variables nor will it return any variables to the function that runs it. But you can access variables that are passed to the function.
function custom_function($arga){
echo $arga;
}
add_action ('callname', 'custom_function', 10, 1);// 1= number of arguments accepted, use 2 for 2 etc...must add variables to do action call
//sometime later in the code
do_action('callname', $arga);
If you want to inject variables in you have to think a little bit back to basics. If you want to access variables within functions that are not passed you have 2 options:
Retrieve them from DB or server storage or similar
Use the Global declaration. You can accesss all variables that are set at the time the do_action is run.
You would rarely call do_action for wordpress hooks (see note below), but you may end up in a situation where you are coding a template FILE (which runs after plugins and then after themes) and you want to keep your logic in the plugin you could add do_action() into the template file and the actions set in the plugin will run at that point. Or similarly if you design a plugin that you want to be able to modify in the theme you could add an action to a late wp hook, within the called function, you could call do_action for your custom hook and hook functions to it in your theme. Loads of possibilities.
Whatever your logic for calling a WP hook early is, don't if you are within the WP load pattern
I would like to know if there is a function in php, or an event that we can subscribe to that is called each time an admin click on "update" a post or "move it to the trash" ? By the way, is it also possible to display a pop up on the admin page ?
Wordpress uses Hooks, which is basically saying I want to hook my function on yours, so when you get called, my function gets called. You have Action Hooks and Filter Hooks.
You can look all action hooks up here: http://codex.wordpress.org/Plugin_API/Action_Reference
You can look all filter hooks up here: http://codex.wordpress.org/Plugin_API/Filter_Reference
the one you're looking for is probably this one: http://codex.wordpress.org/Plugin_API/Action_Reference/save_post - the moment you save a post
http://codex.wordpress.org/Plugin_API/Action_Reference/delete_post - Just before the post is deleted (after deleted post is also shown on this one)
you can use it like this:
<?php
add_action( 'save_post', 'my_function' );
my_function($post_id) {
//do stuff here
}
?>
Do note that this is only for when you create plugins, I'm not sure what you're doing, but this only works when creating plugins.
if you prefer object oriented plugins, you can call functions with this:
<?php
add_action( 'save_post', array($this, 'my_function') );
?>
remember that you need to have my_function() inside the class!
Hope this helps.
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'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.
I'm trying to create a hook in one Wordpress plugin that could be used by other plugins. First off, is this even possible? I'm also sending some additional args so this may be 2 questions in one since I've been having trouble finding definitive information on how to do this.
Here is what I've tried so far:
In the plugin that is creating the hook (call it Plugin 1) I added:
do_action('plugin1_hook', $customArg1, $customArg2, $customArg3);
at the point that I want the hook to fire. Then, in a different plugin (Plugin 2), I added:
add_action('plugin1_hook', 'my_function');
and
function my_function($customArg1, $customArg2, $customArg3) { //my code }
This does not seem to be firing the function, however.
My refence for this has been the Wordpress hook comment_post, which is defined by Wordpress as:
do_action('comment_post', $comment_ID, $commentdata['comment_approved']);
and I am using as:
add_action('comment_post', 'my_comment');
function my_comment($comment_id) { //my code }
The above snippet is functioning properly.
I thought I'd post this as an answer as it's a little clearer to explain :)
When you hook a function, but do not specify the number of arguments, WordPress will always pass back one argument.
You won't get errors for something like this;
function do_my_hook($arg1, $arg2 = '', $arg3 = '') {}
add_action('my_hook', 'do_my_hook');
But you will for something like this;
function do_my_hook($arg1, $arg2, $arg3) {}
add_action('my_hook', 'do_my_hook');
WordPress is trying to call do_my_hook(), but it's only passing back one argument. The first example uses PHP default function arguments, so that you can call a function without passing all available arguments, but without error.
The second example will trigger a 'missing argument(s)' PHP error, as all three arguments are required.
The fix?
add_action('my_hook', 'do_my_hook', 10, 3);
The idea behind defining how many arguments your function takes is to avoid errors like these (though technically they are as easily avoided using default arguments!).
My guess is the second plugin is loading after the first one, so the hook has already fired by the time you add an action to it. You might try this for the first plugin:
function my_custom_hook_insertion($arg1, $arg2, $arg3){
do_action('plugin1_hook', $arg1, $arg2, $arg3);
}
add_action('plugins_loaded', 'my_custom_hook_insertion');
That will wait until all plugins are loaded before firing the hook.
Changing my add_action to this fixed the problem:
add_action('plugin1_hook', 'my_function', 10, 3);
The 10 represents the priority, and the 3 represents the number of args that the function will take. I'm not exactly sure how the matching works, since the default is 1, and I use plenty of hooks without specifying 0 args and I've used hooks that pass more than 1 arg but only used 1 arg in my function signature. Source: WordPress Codex: Function Reference/add action
It is working though, so cross plugin hooks are possible.