Prevent attaching unattached attachments (nice!) on sending to editor

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Prevent attaching unattached attachments (nice!) on sending to editor

Barry Ceelen
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
_______________________________________________
wp-hackers mailing list
[hidden email]
http://lists.automattic.com/mailman/listinfo/wp-hackers
Reply | Threaded
Open this post in threaded view
|

Re: Prevent attaching unattached attachments (nice!) on sending to editor

J.D. Grimes
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 <[hidden email]> 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
> _______________________________________________
> wp-hackers mailing list
> [hidden email]
> http://lists.automattic.com/mailman/listinfo/wp-hackers

_______________________________________________
wp-hackers mailing list
[hidden email]
http://lists.automattic.com/mailman/listinfo/wp-hackers
Reply | Threaded
Open this post in threaded view
|

Re: Prevent attaching unattached attachments (nice!) on sending to editor

Barry Ceelen
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 <[hidden email]> 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 <[hidden email]> 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
>> _______________________________________________
>> wp-hackers mailing list
>> [hidden email]
>> http://lists.automattic.com/mailman/listinfo/wp-hackers
>
> _______________________________________________
> wp-hackers mailing list
> [hidden email]
> http://lists.automattic.com/mailman/listinfo/wp-hackers

_______________________________________________
wp-hackers mailing list
[hidden email]
http://lists.automattic.com/mailman/listinfo/wp-hackers
Reply | Threaded
Open this post in threaded view
|

Re: Prevent attaching unattached attachments (nice!) on sending to editor

J.D. Grimes
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 <[hidden email]> 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 <[hidden email]> 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 <[hidden email]> 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
>>> _______________________________________________
>>> wp-hackers mailing list
>>> [hidden email]
>>> http://lists.automattic.com/mailman/listinfo/wp-hackers
>>
>> _______________________________________________
>> wp-hackers mailing list
>> [hidden email]
>> http://lists.automattic.com/mailman/listinfo/wp-hackers
>
> _______________________________________________
> wp-hackers mailing list
> [hidden email]
> http://lists.automattic.com/mailman/listinfo/wp-hackers

_______________________________________________
wp-hackers mailing list
[hidden email]
http://lists.automattic.com/mailman/listinfo/wp-hackers
Reply | Threaded
Open this post in threaded view
|

Re: Prevent attaching unattached attachments (nice!) on sending to editor

Barry Ceelen
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 <[hidden email]> 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 <[hidden email]> 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 <[hidden email]> 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 <[hidden email]> 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

_______________________________________________
wp-hackers mailing list
[hidden email]
http://lists.automattic.com/mailman/listinfo/wp-hackers