【问题标题】:Wordpress customizer custom control transport postMessage not workingWordpress 定制器自定义控件传输 postMessage 不起作用
【发布时间】:2016-01-11 14:06:22
【问题描述】:

我正在构建一个WordPress 主题,需要在定制器中添加自定义controls,输入字段会显示,但是当我更改输入值时没有任何反应。

这是我的代码。

class WP_Customize_Custom_Control extends WP_Customize_Control {
  public $type = 'custom_control';

  function render_content(){
  }
  public function content_template() {
    ?>
    <input type="text" id="custom_control" name="custom_control" />
    <?php
  }
}

function company_customize_register( $wp_customize ){
  $wp_customize->register_control_type( 'WP_Customize_Custom_Control' );
  $wp_customize->add_setting('custom_smthing', array( 'default' => get_theme_mod( "custom_smthing" ), 'transport' =>'postMessage' ) );
  $wp_customize->add_control(new WP_Customize_Custom_Control($wp_customize,'custom_smthing',
    array(
      'label'      => __( 'Custom Control', 'company' ),
      'section'    => 'body_backgrounds',
      'settings'   => 'custom_smthing',
     )
   )
  );
}
add_action( 'customize_register', 'company_customize_register' );

js 是

( function( $ ) {
  console.log("test1");
  wp.customize( 'custom_smthing', function( value ) {
    console.log("test2");
    value.bind( function( to ) {
      console.log("test3");
    } );
  });
})( jQuery );

test1 和 test2 正在工作,但 test3 永远不会触发。

