I want you add this to the tempalte please with review and debug them " functions.php "// Register Customizer Sections for Template Builder add_action('customize_register', 'register_template_builder'); function register_template_builder($wp_customi

WPTurbo » Snippets » I want you add this to the tempalte please with review and debug them " functions.php "// Register Customizer Sections for Template Builder add_action('customize_register', 'register_template_builder'); function register_template_builder($wp_customi
0

Created with:

Visibility: 

public

Creator: Gamal

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project
					<?php

// Register Customizer Sections for Template Builder
add_action('customize_register', 'wpturbo_register_template_builder');

function wpturbo_register_template_builder($wp_customize) {

    // Create Section
    $wp_customize->add_section('template_builder', [
        'title' => __('Template Builder', 'wpturbo'),
        'priority' => 30,
    ]);

    // Add Page Builder Setting
    $wp_customize->add_setting('page_builder', [
        'default' => '',
        'sanitize_callback' => 'wpturbo_sanitize_json',
    ]);

    // Add Header Builder Setting
    $wp_customize->add_setting('header_builder', [
        'default' => '',
        'sanitize_callback' => 'wpturbo_sanitize_json',
    ]);

    // Add Footer Builder Setting
    $wp_customize->add_setting('footer_builder', [
        'default' => '',
        'sanitize_callback' => 'wpturbo_sanitize_json',
    ]);
}

// Sanitization Callback
function wpturbo_sanitize_json($input) {
    return sanitize_text_field($input);
}

// Output Template Parts
add_action('wp_head', 'wpturbo_output_template_parts');

function wpturbo_output_template_parts() {
    // Get saved settings
    $header_builder = get_theme_mod('header_builder', '');
    $page_builder = get_theme_mod('page_builder', '');
    $footer_builder = get_theme_mod('footer_builder', '');
    
    // Add your drag-and-drop logic here
    // This is a simplified placeholder - you'll need JS for drag-and-drop
}

// Register Swatch Attributes
add_filter('product_attributes_type_selector', 'wpturbo_register_swatch_attributes');

function wpturbo_register_swatch_attributes($types) {
    $types['color'] = __('Color Swatch', 'wpturbo');
    $types['image'] = __('Image Swatch', 'wpturbo');
    return $types;
}

// Display Swatches in Product Listings
add_filter('woocommerce_get_item_data', 'wpturbo_display_product_swatches', 10, 2);

function wpturbo_display_product_swatches($item_data, $cart_item) {
    $product = $cart_item['data'];
    
    // Get color swatches
    $color_attributes = array_filter($product->get_attributes(), function($attr) {
        return $attr->get_type() === 'color';
    });

    // Get image swatches
    $image_attributes = array_filter($product->get_attributes(), function($attr) {
        return $attr->get_type() === 'image';
    });

    // Add color swatches to cart item data
    if (!empty($color_attributes)) {
        foreach ($color_attributes as $attribute) {
            $terms = wc_get_product_terms($product->get_id(), $attribute->get_name());
            foreach ($terms as $term) {
                $item_data[] = [
                    'key' => $attribute->get_name(),
                    'value' => $term->name,
                    'display' => '',
                ];
            }
        }
    }

    // Add image swatches to cart item data
    if (!empty($image_attributes)) {
        foreach ($image_attributes as $attribute) {
            $terms = wc_get_product_terms($product->get_id(), $attribute->get_name());
            foreach ($terms as $term) {
                $item_data[] = [
                    'key' => $attribute->get_name(),
                    'value' => wp_get_attachment_image($term->term_id, 'thumbnail'),
                    'display' => '',
                ];
            }
        }
    }

    return $item_data;
}

// Override Product Template to Show Swatches
add_action('woocommerce_single_product_summary', 'wpturbo_display_swatch_beside_title', 6);

function wpturbo_display_swatch_beside_title() {
    global $product;
    
    $attributes = $product->get_attributes();
    foreach ($attributes as $attribute) {
        if ($attribute->get_type() === 'color' || $attribute->get_type() === 'image') {
            echo '<div class="swatch-container">';
            $terms = wc_get_product_terms($product->get_id(), $attribute->get_name());
            foreach ($terms as $term) {
                if ($attribute->get_type() === 'color') {
                    echo '<span class="color-swatch" style="background-color: ' . 
                         esc_attr($term->description) . '"></span>';
                } elseif ($attribute->get_type() === 'image') {
                    $image = wp_get_attachment_image($term->term_id, 'thumbnail');
                    echo '<span class="image-swatch">' . $image . '</span>';
                }
            }
            echo '</div>';
        }
    }
}

