Support

Account

Home Forums ACF PRO Wysiwyg Editor – hooks for "Add Media" button? Reply To: Wysiwyg Editor – hooks for "Add Media" button?

  • I was probably doing something wrong before, because I’ve tried again with the add_attachment action and this time I was successful.

    function do_stuff_to_the_uploaded_attachment( $attachment_ID ) {
    
        global $MY_ENDPOINT;
        //$user_id = get_current_user_id();
    
        if ( ! current_user_can( 'upload_files' ) ) {
            return;
        }
    
        $args = array( 
            'ID'           => $attachment_ID, 
            //'post_title' => 'My default title ...', 
            'post_excerpt' => 'My default caption ...', 
            'post_content' => $_SERVER[ 'HTTP_REFERER' ], 
        );
        // Use the Title, Caption and Description,
        // to output/diagnose the referer and other data.
        wp_update_post( $args );
    
        /**
         * If the request came from the specific URL, then do stuff to the attachment.
         * 
         * Note: Ideally we'd target the "Add Media" button itself,
         * but since there doesn't seem to be anything identifiable about it,
         * we're targeting the URL of its page.
         */
        if ( strpos( $_SERVER[ 'HTTP_REFERER' ], $MY_ENDPOINT ) !== false ) {
            // Do stuff to the attachment by using its ID i.e. $attachment_ID
        }
    }
    add_action( 'add_attachment', 'do_stuff_to_the_uploaded_attachment', 10, 1 );