0

Currently I'm developing WordPress Theme.Within my theme I'm using Customizer API.I added some controls,setting,sections It works.

Also, I created new PHP file called class-customizer-control.php.Inside this file I'll put all custom controls, so I created one custom control (textarea):

<?php
include_once ABSPATH . 'wp-includes/class-wp-customize-control.php';
/**
*    Add Custom controls to Customizer API
*/
function bedakb_gamer_customcontrols() {
    class Custom_Textarea_Control 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
        }
    }
}
add_action('customize_register', 'bedakb_gamer_customcontrols');
?>

Next, I added control, inside customization file, that using Textarea custom control:

$wp_customize->add_control( new Custom_Textarea_Control($wp_customize, 'bedakb_ad728', array(
    'label'        => __('Adsense 728x90', 'bedakb_gamer'),
    'section'    => 'bedakb_adsense',
    'priority'    => 10,
)));

But when I try to open Customization in Wp-admin, I got error:

Fatal error: Class 'Custom_Textarea_Control' not found in C:\xampp\htdocs\wp2\wp-content\themes\bedakb_gamer\inc\customizer.php on line 35

  • Why are you including files from wp-includes? You should include class-customizer-control.php from your theme's functions.php. – RRikesh Nov 15 '14 at 15:32
  • class-customizer-control.php is already included in functions.php file.Same problem happened when I remove line that include wp-customize-control.php. – Belmin Bedak Nov 15 '14 at 16:56
  • 1
    Why are you defining a class inside a function? It makes little sense – Tom J Nowell Nov 15 '14 at 21:00
1

I'm not sure if there are different approaches for this, but this is a pattern I've implemented successfully in several places.

In my approach, I am including the "add_control" inside the function that contains the custom class. The $wp_customize object gets passed in to the main function.

Also, I'm not sure why you are trying to include class-wp-customize-control.php.

Here's my guess for what you need, assuming you've created the "bedakb_adsense" section elsewhere. Just put the following in a php file and call it from your functions.php

<?php

function bedakb_gamer_customcontrols($wp_customize) {

    class Custom_Textarea_Control 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
        }
    }

    $wp_customize->add_setting( 'bedakb_ad728', array(
        'capability'  => 'edit_theme_options'
    ) );

    $wp_customize->add_control( new Custom_Textarea_Control($wp_customize, 'bedakb_ad728', array(
        'label'        => __('Adsense 728x90', 'bedakb_gamer'),
        'section'    => 'bedakb_adsense',
        'priority'    => 10,
    )));
}

add_action('customize_register', 'bedakb_gamer_customcontrols', 99);
0

There were 2 problems.

First: I didn't add setting for custom control

$wp_customize->add_setting( 'bedakb_ad728', array(
        'capability'  => 'edit_theme_options'
));

Second: Into functions.php file customizer.php was included before class-customizer-control.php

require get_template_directory() . '/inc/class-customizer-control.php';
require get_template_directory() . '/inc/customizer.php';

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.