[wp-hackers] Prevent attaching unattached attachments (nice!) on sending to editor

Barry Ceelen barry at multipop.org
Wed Sep 9 15:08:49 UTC 2015


Maybe less fragile, but kind of weary of replacing core functions with my own :)
Thank you for pointing out the option of looking at $_REQUEST['action’], that was what I was looking/hoping for.

This does the trick for me now:

add_filter( 'wp_insert_post_parent', 'xqr_z100a_filter_insert_post_parent', 10, 2 );

function xqr_z100a_filter_insert_post_parent( $post_parent, $post_ID ) {

	if ( ! isset( $_REQUEST['action'] ) || 'send-attachment-to-editor' != $_REQUEST['action'] ) {
		return $post_parent;
	}

	$post_before = get_post( $post_ID );

	if ( 0 != $post_before->post_parent ) {
		return $post_parent;
	}

	return 0;
}

Thanks for your help J.D.!

Le Plugin: https://github.com/barryceelen/wp-xqr-z100a




> On 06 Sep 2015, at 22:01, J.D. Grimes <jdg at codesymphony.co> wrote:
> 
> Replacing the handler seems less fragile, so its probably best that you do that anyway, but to answer you question: yes, you I think can know if the update was being triggered by sending an attachment to the editor, by checking that  $_REQUEST['action'] is 'send_attachment_to_editor'. I guess I could have said that more clearly in my original reply. :-)
> 
> 
>> On Sep 5, 2015, at 8:28 PM, Barry Ceelen <barry at multipop.org> wrote:
>> 
>> Hi J.D.
>> 
>> When I use filters inside wp_update_post(), besides probably knowing I’m DOING_AJAX and that I’m dealing with an attachment, I lose context insofar as that I cannot know if the update was triggered by sending an attachment to the editor.
>> Or am I missing something here?
>> I went with your suggestion of replacing the core handler, works like a charm.
>> 
>> Thanks for your reply!
>> 
>> 
>>> On 04 Sep 2015, at 17:55, J.D. Grimes <jdg at codesymphony.co> wrote:
>>> 
>>> Hi Barry,
>>> 
>>> When the attachment is updated, wp_insert_post() is ultimately called to update the post_parent field. In that function you have several filters that you could hook into. wp_insert_post_parent or wp_insert_attachment_data seem like good options. You'd just need to check that that Ajax request is being performed and the post being saved is an attachment, and not modify the post array otherwise.
>>> 
>>> Another possibility would be to add your own Ajax handler for that action and unhook the core one. This might actually be the most straightforward and least buggy approach.
>>> 
>>> -J.D.
>>> 
>>>> On Sep 4, 2015, at 11:31 AM, Barry Ceelen <barry at multipop.org> wrote:
>>>> 
>>>> Hi All,
>>>> 
>>>> Unattached images inserted into a post via the media library tab automatically get attached to the post.
>>>> I’m looking to prevent this from happening.
>>>> 
>>>> The wp_ajax_send_attachment_to_editor() function (https://developer.wordpress.org/reference/functions/wp_ajax_send_attachment_to_editor/) has a filter at the end, but by that time the attachment is already attached.
>>>> Any tips on handling this?
>>>> 
>>>> Thanks!
>>>> 
>>>> Barry



More information about the wp-hackers mailing list