Insert Media – Attachment – Link to : Remove the “attachment page” option

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>';
});

Leave a Comment