0

I'm using WAMP on Windows 8.1 64 bits to develop a custom theme.

Right now, I'm getting the following errors when trying to create a custom control with the WP_Customize_Image_Control class:

Fatal error: Uncaught Error: Class 'Ivana\Api\Customizer\WP_Customize_Image_Control' not found in C:\wamp64\www\ivana-5.2\wp-content\themes\ivana\inc\Api\Customizer\FeaturedCategories.php on line 115

Error: Class 'Ivana\Api\Customizer\WP_Customize_Image_Control' not found in C:\wamp64\www\ivana-5.2\wp-content\themes\ivana\inc\Api\Customizer\FeaturedCategories.php on line 115

I'm using an object oriented design, got from this starter theme: https://github.com/Alecaddd/awps

inc/Api/Customizer.php namespace Ivana\Api;

class Customizer
{
    public function register()
    {
        add_action( 'customize_register', [ $this, 'setup' ] );
    }

    public function get_classes()
    {
        return [
            Customizer\FeaturedCategories::class
        ];
    }

    public function setup( $wp_customize )
    {
        foreach ( $this->get_classes() as $class )
        {

            if ( method_exists( $class, 'register' ) )
            {
                $service = new $class;

                $service->register( $wp_customize );
            }
        }
    }
}

inc/Api/Customizer/FeaturedCategories.php

namespace Ivana\Api\Customizer;

use Ivana\Helpers;

class FeaturedCategories
{
    public function register( $wp_customize )
    {
        $this->add_panels( $wp_customize );
        $this->add_sections( $wp_customize );
        $this->add_settings( $wp_customize );
        $this->add_controls( $wp_customize );
    }

    private function add_panels( $wp_customize )
    {
        $wp_customize->add_panel( 'featured_categories', [
            'title' => 'Featured Categories',
            'description' => 'I\'m looking for a good placeholder',
            'priority' => 100
        ] );
    }

    private function add_sections( $wp_customize )
    {
        $wp_customize->add_section( 'featured_category_0', [
            'title' => 'Featured Category 1',
            'panel' => 'featured_categories',
            'description' => 'Highlight categories on your front page.'
        ] );

        $wp_customize->add_section( 'featured_category_1', [
            'title' => 'Featured Category 2',
            'panel' => 'featured_categories',
            'description' => 'Highlight categories on your front page.'
        ] );

        $wp_customize->add_section( 'featured_category_2', [
            'title' => 'Featured Category 3',
            'panel' => 'featured_categories',
            'description' => 'Highlight categories on your front page.'
        ] );
    }

    private function add_settings( $wp_customize )
    {
        $wp_customize->add_setting( 'category_slug_0', [
            'default' => '',
            'transport' => 'postMessage',
            'sanitize_callback' => 'wp_filter_nohtml_kses'
        ] );

        $wp_customize->add_setting( 'category_slug_1', [
            'default' => '',
            'transport' => 'postMessage',
            'sanitize_callback' => 'wp_filter_nohtml_kses'
        ] );

        $wp_customize->add_setting( 'category_slug_2', [
            'default' => '',
            'transport' => 'postMessage',
            'sanitize_callback' => 'wp_filter_nohtml_kses'
        ] );

        $wp_customize->add_setting( 'category_image_0', [
            'default' => '',
            'transport' => 'postMessage',
            'sanitize_callback' => 'wp_filter_nohtml_kses'
        ] );

        $wp_customize->add_setting( 'category_image_1', [
            'default' => '',
            'transport' => 'postMessage',
            'sanitize_callback' => 'wp_filter_nohtml_kses'
        ] );

        $wp_customize->add_setting( 'category_image_2', [
            'default' => '',
            'transport' => 'postMessage',
            'sanitize_callback' => 'wp_filter_nohtml_kses'
        ] );
    }

    private function add_controls( $wp_customize )
    {
        // var_dump( $wp_customize );
        // echo $wp_customize->registered_control_types;

        $wp_customize->add_control( 'category_slug_2', [
            'section' => 'featured_category_0',
            'label' => 'Category slug',
            // 'description' => 'Not the animal',
            'type' => 'text'
        ] );

        $wp_customize->add_control( 'category_slug_1', [
            'section' => 'featured_category_1',
            'label' => 'Category slug',
            // 'description' => 'Not the animal',
            'type' => 'text'
        ] );

        $wp_customize->add_control( 'category_slug_0', [
            'section' => 'featured_category_2',
            'label' => 'Category slug',
            // 'description' => 'Not the animal',
            'type' => 'text'
        ] );

        $wp_customize->add_control( new WP_Customize_Image_Control( $wp_customize, 'category_image_2', [
            'section' => 'featured_category_0',
            'label' => 'Featured Category Image 1'
        ] ) );
    }
}

