首先说明,花了一整天来研究这个东西,虽然各种失败,但是依然告诉自己要冷静,冷静!
下午找了两个同事帮忙,尝试用jquery自动加载点击事件,但怎样都不行。
说一下啊,wp-includes下的functions为后台自动加载函数
因为前侧到ajax,虽然拿到了标签数据,但是点击事件会全部失效。
各种折腾啊,下班又折腾了一个半小时,几乎是背水一战,搞定!
火狐浏览器F12查看了加载的js文件,又从对应js文件中得到js代码,找到了灵感。
直接放代码吧,修改在wp-admin/includes/meta-boxes.php
主要是增加了JS文件,PHP代码没有做任何修改!
<?php if ( $user_can_assign_terms ) : ?>
<p class="hide-if-no-js"><button type="button" class="button-link tagcloud-link" id="link-<?php echo $tax_name; ?>" aria-expanded="true"><?php echo $taxonomy->labels->all_items; ?></button></p>
<?php endif; ?>
<script type="text/javascript">
jQuery(document).ready(function($) {
var id = $('.tagcloud-link').attr( 'id' );
var tax = id.substr( id.indexOf('-') + 1 );
$.post( ajaxurl, { 'action': 'get-tagcloud', 'tax': 'video_tag' }, function( r, stat ) {
if ( 0 === r || 'success' != stat ) {
return;
}
r = $( '<p id="tagcloud-' + tax + '" class="the-tagcloud">' + r + '</p>' );
$( 'a', r ).click( function() {
tagBox.userAction = 'add';
tagBox.flushTags( $( '#video_tag' ), this );
return false;
});
$( '#' + id ).after( r );
});
// Update button state, remove previous click event and attach a new one to toggle the cloud.
$('.tagcloud-link')
.attr( 'aria-expanded', 'true' )
.unbind()
.click( function() {
$( this )
.attr( 'aria-expanded', 'false' === $('.tagcloud-link').attr( 'aria-expanded' ) ? 'true' : 'false' )
.siblings( '.the-tagcloud' ).toggle();
});
});
</script>
最新使用方法,在 wp-admin\includes\meta-boxes.php 约 551 行在 endif 和 <?php 之间增加点击事件
<?php endif; ?>
<script type="text/javascript">
jQuery(document).ready(function($) {
//模拟点击事件
setTimeout(function() {
$('#link-post_tag').click();
},2000)
});
</script>
<?php