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


Wysiwyg Editor – hooks for "Add Media" button?

  • I have a Wysiwyg Editor field, with media upload button enabled, and I would like to identify and target the AJAX queries coming from its media upload button (“Add Media”). Specifically, I’d like to 1) filter the shown attachments in the modal and 2) operate on the uploaded attachment.

    I have done something similar with Image upload fields, by following this solution:

    Thus, for an Image field, I set up a filter for ajax_query_attachments_args and checked against the _acfuploader identifier with an if ($_POST['query']['_acfuploader'] == $THE_FIELD_ID) condition. Inside that condition I could then modify the AJAX query e.g. to show only attachments uploaded by the current user.

    Then, after submitting the form (for my very own use case it was appropriate to use the woocommerce_save_account_details hook), I simply got the attachment’s ID from the $_POST data using $_POST['acf'][$image_field_key] and could then modify that attachment.

    // (1) filter the shown attachments
    function restrict_current_user_to_own_attachments( $query = array() ) {
        // Allow requests only from users with access
        if ( ! current_user_can( 'upload_files' ) ) {
        // Only target the specific ACF field
        if ( $_POST['query']['_acfuploader'] == $THE_FIELD_ID ) {
            $user_id = get_current_user_id();
            if ( $user_id ) {
                // Query only attachments uploaded by the current user
                $query['author'] = $user_id;
        else if ( $_POST['action'] == 'query-attachments' && !current_user_can( 'administrator' ) ) {
            $user_id = get_current_user_id();
            if ( $user_id ) {
                $query['author'] = $user_id;
        else if ( $_POST['action'] == 'query-attachments' && current_user_can( 'administrator' ) ) {
            $user_id = get_current_user_id();
            // I could target a specific page, if I was unable to target the field.
            if ( strpos( $_SERVER[ 'HTTP_REFERER' ], "/${$MY_PAGE}" ) !== false && $user_id ) {
                //  manipulate AJAX query
        return $query;
    // Filter wp uploader shown attachments using ajax_query_attachments_args
    add_filter( 'ajax_query_attachments_args', 'restrict_current_user_to_own_attachments', 10, 1 );
    // (2) operate on uploaded attachment
    function my_save_form( $user_id ) {
        if ( isset( $_POST['acf'][$THE_FIELD_ID] ) ) {
            // ...
            if ( is_user_logged_in() && current_user_can('upload_files') ) {
                $attachment_id = (int) $_POST['acf'][$THE_FIELD_ID];
                // Do stuff to the attachment
    add_action( 'woocommerce_save_account_details', 'my_save_form', 10, 1 );

    The problem with the Wysiwyg Editor’s media upload button is that I couldn’t find anything identifiable about it, in order to be able to modify the AJAX query for that purpose. So I made a compromise and modified it for ordinary user roles and for more elevated users such as admins (current_user_can( 'administrator' )) I targeted the entire page instead of the field, using the $_SERVER['HTTP_REFERER'] (admins are trusted to not spoof the referer).

    However, I still need to operate on the attachments that were uploaded from that specific “Add Media” button, right after they were uploaded, and I’ve failed to find any hook where I could have both access to the attachment ID AND where the $_SERVER['HTTP_REFERER'] would point to the page of the ACF form. If anyone knows of any useful hook, it’d certainly save me plenty of more hours of trying.

  • When using a ACF WYSIWYG field the uploader used is the standard uploader used by WP. Any hooks you are looking for would be WP hooks that fire when a file is uploaded.

    Unfortunately I don’t have any idea what these hooks are and I have been unsuccessful at finding any information on this.

  • Thank you for taking a look into this! I had suspected that I should be focusing solely on WP hooks for this, and I have also been unsuccessful at finding the right one.

    Since there doesn’t seem to be anything identifiable about a specific “Add Media” button, I should find a hook where the $_SERVER['HTTP_REFERER'] will point to the form’s page plus where I can access the attachment’s ID after it has been uploaded.

    I’ve tried the ‘add_attachment‘ action but the referer is always media-new.php, then the ‘wp_handle_upload‘ filter doesn’t seem to provide a way to access the attachment’s ID, and ‘wp_handle_upload_prefilter‘ fires before the upload happens so no luck…

    P.S.: Also many thanks for the aforementioned Image field solution. It was very useful for my projects.

  • 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' ) ) {
        $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 );
Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.