• H2GD Part 45: How to remain detached?

    How to prevent WordPress from attaching an unattached image to a post when you use Insert into post.

    Here’s a snippet of code that I wrote in response to a simple question from Ben Huson, posed on Twitter yesterday.

     * Implement 'wp_ajax_send-attachment-to-editor' to not attach an unattached media item
     * In WordPress TRAC 22085 there was a change
     * that caused unattached media files (images) to be attached to posts if they are inserted into the post.
     * If you don't like this strategy you can disable it using this simple, rather hacky, action hook.

    function dont_attach( $blah ) {
            $_POST['post_id'] = 0;
     * Hook into the AJAX request before WordPress, using priority 0

    add_action( 'wp_ajax_send-attachment-to-editor', 'dont_attach', 0 );


    Additional notes

    This solution relies on the following code being in wp_ajax_send_attachment_to_editor().

    if ( 0 == $post->post_parent && $insert_into_post_id = intval( $_POST['post_id'] ) ) {
      wp_update_post( array( 'ID' => $id, 'post_parent' => $insert_into_post_id ) );

    It also relies on there being no other code that requires the post_id value. If this were not the case then we’d have to:

    • reset it in a later hook, which doesn’t look particularly possible.
    • or implement a different solution to cause current_user_can() to fail.
    • or apply a pretend setting of $post->post_parent at the end of get_post().

    BTW: It’s not a problem with WordPress 4.2.2.

    Alternative approaches

    If you don’t want to use this code…

    • Don’t use Insert into post; use a shortcode instead. In this post I’ve used to display the featured image. The shortcode will display whatever image is currently marked as the featured image.
      Note: Setting a Featured image does not cause an unattached image to be attached to a post.
    • Develop your own solution for the ticket that Ben raised. core.trac.wordpress.org/ticket/32999



    Last updated:

    July 15, 2015


Tide times from tidetimes.co.uk

Tide Times & Heights for Langstone Harbour on
Sunday, 05 December 2021
Low Tide:04:44 ( 0.70m )
High Tide:11:51 ( 5.10m )
Low Tide:17:08 ( 0.60m )

Tide times from tidetimes.org.uk

Tide Times & Heights for Northney on
5th December 2021
04:44 Low Tide ( 0.47m )
11:47 High Tide ( 5.12m )
17:11 Low Tide ( 0.33m )