Hot answers tagged

7

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 ...


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', '...


7

Yes, delete_expired_transients is a cron event that runs once per day and the function delete_expired_transients() is automatically called when the cron event runs — see wp-includes/default-filters.php. So you do not need to call the function manually like you did in your my_custom_fn() function. And if you use a plugin like WP Crontrol, you can easily view ...


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( '...


3

Shouldn't the 2nd argument be a function? The second argument should be a value of type callable. callables are a fundamental type in PHP. E.g. these are some valid callables: Value Equivalent 'test' test() [ $obj, 'test' ] $obj->test() [ 'foo', 'bar' ] foo::bar() function() {...} or closure $foo = function(){...}; $foo(); And so on, see the ...


3

So I presumed your sub plugin loads after the main one, right? Here i can see that new value is added to array but now i have to define test2 function inside my sub plugin If you're certain test2 successfully added to the array and that test2 was being called from MyClass::autoload_function(), then you can try using an early hook like init to define test2, ...


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

As for the user rows in the Users list table (at wp-admin/users.php), you would use the user_row_actions hook like so: add_filter( 'user_row_actions', 'my_user_row_actions', 10, 2 ); function my_user_row_actions( $actions, $user_object ) { // Remove the Edit action. unset( $actions['edit'] ); // Add your custom action. $actions['my_action'] =...


2

Maybe someone need it: I modified a bit the code of remove_object_filter in @gmazzap answer, to make it works with WP 4.7+: /** * Remove an object filter. * * @param string $tag Hook name. * @param string $class Class name. Use 'Closure' for anonymous functions. * @param string|void $method Method name. Leave empty ...


2

You can't. Not how you've written it. You need to be able to access the instance of ChildAdminClass somehow, but it only exists within the MainAdminClass class and isn't exposed in any way. In your code attempting to remove it you've referenced $child_admin as a global variable, but you didn't create it as a global variable. If you did this: public function ...


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

Yes, you can nest actions. Yes, your example will cause an infinite loop.


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

I think it's better to use the $wpdb->delete() function to remove a row. $article_id = $item['article_id']; $table = 'wp_copywriter_articles'; $wpdb->delete( $table, array( 'id' => $article_id ) );


2

If you look at the end of the function get_cancel_comment_reply_link( $text = '' ) in the file wp-includes/comment-template.php, you see the filter cancel_comment_reply_link. return apply_filters( 'cancel_comment_reply_link', $formatted_link, $link, $text ); It may work.


2

WordPress fires the updated_{$meta_type}_meta hook which fires after the metadata is updated. In your case, the hook would be updated_post_meta and you could have your function run on that hook instead of the save_post hook.


2

It's not Gravity Forms, it's ob_end_clean(), which just deletes the buffer and returns a boolean value. (In this case, true, which is getting expressed as 1.) Is there a pressing reason to use the ob_* functions? The code would be more readable (and predictable) if it was something like this. public function collectGFData($entry, $form ) { error_log(&...


1

The current_user_has_role() is not a WordPress core function, on the other hand there's wc_current_user_has_role( $role ) in WooCommerce where $role is a string, but 3rd party plugins are general off-topic. Creating this function was closed as wontfix in this ticket: https://core.trac.wordpress.org/ticket/22624 where Andrew Nacin summarized it neatly: ...


1

It's a timing issue. This tries to call is_home() before that function exists. $my_ver = check_home(); It works in the function called by add_action because then is_home() does exist because WP has fully loaded. If you load php files in your plugin, then you can call check_home anytime in those files. For example: define( 'MY_PLUGIN_PATH', ...


1

You could restrict your admin_init callback with e.g. a is_network_admin() check.


1

I don't understand any of it but it seems to work just fine. Thank you @Buttered_Toast for mentioning the useful thread. add_action("init", function() { global $wp_filter; foreach($wp_filter["wp"][10] as $id => $filter) { if(is_array($filter["function"]) && count($filter["function"]) == 2 &&...


1

I just try to add some content at the bottom of each posts from a specific category but the content is added in all posts. Are you sure your "Hello World!" text is added in all posts? Because is_single() && is_category() would never be true because a query can't be for a single post and a category archive at the same time. 🤔 So if you ...


1

Update In response to the new code in the updated question, you could actually use $wpdb->get_col() with a single SQL command via a JOIN clause, like so: $query = "SELECT c.comment_ID FROM $wpdb->comments c JOIN $wpdb->posts p ON p.ID = c.comment_post_ID WHERE c.user_id = $user_ID OR p.post_author = $user_ID"; $comment_ids = $wpdb-...


1

In your login link you call the WordPress function wp_login_url() to generate the login URL. Mr Peattie's question in the comments refers to the documented parameter redirect supported by this function. When in doubt, always check the documentation: https://developer.wordpress.org/reference/functions/wp_login_url/ There are actually two parameters for ...


1

You can check if the query returned any posts by doing $home_query->have_posts(). $home_query = new WP_Query( array('posts_per_page' => get_query_var('posts_per_page'),'paged' => $current_page ) ); if( !$home_query->have_posts() ){ // Do this if $home_query had no posts. }


1

I think for your purpose wp_untrash_post_status filter will be enough. Will work with single and multiple posts restore. Filters the status that a post gets assigned when it is restored from the trash (untrashed). add_filter( 'wp_untrash_post_status', 'change_untrash_post_status'); function change_untrash_post_status($status){ return 'pending'; } P.S....


1

Used shorcodes can be retrived only if they are added in post content (or any content saved in db). They can not be retrived if hardcoded in template files (TBH, they can, but that's not the issue here). This code can help you to get the shortcodes used in a post content. function wpse387291_get_shortcodes() { global $post; $pattern = ...


1

I suggest that you install FPW Category Thumbnails plugin. It will cover every scenario mentioned in your question. It will save you from maintaining code in functions.php, and will do it visually! The built-in FPW Post Thumbnails will add very powerful styling to your featured images (borders, shadows, margins, backgrounds, etc). You will be able to switch ...


1

A few changes to your code: Changed the action hook to wp_head. Removed the false 'pluggable' wrapping of your function. if ( ! function_exists( 'post_is_in_descendant_category' ) ) { function post_is_in_descendant_category( $cats, $_post = null ) { foreach ( (array) $cats as $cat ) { // get_term_children() accepts integer ID only ...


1

For those looking, Gravity Forms now provides this solution: add_filter( 'gform_form_trash_link', 'prevent_form_deletion', 10, 2 ); function prevent_form_deletion( $trash_link, $form_id ){ if ( $form_id == 75 ) { $trash_link = '<a class="submitdelete" onclick="alert(\'This form cannot be deleted.\');" onkeypress="alert(\'This form ...


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