Hot answers tagged

7

For this example lets say we have the following do_action('bt_custom_action', get_the_ID(), get_the_title(), get_the_content()); The arguments that will be passed to add_action would be in this order the post id the post title the post content By default if we hook into our do_action without any arguments, like this add_action('bt_custom_action', '...


6

I found the magic code you need. Use current_filter(). This function will return name of the current filter or action. add_action('my_test_hook_1','my_test_callback_function'); add_action('my_test_hook_2','my_test_callback_function'); add_action('my_test_hook_3','my_test_callback_function'); add_action('my_test_hook_4','my_test_callback_function'); function ...


5

Everything Butterend_Toast says is correct, but I want to touch on why it works that way. Under the hood, do_action() and apply_filters() are just calling the core PHP function call_user_func_array(). When you run add_action() you are storing a reference to a callable, with a number representing how many arguments that function accepts. add_action( '...


4

I think I found answer to your question, it is: rest_{$this->post_type}_query filter hook. With this hook you are able to directly edit internal WP_Query parameter. For example, if you'd like to internally sort Posts by post_title by default, then you'd have to write something like this: function order_rest_post_by_post_title($args, $request) { $args['...


4

The action you are looking for is wp_body_open(). I do not know if you are writing your own theme or not but if you are using a pre-built theme, the author of your theme may not have included this support. If properly supported in your theme, any function hooked to this action will be called immediately after the page opening <body> element. WordPress ...


3

Ok - so, firstly you should know that it is a variable and not a function - in php this is indicated by the dollar symbol before the name: $variable as opposed to function() Second, you should note that $hook is passed as a parameter to the function block - like so: function_name( $hook ){ // this makes the variable $hook available inside the function - ...


3

There's a filter called login_redirect. add_filter( 'login_redirect', 'wpse377295_login_redirect', 10, 3 ); function wpse377295_login_redirect( $url, $request, $user ) { if ( is_wp_error( $user ) ) { // It's possible that the $user param is a WP_Error. If so, bail out. return $url; } if ( ! user_can( $user, 'update_core' ) { ...


3

The actual templates are loaded with require_once, so PHP automatically ignores the second attempt to load them. (You will trigger the 'get_header' hook twice though.) Here's the relevant code in get_header(): $templates[] = 'header.php'; if ( ! locate_template( $templates, true, true, $args ) ) { return false; } The third parameter to ...


3

This was (finally) fixed to Gutenberg (and will be applicable for both the image and the gallery blocks); and will add an option to WordPress options' database table. this was done in https://github.com/WordPress/gutenberg/pull/25578 and https://github.com/WordPress/gutenberg/pull/25582 It'll be added to WordPress 5.6 but it's already available in the ...


3

You need to unhook the function from that action specifically: remove_action( 'woocommerce_review_before_comment_meta', 'woocommerce_review_display_rating', 10 ); (e.g. look for the opposite add_action() line in includes/wc-template-hooks.php) You'll also need to make sure that this is run after WooCommerce has loaded and added the action you'...


3

remove_action requires that the second parameter is the name of the function/callable that was added, and the priority must match what was added. The official documentation says its parameters are: remove_action( string $tag, callable $function_to_remove, int $priority = 10 ) You can use also remove_all_actions: remove_all_actions( 'hook name' ); Remember, ...


3

The issue has nothing to do with the source code of a theme or plugin or with the configuration of Wordpress. It is: a 6+ months old bug in File block in Gutenberg that narrows down to a 4+ years old bug in core Wordpress. No solution exists for the moment, except for uploading files via Media section of Wordpress Dasboard. As file upload through that way ...


3

I would suggest first read the wordpress documentation about actions to understand how they work. Actions are aplit into two section, add_action and do_action do_action is the "location" where you hook into it. A basic example of do_action do_action('my_custom_action'); You can also pass arguments into do_action which can be available in ...


2

woocommcerce has this as a separate function wc_print_notices(); so just use that on your template pages. You don't need to echo or print it, just use it as is.


2

I also found that Cloudflare Rocket Loader would cause javascript errors in this situation. When the .js file was marked to be ignored, but the inline js was unable to be so marked, undefined errors would occur when the script tried to access the async-loaded inline data. The approach detailed in How to intercept already localized scripts only allows you to ...


2

Presenting Notices Gutenberg provides a mechanism for displaying notices in the form of the Notices Data package. In Gutenberg's editors, the package's selectors and action creators are exposed on the core/notices store, and can be accessed by any standard means therein, e.g.: useDispatch( 'core/notices' ) hook in functional components (including the edit() ...


2

How can I pass 'right' parameter into add_action? In short: You don't pass 'left' or 'right' itself to add_action(), but you can make the parameter be available in your callback by using the fourth parameter for add_action(). See examples below. So despite you may already know this, a hook is a specific place in a block of (PHP) code where custom functions ...


2

You can't, and it doesn't make sense to do so. Different pages/URLs enqueue different things, e.g. a WP Admin page won't enqueue the same styles and scripts, widgets might enqueue things conditionally, etc. But in WP CLI those hooks don't run, and there is no page or frontend. So the question doesn't make sense at a fundamental level. It isn't enough to know ...


2

Your answer above was close but is should be add_action( 'upgrader_process_complete', 'wp_answ_355114' ); not do_action( 'upgrader_process_complete', 'wp_answ_355114' ); noting that this is only the case when a plugin is updated view WP plugin updates.


2

A simple solution would be to register custom block style variations for the core/columns block and let the user select your custom padding settings from these, instead of editing class names in the extra classes field (which isn't too comfortable from a user's perspective). Gutenberg automatically outputs class names in the form is-style-(name of block ...


2

The hook that you're currently using is publish_<post type> which means the <post type> part is dynamic and the value is a post type name/slug. So for other post types like page and (a custom post type named) my_cpt, you would just need to change the "post" (or the <post type> part) in the hook name to page, my_cpt or whatever is ...


2

The problem is the order of execution. The order things run matters. For your code to function, it would require either add_action to implement time travel several microseconds into the past so that it can add the hook before it ran, or it would require do_action to implement precognition so that it knows about add_action calls in the future that have not ...


2

Short Answer The second parameter for useSelect is the second parameter for useCallback, so check the React website and you'd understand what the heck that second parameter is.. :) Long Answer useSelect is a custom React hook which uses the core useCallback hook in React which returns a memoized callback which can avoid expensive calculations on every render,...


2

My question is, is there a similar selector for when you don't know the post type of a post beforehand? No, at the current time of writing there is not. The fundamental problem is that the REST API doesn't provide a generic mechanism for getting a post type given a post ID. You can retrieve a post and it will say post but to do this you need to know the ...


2

Firstly, keep in mind that the structure of $wp_filter was changed in WordPress 4.7: https://make.wordpress.org/core/2016/09/08/wp_hook-next-generation-actions-and-filters/ As for your questions: Is this possible? Yes. Under what circumstances would this occur? If multiple functions have been hooked to the same action/filter, eg: add_action( 'save_post', ...


2

Not automatically. You don't know a filter exists until something tries to add it or runs it, and you have no guarantee what that filter is for without docs or a human being to describe it. You can do this by hand for core itself as it's well documented what the filters are, but there isn't a generic way to do this for 3rd party plugins/themes that provide ...


2

If you wanted to do this for all post types, you'd simply do define('EMPTY_TRASH_DAYS', 0); see https://codex.wordpress.org/Trash_status#EMPTY_TRASH_DAYS_option. But since you only want to do this for a custom post type, you will need to hook into 'trashed_post' like this: add_action('trashed_post', function( $post_id ){ [...] then check your trashed ...


2

You need to use strlen() to determine the length of a variable, so your second if condition is invalid. As it is, this question is not really related to Wordpress, so likely off-topic.


2

It's called post state, but not to be confused with post status (publish, draft, etc.) (although the state can of course contain the post status), and the core WordPress function which adds the post states such as the Privacy Policy Page to the posts (Posts, Pages, CPTs) list table is _post_states(), but the states are retrieved using get_post_states() which ...


2

The error happens because the filter callback (shapeSpace_disable_sitemap_post_meta()) expects to receive two parameters ($args and $post_type) and yet you call add_filter() without setting the fourth parameter to 2. So to solve the problem: // Replace this: add_filter('wp_sitemaps_posts_query_args', 'shapeSpace_disable_sitemap_post_meta'); // with this one:...


Only top voted, non community-wiki answers of a minimum length are eligible