問題描述

我設法添加了一個自定義 option-select 的圖像

function attachment_field_credit( $form_fields, $post ) {
    $field_value = get_post_meta( $post->ID, 'first_image', true );
    $isSelected1 = $field_value == '1' ? 'selected ' : '';
    $isSelected2 = $field_value != '1' ? 'selected ' : '';
    $form_fields['first_image'] = array(
        'label' => __( 'Use as first image' ),
        'input' => 'html',
        'html' => "<select name='attachments[{$post->ID}][first_image]' id='attachments[{$post->ID}][first_image]'>
                    <option ".$isSelected1." value='1'>Yes</option>
                    <option ".$isSelected2."  value='2'>No</option>
                   </select>"
    );
    return $form_fields;
}
add_filter( 'attachment_fields_to_edit', 'attachment_field_credit', 10, 2 );

現在我需要做幾乎相同的鏈接。所以如果我點擊 Pages -> Add New -> Insert / Edit Link 我得到這個:

我可以為這些鏈接添加另一個 option-select 字段嗎?怎麼做?

最佳解決方案

對話框 HTML 來自 WP_Editors::wp_link_dialog(),但沒有掛鈎。

我們可以使用 jQuery 來將自定義 HTML 附加到鏈接對話框,並嘗試覆蓋例如 wpLink.getAttrs(),因為它很短;-)

演示範例:

jQuery( document ).ready( function( $ ) {
    $('#link-options').append(
        '<div>
            <label><span>Link Class</span>
            <select name="wpse-link-class" id="wpse_link_class">
                <option value="normal">normal</option>
                <option value="lightbox">lightbox</option>
           </select>
           </label>
       </div>' );

    wpLink.getAttrs = function() {
        wpLink.correctURL();
        return {
            class:      $( '#wpse_link_class' ).val(),
            href:       $.trim( $( '#wp-link-url' ).val() ),
            target:     $( '#wp-link-target' ).prop( 'checked' ) ? '_blank' : ''
        };
    }
});

我剛剛做了一個快速測試,似乎工作,但需要進一步的測試和更新鏈接的調整。我這樣做的 Here’s an old hack 可能需要刷新。

Update

看起來你想將 rel="nofollow"選項添加到鏈接對話框中,所以我們來更新上述方法:

我們添加 rel 鏈接屬性:

/**
 * Modify link attributes
 */
wpLink.getAttrs = function() {
    wpLink.correctURL();
    return {
        rel:        $( '#wpse-rel-no-follow' ).prop( 'checked' ) ? 'nofollow' : '',
        href:       $.trim( $( '#wp-link-url' ).val() ),
        target:     $( '#wp-link-target' ).prop( 'checked' ) ? '_blank' : ''
    };
}

如果 rel 屬性為空,那麼它將自動從編輯器中的鏈接中刪除。

然後我們可以掛接到打開鏈接對話框時觸發的 wplink-open 事件。在這裏我們可以注入我們的自定義 HTML 並根據當前鏈接選擇進行更新:

$(document).on( 'wplink-open', function( wrap ) {

    // Custom HTML added to the link dialog
    if( $('#wpse-rel-no-follow').length < 1 )
        $('#link-options').append( '<div> <label><span></span> <input type="checkbox" id="wpse-rel-no-follow"/> No Follow Link</label></div>');

    // Get the current link selection:
    var _node = wpse_getLink();

    if( _node ) {
        // Fetch the rel attribute
        var _rel = $( _node ).attr( 'rel' );

        // Update the checkbox
        $('#wpse-rel-no-follow').prop( 'checked', 'nofollow' === _rel );
    }

});

我們使用以下幫助函數,基於 getLink()核心函數,獲取所選鏈接的 HTML:

function wpse_getLink() {
    var _ed = window.tinymce.get( window.wpActiveEditor );
    if ( _ed && ! _ed.isHidden() ) {
        return _ed.dom.getParent( _ed.selection.getNode(), 'a[href]' );
    }
    return null;
}

以下是輸出:

使用以下 HTML:

ps:這可以進一步測試,也可以擴展到支持翻譯

次佳解決方案

看看核心,wp_link_dialog 功能中沒有任何過濾器或動作的痕跡,這將使您的生活更輕鬆…

調查其他人如何解決這個問題,a plugin 或多或少與你想要的一樣。基本上它取消註冊 wplink.js,wp_deregister_script('wplink'); 並再次註冊該文件的修改版本,此時包括所需的額外字段。

雖然我不認為這是最好的方法 (考慮到更新 WordPress 時可能發生的後續衝突),我認為這是最簡單的方法。

希望有幫助!

參考文獻

注:本文內容整合自 Google/Baidu/Bing 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。