javascript - Wordpress - 向编辑器添加自定义媒体按钮不起作用。事件监听器没有捕捉到点击
问题描述
嗨,我正在尝试为用户做一个按钮来插入一个短代码模板,例如[commit_shortcode commit_ids=""]
. 为了实现这一点,我在按钮旁边创建了一个Add Media
按钮,名为Add Commit Lists
.
但显然点击它不会触发事件来捕获和执行 js 脚本。
我如何为其添加按钮和 js 脚本:
add_action('media_buttons', function() {
echo '<button type="button" id="insert-my-commits" class="button insert-my-commits">Add Commit List</button>';
}, 100);
add_action( 'wp_enqueue_media', function (){
wp_enqueue_script('media_button',
OWD_VCS_MANAGER_PLUGIN_URL . 'assets/js/addCommitListButton.js',
['jquery'],
'1.0.0',
true);
}, 100);
还有我用于处理向编辑器添加短代码的 js 脚本:
jQuery( function( $ ) {
$( '.insert-my-commits' ).click( openMediaWindow );
console.log('It is working here');
acf.addAction( 'load', function() {
$( '.insert-my-commits' ).on( 'click', function() {
console.log('It is NEVER working here');
openMediaWindow();
} );
} );
function openMediaWindow() {
wp.media.editor.insert('[commit_shortcode commit_ids=""]');
}
} );
看起来有些东西正在捕获click
事件并阻止我的脚本执行。如果有人有想法,我将非常感谢。我也在使用本教程:https ://www.sitepoint.com/adding-a-media-button-to-the-content-editor/
解决方案
推荐阅读
- python - 第一个 if 条件始终为真
- excel - Excel幂查询分组方式:通过Z分数公式(((列表值(s)-平均值)/标准偏差)<3)去除异常值后获取列表值的最大值
- r - 如何检查 Rcpp List 对象中是否存在名称?
- python - Tk 未定义
- svg - 使用颜色的类名从 Illustrator 导出 SVG
- python - 添加测量两个任务之间时间差的列
- webpack - 构建时如何将 HTML 导入 HTML?
- sql - 根据 SQL 中的条件创建另一列
- ffmpeg - 如何将分辨率 FFMPEG 实时流式传输到 youtube
- php - 一个网站和移动应用程序的管理页面