3

I want to remove the attachment page option from the attachment's link-to selection.

I don't always want to tell my clients to avoid selecting this option, I simply want to take it out. But I am not programmer enough to do so. The Media File, Custom URL and None options are useful and should stay.

Does anyone know how to remove only the attachment page option there?

6

These options are hardcoded into the tmpl-attachment-display-settings Underscore media template in the /wp-includes/media-template file:

<script type="text/html" id="tmpl-attachment-display-settings">
    <h3><?php _e('Attachment Display Settings'); ?></h3>
    ...cut...
    <select class="link-to"             
            data-setting="link"
            <# if ( data.userSettings && ! data.model.canEmbed ) { #>
                data-user-setting="urlbutton"
            <# } #>>

        <# if ( data.model.canEmbed ) { #>
            <option value="embed" selected>
                <?php esc_attr_e('Embed Media Player'); ?>
            </option>
            <option value="file">
        <# } else { #>
            <option value="file" selected>
        <# } #>
        <# if ( data.model.canEmbed ) { #>
            <?php esc_attr_e('Link to Media File'); ?>
        <# } else { #>
            <?php esc_attr_e('Media File'); ?>
        <# } #>
            </option>
            <option value="post">
            <# if ( data.model.canEmbed ) { #>
                <?php esc_attr_e('Link to Attachment Page'); ?>
            <# } else { #>
                <?php esc_attr_e('Attachment Page'); ?>
            <# } #>
            </option>
            <# if ( 'image' === data.type ) { #>
                <option value="custom">
                    <?php esc_attr_e('Custom URL'); ?>
                </option>
                <option value="none">
                      <?php esc_attr_e('None'); ?>
                </option>
            <# } #>
        </select>
        ...cut...
</script>

You can always override the template by adding your own custom template, but I'm not sure how stable that would be, with respect to changes in the future.

If your image_default_link_type option is for example set to file and not post, then you could instead try to hide the attachment page option with some CSS hacks like:

/**
 * Hide the 'attachment page' option from the attachment link selection.
 * @see http://wordpress.stackexchange.com/a/173027/26350
 */
add_action( 'print_media_templates', function(){
    echo '<style>.post-php select.link-to option[value="post"] {display:none;}</style>';
});

Then the link options would be:

Before:

Before

After:

Hide attachment page option

Update:

In reply to the comment, we can handle both the Add- and Edit image media dialogs on the post edit screen with:

/**
 * Hide the 'Attachment Page' option for the link-to part.
 */

add_action( 'print_media_templates', function(){
    echo '
        <style>       
            .setting select.link-to option[value="post"],
            .setting select[data-setting="link"] option[value="post"] 
            { display: none; }
        </style>';
});
  • Dear birgire, thank you very much for your fast and comprehensive answer. I can only check it after the holy days and get back to you then, for the moment i wish you a merry christmas and a great start into 2015. – Sofian Dec 24 '14 at 11:29
  • Ah yes sorry just didnt get back, it works perfect and makes me very happy :) Just that now recently I discovered that it appear at a new spot as well since WP restructured the media handling. So when adding a image newly to an article, it is out. But when clicking on the edit pen, in the following edit screen it still appears. Can we also adress it there with a second trick? Thanks a lot for your help. – Sofian Apr 14 '15 at 10:49
  • aha, I see what you mean, please try the updated answer @Sofian – birgire Apr 15 '15 at 18:16
  • I updated my answer to also work on post-new.php – birgire Nov 4 '16 at 17:02
  • 1
    just the snippet under "update" does the trick for me. Thanks! – squarecandy May 18 '17 at 23:11

Your Answer

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

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