如何在Customizer中创建自定义控件

时间:2017-07-19 作者:Marvin

我正在尝试创建一个自定义控件,它可以选择插件中的所有联系人表单7表单。我已经做到了

<?php 
   if( !class_exists( \'Silicon_CF7_Custom_Control\' ) ) {
      class Silicon_CF7_Custom_Control extends WP_Customize_Control {
    /**
     * Returns true / false if the plugin: Contact Form 7 is activated;
     *
     * This right here disables the control for selecting a contact form IF the plugin isn\\\'t active
     *
     * @since Pixova Lite 1.15
     *
    * @return bool
     */

    public function active_callback( ) {

        if( class_exists( \'WPCF7\' ) ) {
            return true;
        } else {
            return false;
        }
    }

    public function silicon_get_cf7_forms() {
        global $wpdb;

        // no more php warnings
        $contact_forms = array();

        // check if CF7 is activated
        if ( $this->active_callback()) {
            $cf7 = $wpdb->get_results("SELECT ID, post_title FROM $wpdb->posts WHERE post_type = \'wpcf7_contact_form\' ");
            if ($cf7) {

                foreach ($cf7 as $cform) {
                    $contact_forms[$cform->ID] = $cform->post_title;
                }
            } else {
                $contact_forms[0] = __(\'No contact forms found\', \'silicon\');
            }
        }
        return $contact_forms;
    }

    public function render_content() {
        $Pixova_Lite_contact_forms = $this->silicon_get_cf7_forms();

        if ( !empty($Pixova_Lite_contact_forms ) ) { ?>
            <span class="customize-control-title"><?php echo esc_html($this->label); ?></span>
            <select <?php esc_url($this->link()); ?> style="width:100%;">
            <?php echo \'<option value="default">\'.__(\'Select your contact form\', \'silicon\').\'</option>\';
            foreach ($Pixova_Lite_contact_forms as $form_id => $form_title) {
                echo \'<option value="\' . sanitize_key( $form_id ). \'" >\' . esc_html( $form_title ). \'</option>\';
            }
            echo \'</select>\';
        }
    }
}
}

在这里需要它

require_once ( dirname( __FILE__ ) . \'/controls/contact-form.php\' );
但如果我这样称呼它

$wp_customize->add_control( new Silicon_CF7_Custom_Control(
$wp_customize,
$setting_id,
    array(
        \'label\'             => $label,
        \'section\'           => $section_id,
        \'priority\'          => $priority,
        \'type\'              => \'silicon_contact_form_7\'
    ) ) );
我得到一个错误

致命错误:在C:\\wamp\\www\\wp-5\\wp-content\\plugins\\Silicon extension\\inc\\customize\\sections\\contact中找不到类“Silicon\\u CF7\\u Custom\\u Control”。php第41行

1 个回复
SO网友:Marvin

我解决了这个问题,因为我在创建类之前调用了它

require_once ( dirname( __FILE__ ) . \'/sections/contact.php\' );
require_once ( dirname( __FILE__ ) . \'/controls/contact-form.php\' );

结束

相关推荐

Theme styling for plugins

我有一个插件,它有自己的CSS,用于在使用相关短代码时生成的内容。我正在尝试创建一个主题来重新设置我网站前端的样式,但由于这个插件有自己的CSS,我如何在新主题中修改它?