Why is WordPress unable to find the WP_Customize_Image_Control class, or any of those classes, for that matter? (Also tried with color and media with no success).

If you see Customizer.php, the register method is calling the 'customize_register' action, and setup receives $wp_customize.

This works perfectly if not trying to use custom controls.

  • By directly using the Customizer API in the functions.php class, everything works as expected. – ManuAlvarado22 Jun 16 '19 at 21:53
0

Solved by adding use WP_Customize_Image_Control; to inc\Customizer\FeaturedCategories.php:

namespace Ivana\Api\Customizer;

    use WP_Customize_Image_Control;
use Ivana\Helpers;

class FeaturedCategories
{
    public function register( $wp_customize )
    {
        $this->add_panels( $wp_customize );
        $this->add_sections( $wp_customize );
        $this->add_settings( $wp_customize );
        $this->add_controls( $wp_customize );
    }

    public function add_panels( $wp_customize )
    {
        $wp_customize->add_panel( 'featured_categories', [
            'title' => 'Featured Categories',
            'description' => 'I\'m looking for a lady',
            'priority' => 100
        ] );
    }

    public function add_sections( $wp_customize )
    {
        $wp_customize->add_section( 'featured_category_0', [
            'title' => 'Featured Category 1',
            'panel' => 'featured_categories',
            'description' => 'Highlight categories on your front page.'
        ] );

        $wp_customize->add_section( 'featured_category_1', [
            'title' => 'Featured Category 2',
            'panel' => 'featured_categories',
            'description' => 'Highlight categories on your front page.'
        ] );

        $wp_customize->add_section( 'featured_category_2', [
            'title' => 'Featured Category 3',
            'panel' => 'featured_categories',
            'description' => 'Highlight categories on your front page.'
        ] );
    }

    public function add_settings( $wp_customize )
    {
        $wp_customize->add_setting( 'category_slug_0', [
            'default' => '',
            'transport' => 'postMessage',
            'sanitize_callback' => 'wp_filter_nohtml_kses'
        ] );

        $wp_customize->add_setting( 'category_slug_1', [
            'default' => '',
            'transport' => 'postMessage',
            'sanitize_callback' => 'wp_filter_nohtml_kses'
        ] );

        $wp_customize->add_setting( 'category_slug_2', [
            'default' => '',
            'transport' => 'postMessage',
            'sanitize_callback' => 'wp_filter_nohtml_kses'
        ] );

        $wp_customize->add_setting( 'category_image_0', [
            'default' => '',
            'transport' => 'postMessage',
            'sanitize_callback' => 'wp_filter_nohtml_kses'
        ] );

        $wp_customize->add_setting( 'category_image_1', [
            'default' => '',
            'transport' => 'postMessage',
            'sanitize_callback' => 'wp_filter_nohtml_kses'
        ] );

        $wp_customize->add_setting( 'category_image_2', [
            'default' => '',
            'transport' => 'postMessage',
            'sanitize_callback' => 'wp_filter_nohtml_kses'
        ] );
    }

    public function add_controls( $wp_customize )
    {
        $wp_customize->add_control( 'category_slug_2', [
            'section' => 'featured_category_0',
            'label' => 'Category slug',
            // 'description' => 'Not the animal',
            'type' => 'text'
        ] );

        $wp_customize->add_control( 'category_slug_1', [
            'section' => 'featured_category_1',
            'label' => 'Category slug',
            // 'description' => 'Not the animal',
            'type' => 'text'
        ] );

        $wp_customize->add_control( 'category_slug_0', [
            'section' => 'featured_category_2',
            'label' => 'Category slug',
            // 'description' => 'Not the animal',
            'type' => 'text'
        ] );

        $wp_customize->add_control( new WP_Customize_Image_Control( $wp_customize, 'category_image_2', [
            'section' => 'featured_category_0',
            'label' => 'Featured Category Image 1'
        ] ) );
    }
}

I found the solution by copying and pasting the Customizer classes from the source code of the mentioned theme (AWPS): https://github.com/Alecaddd/awps/blob/master/inc/Api/Customizer/Footer.php

| improve this answer | |
  • The issue wasthat you're attempting to use the WP_Customize_Image_Control class in a file that was namespaced. The WP_Customize_Image_Control class is not part of your namespace, so you either need to use use WP_Customize_Image_Control; or prepend each use of the class with \, like \WP_Customize_Image_Control. – Jacob Peattie Jun 17 '19 at 7:01
  • Thank you for clarifying the actual reason for why this works and for adding the solution of prepending the \. – ManuAlvarado22 Jun 18 '19 at 18:02

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.