扩展WP_Customize_Image_Control类以更改其非框架$BUTTON_Labels

时间:2015-07-02 作者:5ervant - techintel.github.io

我试了很多,只是为了改变\'default\' a的按钮标签Customize_Image_Control, 包括复制its class source 并重新命名(*请检查the non-revised edition for this),并扩展类并按如下方式使用:

if (class_exists(\'WP_Customize_Image_Control\')) {
  class WP_Customize_Custom_Control extends WP_Customize_Image_Control {
    public function __construct( $manager, $id, $args = array() ) {
      parent::__construct( $manager, $id, $args );

      $this->button_labels = array(
        \'select\'       => __( \'Custom Select Image\' ),
        \'change\'       => __( \'Custom Change Image\' ),
        \'remove\'       => __( \'Custom Remove\' ),
        \'default\'      => __( \'Custom Default\' ),
        \'placeholder\'  => __( \'Custom No image selected\' ),
        \'frame_title\'  => __( \'Custom Select Image\' ),
        \'frame_button\' => __( \'Custom Choose Image\' ),
      );
    }
  }
}
add_action( \'customize_register\', \'custom_customize_register\' );
function custom_customize_register( $wp_customize ) {
  $wp_customize->register_control_type( \'WP_Customize_Custom_Control\' );

  // More customize code here...

  $wp_customize->add_control(
    new WP_Customize_Custom_Control(
      $wp_customize,
      \'custom\',
      array(
        \'label\'       => __( \'Label\' ),
        \'description\' => __( \'Description here..\' ),
        \'section\'     => \'section_id\',
        \'settings\'    => \'setting_id\',
      )
    )
  );
}
我发现只有分配给按钮标签的值\'frame_title\'\'frame_button\' 正在显示,其他人没有。我通过提供唯一的public $type 像这样:

if (class_exists(\'WP_Customize_Image_Control\')) {
  class WP_Customize_Custom_Control extends WP_Customize_Image_Control {

    public $type = \'custom\'; // THIS IS THE NEW TYPE

    public function __construct( $manager, $id, $args = array() ) {
      // ...
按钮标签肯定会改变,但按钮不再可点击,它们的样式也消失了,似乎它们的功能都消失了。有人知道怎么解决这个问题吗?

其他Q&;中发布的与此相关的其他问题;WordPress网站:

1 个回复
最合适的回答,由SO网友:5ervant - techintel.github.io 整理而成

最后,我找到了如何更改非帧$button_labelsWP_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\' ),
);

结束

相关推荐

Wp-login.php只是刷新表单域

我最近将一个客户博客从一个服务器移动到了另一个服务器。从表面上看,一切似乎都很好——博客正在运行,你可以查看没有问题的条目。我们遇到的问题是管理员登录页面。当您输入用户名和密码时,它只会刷新页面。不会显示任何错误消息或提示。我遵循了本页上的提示:http://codex.wordpress.org/Login_TroubleCookies are enabled, tried via several browsers Plugins folder has been renamed to plugi