| Server IP : 104.171.130.249 / Your IP : 216.73.216.146 Web Server : nginx/1.25.5 System : Linux 0dac65491b7e 6.8.0-134-generic #134-Ubuntu SMP PREEMPT_DYNAMIC Fri Jun 26 18:43:11 UTC 2026 x86_64 User : root ( 0) PHP Version : 8.4.23 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : OFF | Perl : ON | Python : OFF | Sudo : OFF | Pkexec : OFF Directory : /var/www/html/wp-content/themes/matreshka/inc/ |
Upload File : |
<?php
/**
* Category Customization
*
* Adds custom fields for product categories:
* - Background color picker
* - Uses category thumbnail as background image
*
* @package Matreshka
*/
// Exit if accessed directly
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Add background color field when creating new category
*/
function matreshka_add_category_background_color_field() {
?>
<div class="form-field">
<label for="category_bg_color"><?php esc_html_e( 'Background Color', 'matreshka' ); ?></label>
<input type="text" name="category_bg_color" id="category_bg_color" value="" class="matreshka-color-picker" />
<p class="description"><?php esc_html_e( 'Choose a background color for the category button', 'matreshka' ); ?></p>
</div>
<?php
}
add_action( 'product_cat_add_form_fields', 'matreshka_add_category_background_color_field' );
/**
* Add background color field when editing category
*/
function matreshka_edit_category_background_color_field( $term ) {
$bg_color = get_term_meta( $term->term_id, 'category_bg_color', true );
?>
<tr class="form-field">
<th scope="row">
<label for="category_bg_color"><?php esc_html_e( 'Background Color', 'matreshka' ); ?></label>
</th>
<td>
<input type="text" name="category_bg_color" id="category_bg_color" value="<?php echo esc_attr( $bg_color ); ?>" class="matreshka-color-picker" />
<p class="description"><?php esc_html_e( 'Choose a background color for the category button', 'matreshka' ); ?></p>
</td>
</tr>
<?php
}
add_action( 'product_cat_edit_form_fields', 'matreshka_edit_category_background_color_field' );
/**
* Save the background color field
*/
function matreshka_save_category_background_color( $term_id ) {
if ( isset( $_POST['category_bg_color'] ) ) {
update_term_meta( $term_id, 'category_bg_color', sanitize_hex_color( $_POST['category_bg_color'] ) );
}
}
add_action( 'created_product_cat', 'matreshka_save_category_background_color' );
add_action( 'edited_product_cat', 'matreshka_save_category_background_color' );
/**
* Enqueue color picker for category edit page
*/
function matreshka_category_color_picker_admin_scripts() {
global $pagenow;
// Only load on category pages
if ( ( 'term.php' === $pagenow || 'edit-tags.php' === $pagenow ) && isset( $_GET['taxonomy'] ) && 'product_cat' === $_GET['taxonomy'] ) {
// Enqueue WordPress color picker
wp_enqueue_style( 'wp-color-picker' );
wp_enqueue_script( 'wp-color-picker' );
// Add inline script to initialize color picker
wp_add_inline_script( 'wp-color-picker', '
jQuery(document).ready(function($) {
$(".matreshka-color-picker").wpColorPicker();
});
' );
}
}
add_action( 'admin_enqueue_scripts', 'matreshka_category_color_picker_admin_scripts' );
/**
* Helper function to get category button HTML with custom background
*
* @param int $category_id Category term ID
* @return string HTML for category button
*/
function matreshka_get_category_button_html( $category_id ) {
$category = get_term( $category_id, 'product_cat' );
if ( ! $category || is_wp_error( $category ) ) {
return '';
}
$category_link = get_term_link( $category );
// Get category thumbnail
$thumbnail_id = get_term_meta( $category->term_id, 'thumbnail_id', true );
$category_image = $thumbnail_id ? wp_get_attachment_url( $thumbnail_id ) : '';
// Get custom background color
$bg_color = get_term_meta( $category->term_id, 'category_bg_color', true );
// Build inline style
$style_parts = array();
if ( $category_image ) {
$style_parts[] = "background-image: url('" . esc_url( $category_image ) . "')";
$style_parts[] = "background-size: cover";
}
if ( $bg_color ) {
$style_parts[] = "background-color: " . esc_attr( $bg_color );
}
$inline_style = ! empty( $style_parts ) ? ' style="' . implode( '; ', $style_parts ) . '"' : '';
return sprintf(
'<a href="%s" class="btn btn-category montserrat-medium"%s>%s</a>',
esc_url( $category_link ),
$inline_style,
esc_html( $category->name )
);
}
/**
* Get category background styles array
*
* @param int $category_id Category term ID
* @return array Array with 'image', 'color', and 'inline_style' keys
*/
function matreshka_get_category_background_styles( $category_id ) {
// Get category thumbnail
$thumbnail_id = get_term_meta( $category_id, 'thumbnail_id', true );
$category_image = $thumbnail_id ? wp_get_attachment_url( $thumbnail_id ) : '';
// Get custom background color
$bg_color = get_term_meta( $category_id, 'category_bg_color', true );
// Build inline style
$style_parts = array();
if ( $category_image ) {
$style_parts[] = "background-image: url('" . esc_url( $category_image ) . "')";
}
if ( $bg_color ) {
$style_parts[] = "background-color: " . esc_attr( $bg_color );
}
return array(
'image' => $category_image,
'color' => $bg_color,
'inline_style' => ! empty( $style_parts ) ? ' style="' . implode( '; ', $style_parts ) . '"' : '',
);
}