passing an argument to add_action - wordpress

Given a function 'plugin_update' that would take a parameter $version, how can I pass a value to this method using the following syntax with the add_action method (as the method is within a class):
add_action('plugins_loaded',array('test-app-class','plugin_update'));
I have tried several variations that didn't seem to work. Any advice appreciated!

Short answer: No.
Let me explain why.
WordPress Event Engine Explanation
The add_action construct allows you to register a callback to an event. In your case, the event is plugins_loaded. The event is then fired when the construct do_action() or do_action_ref_array.
What happens when the event fires? WordPress Core does the following:
Assembles the arguments, if any are passed
Sorts the event registry table
Calls each of the registered callbacks in order (as determine by the priority number and when it loaded into the registry), passing the arguments (if any)
The arguments that are passed to each callback is determined by the firing construct. Huh? The do_action() specifies if any arguments will be passed to the callbacks. Then each callback can declare if it wants them or not.
Why Can't You Pass the Version?
The plugins_loaded event does not pass arguments. Your callback cannot specify parameters that are not passed from the firing construct.
In other words, the code that does the `do_action’ determines the arguments that your function can choose to accept and use.
Why? Because the do_action is the main firing mechanism. It says "Hello plugins and themes, I'm running now. If you are registered to me, then I'll call you tomrun too. And I might send you some parameters too."
Options for You
How do you get the version into to your callback?
You have many choices:
storing it in the database if it's dynamic (will change)
store it as an environmental variable and load it
store it as a configuration parameter and load it
As your using OOP, I'd advise injecting it when instantiating the object. I typically inject the dynamic configuration when I create the objects.
Reference
Here is a link to the Core code where WordPress fires do_action( 'plugins_loaded' );: click here to view on GitHub. Notice, there are no arguments being declared, which means none will be passed.

Related

what is the use of first argument of createAsyncThunk?

I'm new in redux toolkit and I've managed state with redux toolkit lately. But the thing I don't know, what is usage of first argument of CreateAsyncThunk. I've read this article: https://redux-toolkit.js.org/api/createAsyncThunk and according to this, CreateAsyncThunk has two argument and first argument is named type :
A string that will be used to generate additional Redux action type constants, representing the lifecycle of an async request
Ok. But we never need to call or use this argument again, so why is important to name this argument? I tried adsfds insted of requestStatus after / and my project worked perfectly! I also understand it also works even without slash.
It seems it doesn't matter what you write as first argument, It always works! So what is the usage of the first argument?
In Redux, every action is identified by a unique type string. So createAsyncThunk creates three actions for you - in your case with the type strings "adsfds/pending", "adsfds/fulfilled" and "adsfds/rejected".
If you do not use "asdfds" in any other createAsyncThunk, that's a perfectly fine thing to do, but if you look at the Redux Devtools browser extension to see what is happening in your application, a string like that might make it very difficult to read.

symfony2 doctrine2 : how to debug why a collection setter is not called

I am having an issue described here :
Can't persist symfony collection with double embedded form
https://stackoverflow.com/questions/27188337/symfony-setter-not-called-on-form-collection-type-even-with-by-reference-fals
I can't get a setter collection to get called even with the property by_reference set to true.
Here my question is how can follow the chain of commands conducting symfony/doctrine to call this setUserIngredients of a userIngredients collection ?
Which functions from the vendors files are called to identify the by_reference => false and call the appropriate setter ?
Thanks a lot !
To follow the chain of calls you can hook up a debugger. For example http://phpdbg.com/ or http://xdebug.org/ alternatively if you already know a specific function that gets called then you can temporarily put var_dump(debug_backtrace()) in your vendor code to see how it gets to that point

How to get the form name in class?

I have a form which is used for automatic journal postings.
On that form I have a Ok command button and in closeOk method of the form I call the method from my datasource table.
In the JournalCheckPost class's infoResult() method I want to determine if the method is called from my form. I know that it can be done with caller methods but I don't know how exactly it should be done technically.
It is bad practice to make a method depend on where it is called from.
What you can do is to pass an extra parameter to the LedgerJournalCheckPost and infoResult can then check that. This can be done by introducing a boolean flag and a parm method.
I think, there can be many situations:
You want to pass some parameters from form
You want to manipulate the form (for example refresh datasource after action is complete)
Something other
But in all the cases depending on particular form is not a very good idea.
In first case you can set parameters from code using parm methods, or, better pass parameters using the Args class
In the second you can cast Args.caller to some interface that contain all the methods you want and manipulate the form using that methods (see \Classes\SysFormRun_doRe usages for example)

How to update the value of a single field invoking appropriate validation

I'm making a module to allow users to update single fields on in this case, their user entity.
The code below is an example of the method I have initially been using to get it working and test other elements of the module
global $user;
$account = user_load($user->uid);
$edit = (array) $account;
$edit['field_lastname']['und'][0]['value'] = 'test';
user_save($account, $edit);
However this bypasses any field validation defined elsewhere in Drupal. I don't want to reproduce any validation written elsewhere - it's not the Drupal way!
My question is: Is there a function in Drupal 7 that can be called to update the value of a single field. I imagine such a function would clear the appropriate caches, invoke the fields validation etc.
I am aware the solution will be totally different to my current user object based one. I just can't for the life of me find the appropriate function in the API. I wander whether the fact I am looking for a save function alone is the problem - and that there are some other necessary steps that come before.
Any help gratefully appreciated.
Check out the drupal_form_submit function. It lets you submit forms from code. In this case, you could use it to the user edit form, which would then fire the appropriate validation.

What does apply_filters(...) actually do in WordPress?

I'm trying to understand some of the function in WordPress, but I can't get my head around what apply_filters(...) actually does.
Is someone able to clear this up for me with a few examples?
apply_filters($tag, $value) passes the 'value' argument to each of the functions 'hooked' (using add_filter) into the specified filter 'tag'. Each function performs some processing on the value and returns a modified value to be passed to the next function in the sequence.
For example, by default (in WordPress 2.9) the the_content filter passes the value through the following sequence of functions:
wptexturize
convert_smilies
convert_chars
wpautop
shortcode_unautop
prepend_attachment
do_shortcode
late answer
Short explanation
apply_filters() interacts with the global $wp_filters array. Basically it just checks the array if the current filter (or hook) has an action(/callback function) attached and then calls it.
Long explanation
When you attach a callback/action to a filter or hook, then you just add the callback name to global filters array. When then, in code (for e.g. a template, core or plugin file) a call to do_action() or apply_filters() happens, then WordPress searched through the array and calls the callback. The only thing more special with filters than with hooks is, that it returns the value (for further handling) instead of just firing the callback. So summed up: Hooks are to insert data, while filters are to modify data.
Here's what I'm gleaning, upon considering the most popular answer and additional resources:
$tag seems to be a synonym for the name of the hook. (That's not particularly intuitive to me.)
the_content is an example of a hook, of the "filter" type.
the_content hook consists of multiple filters.
Filters modify data. They basically filter the database, changing the data before the users view it.
A common use of apply_filters(), for instance, is to apply the_content filters to $content. In this instance, double returns will convert to <p> tags, smiley faces will convert to icons, etc.
"the_content" is a hook, while "the_content()" is a function.
In the most basic terms, apply_filters is used to initialise a filter hook... add_filter assigns a new function to hooks that have already been created.

Resources