最后,我找到了如何更改非帧$button_labels
的WP_Customize_Image_Control
通过扩展其类,而不是重新分配新$type
并实施enqueue()
函数将纯JavaScript代码排队,以操作非框架标签,如下面所示,更改动态\'default\'
标签依据.addEventListener("click", handler)
其动态.button
:
class WP_Customize_Custom_Control extends WP_Customize_Image_Control {
public function enqueue() {
// There\'s no parent::enqueue() function.
?>
<script>
// Will execute if the specific element is ready, in pure JavaScript way.
var elemReadyCheckInterval = setInterval(function() {
if (document.getElementById("customize-control-custom")) {
clearInterval(elemReadyCheckInterval);
var controlCustom = document.getElementById("customize-control-custom");
var controlButton = controlCustom.getElementsByClassName("button")[0];
controlButton.addEventListener("click", function () {
buttonClick(controlCustom, controlButton);
});
}
}, 10);
function buttonClick(custom, button) {
var previousLabel = button.innerHTML;
var buttonClickFunc = arguments.callee;
var elemReadyCheckInterval = setInterval(function() {
// Remove the EventListener `buttonClick(custom, button)` from the `button`.
button.removeEventListener(\'click\', buttonClickFunc);
// Assign the new element to the `button` because it\'s dynamically changing!
button = custom.getElementsByClassName("button")[0];
// Compare the `previousLabel` to the current `button` label.
if (previousLabel !== button.innerHTML) {
clearInterval(elemReadyCheckInterval);
// If the dynamic label is \'Default\'.
if (button.innerHTML === \'Default\')
button.innerHTML = \'Custom Default\';
button.addEventListener("click", function () {
buttonClick(custom, button);
});
}
}, 10);
}
</script>
<?php
}
}
我没有发现通过重写其他函数来更改非框架标签,例如
content_template()
和
render_content()
, 我很难用这些方法实现,因为如果你设置一个新的
$type
但是设置一个新类型会使所有按钮功能消失,这就是为什么我没有重新分配一个新的
$type
.
非框架不变$button_labels
这样做,就是一个bug!
$this->button_labels = array(
\'select\' => __( \'Custom Select Image\' ),
\'change\' => __( \'Custom Change Image\' ),
\'remove\' => __( \'Custom Remove\' ),
\'default\' => __( \'Custom Default\' ),
\'placeholder\' => __( \'Custom No image selected\' ),
);