谢谢userabuser
作为领先者。我相信扩展后的代码将更接近:
add_action( \'post_submitbox_misc_actions\', \'custom_button\', 10, 1 );
function custom_button( $post ) {
// Show only for published pages.
if ( ! $post
|| \'publish\' !== $post->post_status
|| \'page\' !== $post->post_type ) {
return;
}
$html = \'<div id="major-publishing-actions" style="overflow:hidden">\';
$html .= \'<div id="publishing-action">\';
$html .= \'<input type="submit" accesskey="p" tabindex="5" value="Custom Button" class="button-primary" id="submitbox-custom-button" name="publish">\';
// Post meta example.
$user = get_user_by( \'ID\', get_post_meta( $post->ID, \'_edit_last\', true ) );
if ( $user ) {
$html .= \'<div>\' . ucfirst( $post->post_type ) . \' \' . $post->ID . \' last edited by \' . $user->display_name . \'</div>\';
}
$html .= \'</div>\';
$html .= \'</div>\';
// Customize button click event.
$html .= "<script>window.addEventListener(\'load\', function(){ jQuery(\'input#submitbox-custom-button\').click(function(event){ event.preventDefault(); alert(\'You\\\'re editing " . $post->post_type . " " . $post->ID . "\'); return true; }); }); </script>";
echo $html;
}
有几种方法可以实现服务层,因此今天没有必要解决这个5年多的老问题。但这对未来的任何人来说都应该是一个很好的复制/粘贴开始,而且会开箱即用。
显然,最好将JS包含在单独加载的文件中,但如果您想在可读性更强的JS中进行黑客攻击,可以使用ob_start
和ob_get_clean
.
ob_start(); ?>
<script>window.addEventListener(\'load\', function() {
jQuery(\'input#submitbox-custom-button\').click(function(event) {
event.preventDefault();
alert(\'You\\\'re editing <?php echo $post->post_type . " " . $post->ID; ?>\');
return true;
});
});
</script>
<?php $html .= ob_get_clean();