You may want to have active field groups for front-end forms, which is very common, but at the same time you may not want those fields to appear anywhere else at the dashboard. So it’s not an unnecessary setting. A draft Page as the location has served me well so far. I just give it a title “ACF FORMS – WARNING – DON’T DELETE !!!”
I usually create a draft Page and use it as the location. Ugly.
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 );
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.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.