Making the Add Media Link URL into a checkbox

Hopefully it’s OK asking a second question pretty much directly after the first one…

I’m currently having some difficulties accomplishing this:

I’ve been able to remove the ‘Attachment Post URL’ button using

function medium_attachment_fields_to_edit($form_fields) {
  $form_fields['url']['html'] = preg_replace("/<button type="button" class="button urlpost" title="(.)*">(.*)<\/button>/", "", $form_fields['url']['html']);
  return $form_fields;
}
add_filter('attachment_fields_to_edit', 'medium_attachment_fields_to_edit', 0, 2);

but I’m somehow unable to make the remaining buttons into a checkbox. Checked being the ‘File URL’, unchecked being ‘None’. (or changing things into a ‘Yes/No’ set of radio buttons if this would be easier to accomplish)

I’ve read about all articles about this matter of course (especially this one), but they about all seem to be about adding new settings, instead of altering existing ones.

2 Answers
2

You can just replace the html with a checkbox, ensuring the name of the checkbox is the name of the <input> we’re replacing.

You can also change the ‘label’ and the ‘help’ text, (see here for there the filter is fired) – just simply uncomment the appropritate lines and replace the text with your own.

The html we are replacing is normally provided by image_link_input_fields (see here). Looking into that code we see the name of the input is attachments[$post->ID][url] – which is the name we give our checkbox, with value the url of the file.

add_filter('attachment_fields_to_edit', 'my_attachment_fields_edit', 10, 2);
function my_attachment_fields_edit($form_fields,$post){ 
    $file = wp_get_attachment_url($post->ID);//Get the file link
    $url_type = get_user_setting('urlbutton', 'none');//Get user setting    
    $checked = checked($url_type,'file',false);//Automatically check box if setting is 'file'.

    $html = "<input type="checkbox" name="attachments[".$post->ID."][url]" value="".esc_attr($file )."" ".$checked."/>";

    $form_fields['url']['html'] = $html; //Replace html
    //$form_fields['url']['label'] = __('Link URL'); 
    //$form_fields['url']['helps'] = __('Enter a link URL or click above for presets.'); 

    return $form_fields;
}

The above works perfectly, however, you may have noticed when selecting ‘file url’ or ‘post url’, or ‘none’ that WordPress remembers your choice and uses this as the default for next time. Your choice is stored in the user setting ‘urlbutton’.

Unfortunately this method means that the setting is no longer updated (i.e. you can manually set the setting to ‘file’ or ‘none’, but it won’t behave dynamically as before). I think the reason is that clicking the buttons triggers an AJAX request to update the settings.

What this means is that unless you add some javascript to trigger this, all instances of $checked are a bit useless and can be removed.

Leave a Comment