You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WordPress媒体上传窗口自定义消息显示及下拉选项添加失败求助

解决WordPress媒体上传窗口「链接到」下拉框添加自定义选项的问题

嘿,我一眼就看出问题所在了——你当前的代码是针对图库(Gallery)设置面板添加自定义选项的,和你要修改的「媒体上传窗口的LINK TO下拉选择框」完全不是同一个模块!这就是为什么代码已经出现在页面源码里,但目标下拉框却没加载新增选项的原因。

下面是针对你的需求的修正方案,直接替换原插件代码即可:

<?php
/**
 * Plugin Name: Add Custom Option to Media Link-To Dropdown
 * Plugin URI: http://example.com/
 * Description: Adds a custom option to the "Link To" dropdown in WordPress media uploader
 * Version: 1.0
 * Author: Your Name
 * License: GPL2
 */

add_action('print_media_templates', function() {
?>
<script type="text/html" id="tmpl-custom-link-to-option">
    <option value="custom-url"><?php _e('Custom Link (My Option)'); ?></option>
</script>

<script>
jQuery(document).ready(function($) {
    // 监听媒体模态窗口的打开事件,确保元素加载完成后再操作
    $(document).on('click', '.media-modal', function() {
        setTimeout(function() {
            // 定位到「链接到」下拉框
            const linkToSelect = $('select.link-to');
            
            // 避免重复添加自定义选项
            if (!linkToSelect.find('option[value="custom-url"]').length) {
                // 加载自定义模板并插入到下拉框末尾
                const customOption = wp.media.template('custom-link-to-option')();
                linkToSelect.append(customOption);
            }
        }, 500);
    });

    // 可选:处理自定义选项的选中逻辑(比如保存到附件元数据)
    $(document).on('change', 'select.link-to', function() {
        const selectedValue = $(this).val();
        const attachmentId = $(this).closest('.attachment-details')
                                  .find('.attachment-id').text().replace('ID: ', '');
        
        if (selectedValue === 'custom-url') {
            // 这里可以添加你的自定义逻辑,比如显示额外的URL输入框
            console.log('Custom link option selected for attachment:', attachmentId);
            // 如果需要保存到数据库,可以用AJAX请求(示例代码)
            // $.post(ajaxurl, {
            //     action: 'save_custom_attachment_link',
            //     attachment_id: attachmentId,
            //     custom_url: 'your-custom-url-here',
            //     nonce: '<?php echo wp_create_nonce("custom_link_nonce"); ?>'
            // });
        }
    });

    // 可选:AJAX处理函数(用于保存自定义链接到附件元数据)
    <?php
    add_action('wp_ajax_save_custom_attachment_link', function() {
        if (!wp_verify_nonce($_POST['nonce'], 'custom_link_nonce')) {
            wp_die('Invalid security token');
        }
        $attachmentId = intval($_POST['attachment_id']);
        update_post_meta($attachmentId, '_custom_attachment_link', sanitize_text_field($_POST['custom_url']));
        wp_die('Success');
    });
    ?>
});
</script>
<?php
});
?>

关键说明:

  • 模板定义:我们创建了一个Backbone模板tmpl-custom-link-to-option,符合WordPress媒体管理器的模板规范,用来定义自定义下拉选项。
  • 动态元素处理:媒体上传窗口是动态渲染的,所以通过监听模态窗口点击事件+延迟执行的方式,确保下拉框元素加载完成后再操作。
  • 避免重复添加:检查下拉框中是否已存在自定义选项,防止多次打开媒体窗口时重复插入。
  • 可选的保存逻辑:如果需要将选中的自定义选项关联到附件数据,可以通过AJAX请求把值保存到附件的元数据中,代码里已经给出了示例框架。

为什么原代码不生效?

你的原代码是扩展了wp.media.view.Settings.Gallery,这是专门给「插入图库」时的设置面板用的,和单个媒体文件上传时的「链接到」下拉框属于完全不同的组件,作用域不匹配,所以代码自然不会生效。

内容的提问来源于stack exchange,提问作者Pavan Yogi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 07:25:09