php - fatal error : Class 'WP_Customize_Control' not found - WordPress

标签 php wordpress wordpress-theming

我正在构建自己的 wordpers 主题,当开始使用 WordPress Customizer 的主题选项时,我遇到了一些麻烦。

基本上我正在尝试创建一个文本区域,而我所阅读的内容我需要创建一个扩展类,然后在 WordPress 的 add_control 函数下调用它。

我已经尝试过这个并且在定制器模式下一切正常但是一旦我进入网站的任何其他部分我就会收到这个错误:

fatal error :找不到类“WP_Customize_Control”

正如我所说,它在定制器中 100% 工作,它是自己的,但包括管理员在内的任何其他页面我都会收到此消息。

这是类:

class ublxlportfolio_textarea extends WP_Customize_Control {
    public $type = 'textarea';

    public function render_content() {
        ?>
        <label>
        <span class="customize-control-title"><?php echo esc_html( $this->label ); ?></span>
        <textarea rows="5" style="width:100%;" <?php $this->link(); ?>><?php echo esc_textarea( $this->value() ); ?></textarea>
        </label>
        <?php
    }
}

我需要将它包装在条件标签中吗?如果是,那会是什么??

我做错了吗?

最佳答案

澄清@Robert 的正确答案:

WP_Customize_Control 类仅在实际使用主题定制器时加载。因此,您需要在绑定(bind)到“customize_register”操作的函数中定义您的类。

例子:

add_action( 'customize_register', 'my_customize_register' );

function my_customize_register($wp_customize) {

  //class definition must be within my_customie_register function
  class ublxlportfolio_textarea extends WP_Customize_Control { ... }

  //other stuff
}

关于php - fatal error : Class 'WP_Customize_Control' not found - WordPress,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16532397/

相关文章:

wordpress - YouTube嵌入宽度不能大于740像素吗?

php - 在 Wordpress 中,如何将自定义帖子类型的默认管理员排序顺序设置为自定义列?

php - PDO fetch() 是否在失败时抛出异常?

php - 为什么 Laravel 的重置密码功能使用数据库,而电子邮件验证使用签名路由?

php - 如果数组值存在于另一个多维数组中,如何检查和替换数组

wordpress - 当自定义帖子类型设置了 show_in_menu 时缺少分类菜单

php - 对 WooCommerce 订单中按产品类别订购的产品数量求和

javascript - Wordpress JS 引用 var 错误

wordpress - 在外部 PHP 文件中获取 Wordpress 选项

php - MySQL 表到 xml 文件 : update files when tables are updated