// Add Dynamic Content Hooks
add_action('woocommerce_before_shop_loop', 'wpturbo_add_dynamic_content_hooks');

function wpturbo_add_dynamic_content_hooks() {
    // Add banner before product loop
    do_action('wpturbo_before_shop_loop_banner');
    
    // Add CTA after product loop
    do_action('wpturbo_after_shop_loop_cta');
}

// Register Customizer Controls for Dynamic Content
add_action('customize_register', 'wpturbo_register_dynamic_content_controls');

function wpturbo_register_dynamic_content_controls($wp_customize) {
    // Add banner content setting
    $wp_customize->add_setting('shop_loop_banner', [
        'default' => '',
        'sanitize_callback' => 'wp_kses_post',
    ]);

    // Add CTA content setting
    $wp_customize->add_setting('shop_loop_cta', [
        'default' => '',
        'sanitize_callback' => 'wp_kses_post',
    ]);

    // Add controls (use WP_Customize_Control subclasses)
}

// Output Dynamic Content
add_action('wpturbo_before_shop_loop_banner', 'wpturbo_output_shop_banner');

function wpturbo_output_shop_banner() {
    echo get_theme_mod('shop_loop_banner', '');
}

add_action('wpturbo_after_shop_loop_cta', 'wpturbo_output_shop_cta');

function wpturbo_output_shop_cta() {
    echo get_theme_mod('shop_loop_cta', '');
}
// Add filter controls to product archive
add_action('woocommerce_before_shop_loop', 'add_custom_filters');
function add_custom_filters() {
    ?>
    <form method="get" class="custom-filters">
        <!-- Price Filter -->
        <div class="filter-group">
            <label>Price Range</label>
            <input type="number" name="min_price" placeholder="Min" value="<?php echo $_GET['min_price'] ?? ''; ?>">
            <input type="number" name="max_price" placeholder="Max" value="<?php echo $_GET['max_price'] ?? ''; ?>">
        </div>

        <!-- Color Filter -->
        <div class="filter-group">
            <label>Color</label>
            <?php
            $colors = get_terms('pa_color');
            foreach ($colors as $color) {
                echo '<label><input type="checkbox" name="color[]" value="'.esc_attr($color->slug).'"> '.esc_html($color->name).'</label>';
            }
            ?>
        </div>

        <!-- Size Filter -->
        <div class="filter-group">
            <label>Size</label>
            <?php
            $sizes = get_terms('pa_size');
            foreach ($sizes as $size) {
                echo '<label><input type="checkbox" name="size[]" value="'.esc_attr($size->slug).'"> '.esc_html($size->name).'</label>';
            }
            ?>
        </div>

        <!-- Category Filter -->
        <div class="filter-group">
            <label>Category</label>
            <?php
            $categories = get_terms('product_cat');
            foreach ($categories as $cat) {
                echo '<label><input type="checkbox" name="category[]" value="'.esc_attr($cat->slug).'"> '.esc_html($cat->name).'</label>';
            }
            ?>
        </div>

        <button type="submit">Filter</button>
        <input type="hidden" name="s" value="">
        <input type="hidden" name="post_type" value="product">
    </form>
    <?php
}

// Modify product query based on filters
add_action('woocommerce_product_query', 'filter_products_by_custom_parameters');
function filter_products_by_custom_parameters($q) {
    $meta_query = (array) $q->get('meta_query');
    $tax_query = (array) $q->get('tax_query');

    // Price Filter
    if (!empty($_GET['min_price']) && !empty($_GET['max_price'])) {
        $meta_query[] = [
            'key' => '_price',
            'value' => [$_GET['min_price'], $_GET['max_price']],
            'type' => 'NUMERIC',
            'compare' => 'BETWEEN'
        ];
    }

    // Color Filter
    if (!empty($_GET['color'])) {
        $tax_query[] = [
            'taxonomy' => 'pa_color',
            'field' => 'slug',
            'terms' => $_GET['color'],
            'operator' => 'IN'
        ];
    }

    // Size Filter
    if (!empty($_GET['size'])) {
        $tax_query[] = [
            'taxonomy' => 'pa_size',
            'field' => 'slug',
            'terms' => $_GET['size'],
            'operator' => 'IN'
        ];
    }

    // Category Filter
    if (!empty($_GET['category'])) {
        $q->set('product_cat', implode(',', $_GET['category']));
    }

    $q->set('meta_query', $meta_query);
    $q->set('tax_query', $tax_query);
}
				

This code will enhance the template with Customizer sections to allow users to modify template components easily, while ensuring that best practices and security measures are in place.

Register an account to save your snippets or go Pro to get more features.