Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

share|improve this question
add comment

2 Answers

up vote 0 down vote accepted
+50

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.

share|improve this answer
    
Thanks! Much appreciated! –  Cor van Noorloos Feb 17 '12 at 14:57
    
Since I can't edit it myself, could you perhaps change help to helps? –  Cor van Noorloos Feb 17 '12 at 15:49
    
Thanks @CorvanNoorloos - I've made the corrections. –  Stephen Harris Feb 17 '12 at 16:46
    
Not entirely sure if 'd would be OK to ask another request :) Would it be possible to have it unchecked by default. (read having 'none' set as default) –  Cor van Noorloos Feb 18 '12 at 13:00
    
Never mind, just had to set $checked = checked( $url_type, 'file', false ); to none. Thanks again! –  Cor van Noorloos Feb 18 '12 at 13:26
show 1 more comment

Give the checkbox the same name as the text field and set it's value to the 'File URL' value. Either rename the Link URL text field or remove it. You can remove the buttons.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.