A name for an event that is executed at specific points throughout the WordPress core.

learn more… | top users | synonyms (2)

21
votes
1answer
4k views

Passing a parameter to filter and action functions

Is a way to pass my own parameters to the function in add_filter or add_action. For example take a look in the following code: function my_content($content, $my_param) { do something... using ...
14
votes
1answer
4k views

Why does save_post action fire when creating a new post?

I'm surprised by the fact that my function that I've tacked onto the save_post action fires when I click the "New Post" link in the Admin Dashboard. Note - this is before I've pressed Save or Update, ...
14
votes
2answers
6k views

Difference Between Filter and Action Hooks?

I have been looking at the plugin API a bit more in depth recently and I was wondering what real differences there were between action and filter hooks. They both are events that receive data as a ...
11
votes
3answers
1k views

get_template_part vs action hooks in themes

It seems to me both of these afford the opportunity for the end user to modify a theme without actually editing the themes files (via child themes). My question is, is one method preferred over the ...
11
votes
1answer
5k views

How to test wp_cron?

This is kind of a stupid question... I scheduled a action to run every hour: if(!wp_next_scheduled('my_hourly_events')) wp_schedule_event(time(), 'hourly', 'my_hourly_events'); ...
10
votes
1answer
506 views

How to create an API for my plugin?

I have been developing plugins for WordPress, most plugins I have developed use two or three classes, hence not as huge as Buddypress or WooCommerce. I am planning to develop two open source plugins ...
10
votes
1answer
5k views

how to limit search to post titles?

Is there a way to limit search to post titles? I know I can modify query.php core file but there must be a way to do it with hooks right? Thanks in advance!
10
votes
1answer
533 views

Using jQuery to delete data stored in wp_options

I wonder if anyone could advise me further on my problem. Part of my plugin stores log files for debugging purposes. I have successfully displayed them in a (div#log) in my admin page using jquery ...
9
votes
4answers
2k views

after_setup_theme always runs

I am setting up a child theme for some of my faculty members, and as a part of the theme, I would like a handful of plugins to be activated at the time that the theme is activated. So, naturally, I ...
9
votes
2answers
2k views

Why do some hooks not work inside class context?

I'm pretty stumped on this one. I'm using add_action inside my plugin class to do certain things- add scripts & styles to the head, wp_ajax, etc. Here's the actions, in the __construct: function ...
8
votes
3answers
4k views

remove_action or remove_filter with external classes?

In a situation where a plugin has encapsulated its methods within a class and then registered a filter or action against one of those methods, how do you remove the action or the filter if you no ...
8
votes
5answers
6k views

add_action reference a class

Is it possible to reference a class instead of a function in 'add_action'? Can't seem to figure it out. Here is just a basic example of the function in question. add_action( 'admin_init', 'MyClass' ...
8
votes
3answers
5k views

How to enqueue scripts on custom post add/edit pages?

I'm trying to enqueue a JS script only when someone is adding or editing a custom post type I created called "recipes". Currently the script works ok when I do this: if (is_admin()){ ...
7
votes
2answers
2k views

Return $post_id when DOING_AUTOSAVE?

I see the following pattern over and over, on this site and on other places: add_action( 'save_post', 'wpse14169_save_post' ); function wpse14169_save_post( $post_id ) { if ( defined( ...
7
votes
2answers
151 views

Remove Actions/Filters added via Anonymous Functions

That's a freaking bad practice I must say. Spent last two hours finding a solution to remove actions and filters added via Anonymous functions. This is the code used on a Parent Theme, and I need ...
7
votes
3answers
1k views

Remove an action from an external Class

I'm trying to do something similar to this question here: remove_action or remove_filter with external classes? I'm trying to remove the <!-- This site is optimized with the Yoast WordPress ...
6
votes
3answers
4k views

How to know what functions are hooked to an action/filter?

Is there a way to know what functions are hooked to a particular hook? For example if I'd like to know what functions are hooked to the wp_head hook.
6
votes
2answers
6k views

Is there a hook that runs after a user logs in?

I am writing a plugin that fetches some extended user info from a remote service and I need it to execute its function each time a user logs in. Is there a hook that gets fired after login that I can ...
6
votes
3answers
2k views

Conditional wp_enqueue_script on a page

For efficiency we are trying to hook some JS scripts only a certain page template of a theme that is to hold a form: page-with-form.php On the theme functions.php we've defined a init_method as ...
6
votes
1answer
2k views

How to override existing plugin action with new action

I'm using a plugin. It has an action like this. add_action('publish_post', 'old_action'); function old_action($pid) { "code goes here" } } I'm writing a module for this plugin. So i need to ...
6
votes
1answer
67 views

Can my “add_action” function know the name of the hook calling it?

Lots of hooks: add_action('thesis_hook_after_comment', 'pj_time'); add_action('thesis_hook_after_comments', 'pj_time'); add_action('thesis_hook_after_comment_box', 'pj_time'); ...
6
votes
1answer
2k views

add_role() run only once?

I was surprised to discover that add_role() modifies the database and fails if the role already exists. There are two implications here, one first more serious than the other: 1) if you're in ...
6
votes
2answers
977 views

Extract image from content and set it as the featured image

The problem: I'm posting from the Wordpress iPhone app. When adding an image, the image shows up in the description itself, which I don't like. What I'm trying to do: Remove the image from the ...
5
votes
2answers
110 views

Valid characters for actions, hooks and filters

I haven't ever seen a tag such as do_action('something_10'). Is it invalid if I use numbers? If the method is valid, how can I write different numbers in the tag string conditionally? For example, is ...
5
votes
1answer
924 views

Difference between do_action('admin_enqueue_scripts', $hook_suffix) and do_action(“admin_print_styles-$hook_suffix”) syntax

Source ref: do_action('admin_enqueue_scripts', $hook_suffix); do_action("admin_print_styles-$hook_suffix"); Laying aside the obvious difference in base action ( admin_enqueue_scripts vs ...
5
votes
2answers
2k views

Earliest hook to reliably get $post/$posts

What's the earliest possible action I can hook into where I'll be able to access the global $post/$posts variables on both the front and back ends? I've tried looking through the Codex reference, Adam ...
5
votes
1answer
2k views

Action hook on Edit custom post type?

Is there an explicit action hook that will fire when (or just before) the admin Edit page renders for a custom post type? Something similar to {$new_status}_{$post->post_type}? I'm trying to find the ...
5
votes
2answers
3k views

Function to execute when a post is moved to trash .

Hi is there an action which can be performed while a post is moved to trash .... I tried delete_post and deleted_post. It works but twice for each action ( before and after the action as written in ...
5
votes
1answer
1k views

Difference between do_action and add_action

This question might be wrong, I am not sure about it. Because I am not clear on this. I know add_action it is used to to hook our function to the specified function. For example ...
4
votes
2answers
2k views

Does an activated plugin automatically mean its methods are available to other WP functions?

I made a WordPress plugin like this: Class MY_CLASS { //codes } Global $myclass; $myclass = New MY_CLASS (); After installed and activated the plugin, can I use this class in other plugins without ...
4
votes
5answers
652 views

Implementing advanced add_* function wrappers

add_action() and add_filter() are major functions. However in some scenarios add one more function and hook it somewhere approach gets bulky and inconvenient. I had determined for myself several use ...
4
votes
3answers
1k views

How can I find out what functions are assigned to actions?

Is there any way to know what functions are added to actions on a certain page in WordPress?
4
votes
3answers
3k views

Which hook should be used to add an action containing a redirect?

I want to build a plugin that grabs certain url params from the query string to build a new query string for the same page. I'm following the excellent the Professional WordPress Plugin Development ...
4
votes
3answers
4k views

Perform an action when post is updated/published

I'd like to run a custom query using some meta data whenever a post is updated or published. Is there something I can put in functions.php to fire when those events occur?
4
votes
2answers
8k views

When to use add_action('init') vs add_action('wp_enqueue_scripts')

In my theme's functions.php, I'm calling an add_action in order to gain a measure of control on where jquery is loaded (in the footer along with my theme's other scripts). The problem I'm having is ...
4
votes
1answer
949 views

Create posts on user registration

How can I create let say 3 posts after the user finish the registration ? The ideal outcome would be to create 3 posts using the user as the author of the posts and with some pre-defined values. ...
4
votes
3answers
146 views

How to restrict actions and filters “properly” by conditions

Which one is correct when adding actions/filters/etc.... Both methods work fine and I've seen people do things both ways, but I'm assuming one is correct. My gut tells me that wrapping the if ...
4
votes
1answer
105 views

How Do I Load My Action Earlier Enough?

With add_action, I want to intercept as early as I can in my plugin and send a 304 Not Modified header per some rules. Does anyone know what the order is of events? When I view this page, it seems to ...
4
votes
1answer
212 views

Why, Where, and When to use reference pointers in filters/hooks?

Why, Where, and When to use reference pointers in filters/hooks? What are the potential cons of not using them when suggested or required? Just looking for a more detailed answer than the codex ...
4
votes
1answer
336 views

Difference between do_action_ref_array() and do_action()

I am having trouble wrapping my head around these two functions. I understand do_action() but I don't see clearly when do_action_ref_array() would be useful. Couldn't we pass an array to do_action() ...
4
votes
1answer
95 views

Getting the action's tag name within the action

There's a function attached to a set of actions, like this: add_action('some_tag', 'the_function'); function the_function($maybe_some_vars){ // ... } Is it possible to find out the action name ...
4
votes
1answer
29 views

Wordpress Multisite: Adding actions to only one site

I have Wordpress Multisite installed with WooCommerce network activated. I want to add an action to a hook in ONE of the sites that has WooCommerce installed, instead of all of them. For example, I ...
4
votes
1answer
713 views

How to change “Draft” string for status of custom post type to “Unavailable”?

ANSWER MOD: just an important mod to the chosen answer: // check if you actually have drafts; also avoids extra '|' separator if (isset($views['draft'])) { // 'Drafts' should be added (and come ...
4
votes
1answer
162 views

Store source permalink on XMLRPC calls

In a theme I'm developing, I need to link to the source post when the post has been stored using XMLRPC. That implies that every time Wordpress asks for a permalink (using get_permalink()) the theme ...
3
votes
1answer
3k views

Difference between after_setup_theme and init action hooks?

What is the difference between the after_setup_theme and init action hooks? It seems they are both called right before any page in WordPress is loaded (even admin pages).
3
votes
3answers
2k views

How to correctly get post type in a the_title filter

I want to prepend text to the post titles of a specific custom post type, but the filter below didn't work. Instead of changing only that CPT's post titles, it changed all titles on the given CPT's ...
3
votes
1answer
169 views

Which action for triggering cron “wp”or “init”?

Which one do you recommend using within a plugin and why? add_action( 'wp', 'trigger_me' ); function trigger_me() { if ( !wp_next_scheduled( 'my_plugin_cron' ) ) { ...
3
votes
3answers
710 views

How can I remove the WP menu from the admin bar?

I never use the WP menu (the logo and its child items) except when I click it accidentally. And it is wasting my time when I navigate per keyboard. Plus, the support page is not our site. How can I ...
3
votes
2answers
733 views

Correct place to register and enqueue scripts

I'm queueing several scripts in my themes Functions.php: if ( ! function_exists('b99_init_scripts') ) : function b99_init_scripts(){ if ( !is_admin()){ ...
3
votes
2answers
88 views

How to count number of functions attached to an action hook?

I have some actions like this function simple_notification() { echo '<div>This is a notification</div>'; } add_action( 'site_notices', 'simple_notification' ); ...