Opened 2 years ago

Last modified 14 months ago

#15847 assigned enhancement

Add filter in wp_insert_attachment for $data

Reported by: pampfelimetten Owned by: rhundesign
Priority: normal Milestone: Future Release
Component: Plugins Version: 3.0.3
Severity: normal Keywords:
Cc:

Description

Pretty self-explanatory, patch attached.

Attachments (1)

insertattachmentfilter.diff (709 bytes) - added by pampfelimetten 2 years ago.

Download all attachments as: .zip

Change History (10)

Why did you place the filter exactly before stripslashes_deep()?

What is the filter going to be used for?

Without the context of why you want the filter it is hard to know if this is the right filter to add!

comment:3 follow-up: ↓ 4   pampfelimetten2 years ago

@scribu: I basically looked at wp_insert_post, where it uses the same order:

	$data = compact( array(...);
	$data = apply_filters('wp_insert_post_data', $data, $postarr);
	$data = stripslashes_deep( $data );

@westi: On the one hand, there's a similar filter in wp_insert_post, but not in wp_insert_attachment. That's odd, I can't see a reason for that.
Particularly, I want to check the post_name field of the attachment before it get's entered in the db. The reason is, I want to avoid numeric-only post_names, as it interferes with my non-standard permalinks (%post_id% only). I've been looking into all the functions, and there are no hooks at all to do that, so I figured it would be easiest to just copy the behaviour of wp_insert_post and modifiy the data there before it gets handed to the sanitize functions.

comment:4 in reply to: ↑ 3   westi2 years ago

  • Milestone changed from Awaiting Review to Future Release

Replying to pampfelimetten:

@scribu: I basically looked at wp_insert_post, where it uses the same order:

	$data = compact( array(...);
	$data = apply_filters('wp_insert_post_data', $data, $postarr);
	$data = stripslashes_deep( $data );

@westi: On the one hand, there's a similar filter in wp_insert_post, but not in wp_insert_attachment. That's odd, I can't see a reason for that.
Particularly, I want to check the post_name field of the attachment before it get's entered in the db. The reason is, I want to avoid numeric-only post_names, as it interferes with my non-standard permalinks (%post_id% only). I've been looking into all the functions, and there are no hooks at all to do that, so I figured it would be easiest to just copy the behaviour of wp_insert_post and modifiy the data there before it gets handed to the sanitize functions.

Ok.

So a filter here would work for you.

But maybe a more generic filter on all post_names would be better and easier for you to use?

comment:5 follow-up: ↓ 6   pampfelimetten2 years ago

@westi: Well, as my main goal is to modify the generated slug, a filter in the function wp_unique_post_slug (wp-includes/post.php) would probably be the best solution, but I'm not sure where the right place to put it would be.

comment:6 in reply to: ↑ 5 ; follow-up: ↓ 7   westi2 years ago

Replying to pampfelimetten:

@westi: Well, as my main goal is to modify the generated slug, a filter in the function wp_unique_post_slug (wp-includes/post.php) would probably be the best solution, but I'm not sure where the right place to put it would be.

3.1 has 3 filters for this :-)

#15726

comment:7 in reply to: ↑ 6   pampfelimetten2 years ago

Replying to westi:

3.1 has 3 filters for this :-)

#15726

Oh great =)
In that case, my personal reason for the ticket is resolved.
In general, I still think that the filter has some validity, as there's no reason for the difference between wp_insert_post and wp_insert_attachment. So it's up to you core devs to decide if you want to close it or include it.
Thanks for the fast feedback!

Just in case anybody wants to do the same as me, this is the code I now use with the 3.1 version:

function cba_wp_unique_post_slug_is_bad_attachment_slug ($false, $slug) {
        if(is_numeric($slug)) return true;
        else return false;
};
add_filter( 'wp_unique_post_slug_is_bad_attachment_slug', 'cba_wp_unique_post_slug_is_bad_attachment_slug', '10', 2);
  • Owner set to rhundesign
  • Status changed from new to assigned

Is this mod going to be in future releases of WP??

Note: See TracTickets for help on using tickets.