In Edit Post page, I'm adding media from Add Media panel by clicking on "Insert into post" button. I'm trying to feed a Custom Field with the URLs from those selected images, but it is not working.

If I use the following action, I can access the field and fill it up with a dummy string without problems:

function set_field_value(){
    echo "<script type='text/javascript'>
        document.getElementById('acf-field_5cd68b4d12a20').value = 'test123456';
add_action('admin_footer', 'set_field_value');

By using the hook image_send_to_editor I'm able to get the URL from the selected images in Add Media panel, but when I try to use these URLs in a variable and set it as a value in a field by its ID, nothing happens. I thought I should be able to have a mix with the above code, but I was wrong. This doesn't work:

function feed_custom_field($html, $id) {
    $src  = wp_get_attachment_image_src( $id, $size, false );
    $image_url = $src[0];
    echo "<script type='text/javascript'>
        document.getElementById('acf-field_5cd68b4d12a20').value = $image_url;
    return $src[0].",";
add_filter( 'image_send_to_editor', 'feed_custom_field', 10, 9 );

How can I feed a Custom Field with URL from Add Media?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.