Using WordPress ‘customize_dynamic_partial_class’ PHP filter

The customize_dynamic_partial_class WordPress PHP filter allows you to construct partials with a custom WP_Customize_Partial subclass for non-statically created partials.

Dynamic partials are used to render parts of a page that change frequently, such as the contents of a sidebar or a list of recent posts. By using this filter, developers can modify the way that these partials are rendered, allowing for greater flexibility and control over the appearance and functionality of their WordPress sites.

Usage

add_filter('customize_dynamic_partial_class', 'your_custom_function', 10, 3);

function your_custom_function($partial_class, $partial_id, $partial_args) {
    // your custom code here
    return $partial_class;
}

Parameters

  • $partial_class: (string) The WP_Customize_Partial or a subclass.
  • $partial_id: (string) The ID for the dynamic partial.
  • $partial_args: (array) The arguments to the WP_Customize_Partial constructor.

More information

See WordPress Developer Resources: customize_dynamic_partial_class

Examples

Customizing a menu partial

Customize the menu partial by creating a new subclass and modifying the render method.

class My_Custom_Menu_Partial extends WP_Customize_Partial {
    public function render() {
        // custom code for rendering the menu
    }
}

add_filter('customize_dynamic_partial_class', 'my_custom_menu_partial', 10, 3);

function my_custom_menu_partial($partial_class, $partial_id, $partial_args) {
    if ('nav_menu_instance' === $partial_id) {
        $partial_class = 'My_Custom_Menu_Partial';
    }
    return $partial_class;
}

Customizing a widget partial

Customize the widget partial by creating a new subclass and modifying the render method.

class My_Custom_Widget_Partial extends WP_Customize_Partial {
    public function render() {
        // custom code for rendering the widget
    }
}

add_filter('customize_dynamic_partial_class', 'my_custom_widget_partial', 10, 3);

function my_custom_widget_partial($partial_class, $partial_id, $partial_args) {
    if ('widget_instance' === $partial_id) {
        $partial_class = 'My_Custom_Widget_Partial';
    }
    return $partial_class;
}

Customize the footer partial by creating a new subclass and modifying the render method.

class My_Custom_Footer_Partial extends WP_Customize_Partial {
    public function render() {
        // custom code for rendering the footer
    }
}

add_filter('customize_dynamic_partial_class', 'my_custom_footer_partial', 10, 3);

function my_custom_footer_partial($partial_class, $partial_id, $partial_args) {
    if ('footer_instance' === $partial_id) {
        $partial_class = 'My_Custom_Footer_Partial';
    }
    return $partial_class;
}

Customizing a header partial

Customize the header partial by creating a new subclass and modifying the render method.

class My_Custom_Header_Partial extends WP_Customize_Partial {
    public function render() {
        // custom code for rendering the header
    }
}

add_filter('customize_dynamic_partial_class', 'my_custom_header_partial', 10, 3);

function my_custom_header_partial($partial_class, $partial_id, $partial_args) {
    if ('header_instance' === $partial_id) {
        $partial_class = 'My_Custom_Header_Partial';
    }
    return $partial_class;
}

Customizing a post partial

Customize the post partial by creating a new subclass and modifying the render method.

class My_Custom_Post_Partial extends WP_Customize_Partial {
    public function render() {
        // custom code for rendering the post
    }
}

add_filter('customize_dynamic_partial_class', 'my_custom_post_partial', 10, 3);

function my_custom_post_partial($partial_class, $partial_id, $partial_args) {
    if ('post_instance' === $partial_id) {
        $partial_class = 'My_Custom_Post_Partial';
    }
    return $partial_class;
}