In wp_insert_post(), there are a number of filters called before the post is actually saved anywhere. These filters are:
And so on. I’m really only interested in the first three, because I’d like to generate the excerpt and the title from the content, if they are left blank.
The problem is, am I guaranteed that these filters will be called in this order? Even if it is guaranteed, it’s still neither easy nor fun. But, I can’t really expect that — there must be a reason these are separate filters, right?
So I would either like to have these guaranteed some sort of order, or something that might make more sense anyway — just call one filter with all these options in a hash, then explode the hash when you get it back. For instance:
$basics_hash = apply_filters(‘save_pre’, array (
‘content’ => $post_content,
‘excerpt’ => $post_excerpt,
‘title’ => $post_title,
$post_content = $basics_hash[‘content’];
$post_excerpt = $basics_hash[‘excerpt’];
$post_title = $basics_hash[‘title’];
This way, a plugin can generate any part of a post from any other part, without having to assume anything about filter order.
If I can’t assume order, but I’m stuck with the hooks I’ve got, the other (very clumsy) possibility is to wait for the ‘save_post’ or ‘wp_insert_post’ hooks to be called, and if it’s a post I’m interested in, call wp_insert_post again with the new data. This does force the plugin to be a LOT more intelligent than it should have to be, though — for one thing, how do I prevent loops that way? And it still assumes that the save_pre filters will be called before the save_post hooks.
- The topic ‘Filter order guaranteed?’ is closed to new replies.