Placement of add_action() for ajax callback?

When you send an AJAX request, wp-admin/admin-ajax.php is called, not your template file. This separate request has no knowledge of all existing code in all existing templates, because they aren’t even included. What is included: the theme’s functions.php and plugin files. This is why your callback works in a plugin. As a rule of thumb: … Read more

What’s hook to use immediately after a user is authentcated [duplicate]

juste before testing login and password, you have the action wp_authenticate: https://codex.wordpress.org/Plugin_API/Action_Reference/wp_authenticate juste after test and only if logging is successful, the action wp_login: https://codex.wordpress.org/Plugin_API/Action_Reference/wp_login and after logging, you can also modify the URL with the filter login_redirect: https://codex.wordpress.org/Plugin_API/Filter_Reference/login_redirect

Validate form in functions.php, send errors back to template

You need a filter not an action. Something like this (notes are commented into the code. You have a few PHP errors that you need to correct): function signup_validate_insert($post){ $errors = false; if (isset($post[‘submit_msg’])) { // validate , insert into database } return $errors; } add_filter(‘signup_insert’, ‘signup_validate_insert’); $errors = apply_filters(‘signup_insert’, $_POST); if((!empty($errors))){ // $errors is … Read more

How to add custom post meta to default blog post?

You can use the action hook save_post_{post_type}, so for the post type ‘post’ (blog posts) it is: save_post_post. For more info see the codex. add_action( ‘save_post_post’, ‘sw150216_save_reading_time’, 10, 3); function sw150216_save_reading_time( $post_id, $post, $update ) { $reading_time = estimated_reading_time( $post ); if ( $update ) { update_post_meta ( $post_id, ‘reading_time’, $reading_time ); } else { … Read more

Which action hook to use for function?

You don’t need an action, but a filter, because you want to do something with the result of a function. The filter is, predictably, called category_description In this way (didn’t test it though): function wpse236947_trim_category_desc ( $desc, $cat_id ) { // do your thing return $desc; } add_filter( ‘category_description’, ‘wpse236947_trim_category_desc’ );

Performance : Duplicating `add_action`

Well, the short answer is the first item consumes more memory then the second one. One of the reason would be that WordPress hooks uses call_user_func_array() to call al those functions dynamically. And call_user_func_array() actually takes an array as one of its parameter and do a search for a function with this name. So in … Read more

How do I pass arguments for multiple functions hooked to a single action?

The value for the default output should be passed to do_action() as the second parameter. E.g.: do_action( ‘my_action’, ‘<p>Default output</p>’ ); Note that the above code will not actually output anything. It’s just going to fire the callbacks attached to the my_action hook and it will pass ‘<p>Default output</p>’ to those functions. You can modify … Read more

How to remove function?

Essentially you want to remove this after it had been added, but before that hook fires. Which is hard to tell in general without knowing when does the parent theme executes this. Also in often confusing way functions.php of child theme is loaded before that of parent theme. Often makes timing of things counter–intuitive. The … Read more