【问题讨论】:

    标签: javascript php jquery wordpress wordpress-theming


    【解决方案1】:

    也许我的答案不是你要找的,但content_template 是针对下划线 JS 模板的。我不完全确定您的回答是否需要它。但是您可以找到更多信息here

    如果您删除 register_control_type 行并将您的输入重新定位到 render_content 方法中,它将起到作用。

    你的 PHP 部分会是这样的

    class WP_Customize_Custom_Control extends WP_Customize_Control {
      public $type = 'custom_control';
    
      public function render_content() {
        ?>
        <label>
            <span class="customize-control-title"><?php echo esc_html( $this->label ); ?></span>
                <input class="custom_control" id="custom_control" type="text" <?php $this->link(); ?> value="<?php echo esc_attr( $this->value() ); ?>">
            </label>
        <?php
      }
    }
    
    function company_customize_register( $wp_customize ){
    
      // $wp_customize->register_control_type( 'WP_Customize_Custom_Control' ); - remove this line
    
      $wp_customize->add_setting('custom_smthing', array( 'default' => get_theme_mod( "custom_smthing" ), 'transport' =>'postMessage' ) );
    
      $wp_customize->add_control(new WP_Customize_Custom_Control($wp_customize,'custom_smthing',
        array(
          'label'      => __( 'Custom Control', 'company' ),
          'section'    => 'body_backgrounds',
          'settings'   => 'custom_smthing',
         )
       )
      );
    }
    add_action( 'customize_register', 'company_customize_register' );
    

    【讨论】:

      【解决方案2】:

      我修改了您的代码,发现您的代码在许多情况下或按照最新的 WP 版本都不起作用。

      您没有在代码中提及body_backgrounds 部分。所以我假设 Body Backgrounds 是您的自定义部分,因为我在 WordPress 中没有找到默认的 body_backgrounds 部分。

      所以我添加了一个部分。

      $wp_customize->add_section( 'body_backgrounds' , array(
          'title'      => 'Body Backgrounds',
          'priority'   => 20,
      ) );
      

      WP_Customize_Control 仅在实际使用主题定制器时加载。因此,您需要在函数“company_customize_register”中定义您的类。否则会给PHP Fatal error: Class 'WP_Customize_Control' not found

      您没有提到如何将 js 排入队列,但最好添加依赖项 jquerycustomize-preview

      function customizer_live_preview() {
          wp_enqueue_script(
              'theme-customizer',
              get_template_directory_uri() . '/assets/js/theme-customizer.js',
              array( 'jquery', 'customize-preview' ),
              '1.0.0',
              true
          );
      }
      add_action( 'customize_preview_init', 'customizer_live_preview' );
      

      您需要将data-customize-setting-link 传递给您的输入以绑定您的值。

      <input type="text" id="custom_smthing" name="custom_smthing" data-customize-setting-link="custom_smthing" />
      

      PHP

      function company_customize_register( $wp_customize ){
      
          class WP_Customize_Custom_Control extends WP_Customize_Control {
              public $type = 'custom_control';
      
              function render_content(){ }
              
              public function content_template() {?>
                  <input type="text" id="custom_smthing" name="custom_smthing" data-customize-setting-link="custom_smthing" />
              <?php }
          }
      
          $wp_customize->register_control_type( 'WP_Customize_Custom_Control' );
          $wp_customize->add_section( 'body_backgrounds' , array(
              'title'      => 'Body Backgrounds',
              'priority'   => 20,
          ) );
          $wp_customize->add_setting('custom_smthing', array( 'default' => get_theme_mod( "custom_smthing" ) ? get_theme_mod( "custom_smthing" ) : '', 'transport' =>'postMessage' ) );
          $wp_customize->add_control(new WP_Customize_Custom_Control($wp_customize,'custom_smthing',
                  array(
                      'label'      => __( 'Custom Control', 'company' ),
                      'section'    => 'body_backgrounds',
                      'settings'   => 'custom_smthing',
                  )
              )
          );
      }
      add_action( 'customize_register', 'company_customize_register' );
      
      function customizer_live_preview() {
          wp_enqueue_script(
              'theme-customizer',
              get_template_directory_uri() . '/assets/js/theme-customizer.js',
              array( 'jquery', 'customize-preview' ),
              '1.0.0',
              true
          );
      }
      add_action( 'customize_preview_init', 'customizer_live_preview' );
      

      “theme-customizer.js”

      (function( $ ) {
          "use strict";
      
          wp.customize('custom_smthing', function(value) {
              value.bind(function(value) {
                  console.log(value);
              });
          });
      
      })( jQuery );
      

      经过测试并且有效。

      【讨论】:

        【解决方案3】:

        test3 只会在custom_smthing 的值发生变化时触发。

        【讨论】:

        • 我自己查过了。即使值发生变化也不会触发
        【解决方案4】:

        我已经解决了这个问题。
        如果前两个日志被触发,那么问题没有任何问题
        那里没有错误,一切都按照官方 wordpress 主题开发手册中的说明进行。
        问题出在 index.php
        wp_head();wp_footer(); 必须存在
        我删除了它们 = 只触发了前两个日志
        我把它们放在上面 = 所有三个日志都被解雇了
        让我生气的是为什么 wordpress 官方手册中没有提到这一点。
        在整本手册的最后 是
        但它并没有说它对很多事情都是必不可少的,包括这个。

        【讨论】:

        • 那么,您在创建主题时检查了"Create an index.php file" in the WordPress theme handbook 吗?请不要期望每个文档都包含类似“记住您的主题应该调用 wp_head()wp_footer() 以便正确加载脚本和样式”之类的内容,我们的工作是找到正确的资源(文档,教程等)。 (顺便说一句,我没有 -1 你的答案......但你应该考虑改进它。)
        • @SallyCJ 实际上是一个公平的期望。我在做一个极简主义的例子,不想有页眉和页脚。我怎么知道他们负责加载样式和脚本。我认为它们只是 WP 的另一种多余的方式,可以使事情变得更复杂和外部化索引部分。直到现在我才知道 wp_head() 和 get_header() 之间的区别,我认为它应该被覆盖和解释。如果你密切注意这不是我的问题。我刚刚在它上面放了一个赏金。也许你只是难过我没有给你。那!是什么不公平。
        • 请放心,我很清楚您只是悬赏这个问题。是的,(主题)手册并没有涵盖所有内容,因此如果缺少重要内容,请随时联系docs team 或向docs team 捐款。请您编辑您的答案,也许包括“我正在做一个极简主义的例子,不想有页眉和页脚。”部分代码并解释发生了什么以及您是如何解决问题的。
        • 实际上,如果我是你,我会问是否可以删除页眉和页脚,以及删除它们的正确方法。此外,如果明确向我提供了赏金,但最终却将其授予其他人,我会感到难过。否则,我会很好......
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多