Wp 定制器自定义控件类 - 从自定义参数获取值到类内的排队函数

问题描述 投票:0回答:1

我正在 wp 定制器中使用自定义控件。

获得此控件的自定义参数( foo )并尝试获取其值作为排队目录的一部分。

因此,自定义参数 foo 在函数 render_content() 中正确显示;

当我尝试在函数 foo_enqueue(); 中做同样的事情时(是的,我知道它与行动有关)它破坏了。 之后,即使在渲染内容中 - foo 参数输出也会消失。 类看起来像这样:

class Foo_Control extends WP_Customize_Control {

 public $type = 'foo_control';  
    
 public $foo; // declare foo argument


 public function __construct( $manager, $id, $args = array(), $options = array() ) {
    
    parent::__construct( $manager, $id, $args );
        
    add_action( 'customize_controls_enqueue_scripts', array( $this, 'foo_enqueue' ), 15 );                      
 }
 
 public function foo_enqueue() {
                        
    wp_enqueue_script( 'foo-script', get_template_directory_uri() . '/js/foo-script.js', array( 'jquery' ), '1.0', true );
    
    // $this->foo = $foo_dir;   
    // wp_enqueue_script( 'foo-script', get_template_directory_uri() . '/js/'.$foo_dir.'.js', array( 'jquery' ), '1.0', true );

    // js file is not loading with $foo_dir and brokes display foo at render_content after apply                    
 }

 public function render_content() {

    echo $this->label;          // Foo label
    echo $this->description;    // Foo desc

    echo $this->foo;  // foo-script                         
 }
}

将控件添加到 $wp_customize 的设置如下所示:

$wp_customize->add_setting( 'my_foo_control', array(
    'type' => 'theme_mod',
));

$wp_customize->add_control( new Foo_Control( $wp_customize,'my_foo_control', array(
    'section'       => 'homepage',
    'label'         => 'Foo label',
    'description'   => 'Foo desc',       
    'foo'           => 'foo-script', // custom argument

) ));
  • 尝试过$this->foo,

  • 尝试使用函数 a 、函数 b($a)

  • 尝试使用函数 a() { b(); }

  • 尝试使用函数 a { $this->b();}

  • 尝试使用函数 a { self::b(); }

还是做不到。

php wordpress function arguments customizer
1个回答
0
投票

该问题与

$foo
类中如何处理
Foo_Control
参数有关。当我们向
Customizer control
添加自定义参数时,这些参数应传递给构造函数并存储为类属性,以便我们可以进行给定的更改以确认
custom argument foo
得到正确利用并且不会与入队函数冲突。

您可以尝试更换吗

public function __construct( $manager, $id, $args = array(), $options = array() ) {
    
    parent::__construct( $manager, $id, $args );
        
    add_action( 'customize_controls_enqueue_scripts', array( $this, 'foo_enqueue' ), 15 );                      
 }

使用给定的代码

public function __construct( $manager, $id, $args = array(), $options = array() ) {
    
    parent::__construct( $manager, $id, $args );

    if ( isset( $args['foo'] ) ) {
        $this->foo = $args['foo']; // Here we are assigning the custom argument.
    }
        
    add_action( 'customize_controls_enqueue_scripts', array( $this, 'foo_enqueue' ), 15 );                      
 }

另外请尝试替换给定的代码

public function foo_enqueue() {
                        
    wp_enqueue_script( 'foo-script', get_template_directory_uri() . '/js/foo-script.js', array( 'jquery' ), '1.0', true );
    
    // $this->foo = $foo_dir;   
    // wp_enqueue_script( 'foo-script', get_template_directory_uri() . '/js/'.$foo_dir.'.js', array( 'jquery' ), '1.0', true );

    // js file is not loading with $foo_dir and brokes display foo at render_content after apply                    
 }

与给定的那个

    public function foo_enqueue() {
        // Ensure that the foo property is set before using it
        if ( ! empty( $this->foo ) ) {
            wp_enqueue_script( 'foo-script', get_template_directory_uri() . '/js/' . $this->foo . '.js', array( 'jquery' ), '1.0', true );
        }
    }

© www.soinside.com 2019 - 2024. All rights reserved.