I've just started learning Wordpress and going through standard/default themes. If i have understood filters idea correctly, before we can apply them we need to add callback functions via add_filter($hook, $callback, $args). However looking at the 'twentyseventeen' theme i can't see those declaration for twentyseventeen_starter_content and it is used then with: $starter_content = apply_filters( 'twentyseventeen_starter_content', $starter_content ); (file functions.php) and twentyseventeen_front_page_sections - $num_sections = apply_filters( 'twentyseventeen_front_page_sections', 4 ); (file front-page.php). What am i missing and how does it work without setting callback functions?
apply_filters runs all the callbacks attached to it by add_filter to the same hook/tag. If there are no callbacks attached to that hook/tag it returns the second parameter (which is the value beng filtered) of the apply_filters. Therefore apply_filters( 'twentyseventeen_front_page_sections', 4 ); will return 4 if there are no add_filter('twentyseventeen_front_page_sections', 'callbackfunc');. Else it will return the result of the add_filter callback with the highest priority after going through all callbacks.
Priorities are set in the add_filter as the third parameter.
I don't know if this what you were looking for but i thought it might give you a better understanding.
Related
I'm using a plugin with this action:
add_filter( 'pre_comment_approved', array( $this, 'pre_save_review' ), 10, 2 );
I've written a plugin to do some tweaking to my site here and there. Mostly REST API customizations.
I'd like to override, subvert, ignore, remove, that filter. Right now I've got it "working" by just returning on the first line of the pre_save_review function.
I tried:
remove_action( 'pre_comment_approved', 'pre_save_review');
... but I wonder if there's some namespacing issue. I don't know a lot about PHP so I don't know how to refer to classes in other files/plugins, which I imagine is the issue.
Thanks!
You can remove all filters with a wordpress function "remove_all_filters"
remove_all_filters('pre_comment_approved');
This also has a second parameter to pass the priority number, since this one is added with priority 10 it would be this:
remove_all_filters('pre_comment_approved', 10);
Reference remove_all_filters
You'll want to use the remove_filter function for this.
See the Example in the documentation here: https://codex.wordpress.org/Function_Reference/remove_filter
If a filter has been added from within a class, for example by a
plugin, removing it will require accessing the class variable.
global $my_class;
remove_filter( 'the_content', array($my_class, 'class_filter_function') );
So maybe give the following a try;
global $this;
remove_filter( 'pre_comment_approved', array( $this, 'pre_save_review' ) );
I've been learning WP plugin development and the tutorial that I was looking at said that Wordpress has two hooks, one being add_action and the other being add_filter. Then i searched within the core to learn more about it and i noticed that the add_action function returned an add_filter function. What is the point of using add_action if i can just use add_filter? Can someone explain this?
I found it here: /wp-includes/plugin.php
function add_action($tag, $function_to_add, $priority = 10, $accepted_args = 1)
{
return add_filter($tag, $function_to_add, $priority, $accepted_args);
}
I had an action using admin_init to play with wordpress, i changed it to use add_filter instead and it worked without any problems. Isnt add_filter only used for string outputs while add_action uses hooks added throughout the wp core?
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 need your help! I am having trouble passing arguments to a function using pre_get_posts.
Purpose of passing arguments:
I have a custom taxonony-$taxonomy.php page which I use to list posts related to a particular category within the specified taxonomy. On this page I also retrieve & setup tags assigned to each post from a custom non hierarchical taxonomy and list them as links on the side. I have another taxonomy-$taxonomy.php (for the custom non hierarchical taxonomy mentioned above) setup so when a user chooses a link he/she clicks on a tag it will direct them to this taxonomy page. This is all working fine.
However, here's where I'm having issues. The posts listed on the non hierarchical taxonomy page are those associate to a particular tag but from multiple categories. What I’m trying to achieve is to list posts associates to the tag chosen and from the category previously being viewed. So for example: Lets say the user clicks on category ‘Accounting’, then chooses the tag ‘Creative Services’. All posts listed should not only be associated with ‘Creative Services’ must also be assigned to only the ‘Accounting’ category. This is where I’ve been trying to pass arguments to the function used by pre_get_posts.
How have I tried passing arguments?:
1- Setup globals: This way no arguments have to be past via the function being invoked. It did not work because the timing is off. The globals are not yet set when the action is called. Below is my code with example post_type and taxonomy. Notice $category is the global variable which hold the category within that taxonomy.
// Alter main query on doc_tag taxonomy templates
function only_query_doc_tag_posts( $query ) {
global $category;
if ( is_tax(array('doc_tag')) && !is_admin() ) {
if($query->is_main_query()) {
$query->set( 'post_type', 'post_type' );
$query->set( 'taxonomy', 'taxonomy_array' );
$query->set( 'taxonomy_name', $category ); //global
}
}
}
add_action( 'pre_get_posts', 'only_query_doc_tag_posts' );
Results:
When I view the main query it shows all the changes except the one using the global. If I manually insert the value into the function, instead of using the a global variable then it works. However, I'd like to be able to change this on the fly. Just so you're aware I do get the posts related to the tag but not those only associated to the category indicated by the global (when using the global).
2- do_action:
// Alter main query on doc_tag taxonomy templates
function only_query_doc_tag_posts( $query,
$post_types, //array
$taxonomies, //array
$category //global var
) {
global $category;
if ( is_tax(array('doc_tag')) && !is_admin() ) {
if($query->is_main_query()) {
$query->set( 'post_type', $post_types );
$query->set( 'taxonomy', $taxonomies );
$query->set( 'taxonomy_name', $category ); //global
}
}
}
add_action( 'pre_get_posts', 'only_query_doc_tag_posts', 10, 4 );
Then on the custom non hierarchical taxonomy page template I add the following:
do_action( 'pre_get_post', $post_types, $taxonomies, $category );
I have a feeling my second approach is not technically but I could be wrong, which is why I'm posting it here in hopes that someone can provide some direction. If I missed information that would help you help me please let me know.
Thank you in advance for helping me with this.
FYI: I've read these posts but did not get find a solution. Maybe it's there and I missed it? Please let me know.
Wordpress pre_get_posts and passing arguments
WordPress pre_get_posts not working
wp_query not filtering tax_query correctly in pre_get_posts
Passing arguments with add_action and do_action throwing error 'First argument is expected to be a valid callback'
Wordpress, filter posts if custom query variables are present. (pre_get_posts, add_vars)
Wordpress pre_get_posts category filter removes custom menu items
passing argument using add_action in wordpress!
I think I understand what you try to do.
Long in short - no, you can not do (do_action) by yourself, it is called by wp query parts.
If you try to pass arguments, you need look into [$query->query_vars].
I think a proper solution, for you need, is write a proper url rewrite rules, it can auto pick the taxonomy, and put it into url list, then values can auto show in [$query->query_vars] .
Which is [add_filter('rewrite_rules_array', 'set_url_rule');]
You may also need install plugin [rewrite-rules-inspector] to help you inspect the rewrite status.
Anyway, it could take you an afternoon to find the logical behind it, but then, it all makes sense.
If you just looking for a quick solution, you can inject some code into query_vars, you just need to :
add_filter('query_vars', 'insert_query_vars');
function insert_query_vars( $vars ){
array_push($vars, 'c_type');
array_push($vars, 'c_tax');
array_push($vars, 'c_term');
return $vars;
}
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.