/* ============ Setup Functions ============ */
/**
* Sets the schema type for structured data based on the context (single post, author, search results, or webpage).
*/
function set_schema_type()
{
$schema = 'https://schema.org/';
if (function_exists('is_product') && is_product()) {
$type = 'Product';
} elseif (is_single()) {
$type = 'Article';
} elseif (is_author()) {
$type = 'ProfilePage';
} elseif (is_search()) {
$type = 'SearchResultsPage';
} else {
$type = 'WebPage';
}
echo 'itemscope itemtype="' . esc_url($schema . $type) . '"';
}
/**
* Fallback for wp_body_open() for older WordPress versions.
*/
if (!function_exists('wp_body_open')) {
function wp_body_open()
{
do_action('wp_body_open');
}
}
/* ============ Setup Hooks and Actions ============ */
/**
* Initializes theme setup functions and supports various features.
*/
add_action('after_setup_theme', '__setup_theme');
function __setup_theme()
{
global $content_width;
if (!isset($content_width)) {
$content_width = 1920; // Set the maximum content width.
}
// Add theme supports for various features.
add_theme_support('title-tag');
add_theme_support('post-thumbnails');
add_theme_support('responsive-embeds');
add_theme_support('automatic-feed-links');
add_theme_support('html5', array('search-form', 'navigation-widgets'));
add_theme_support('appearance-tools');
add_theme_support('woocommerce');
}
/**
* Enqueues stylesheets for the theme. Includes cache busting
*/
add_action('wp_enqueue_scripts', '__load_css');
function __load_css()
{
wp_enqueue_style(
'presets',
get_stylesheet_directory_uri() . '/css/presets.css',
array(),
1
);
wp_enqueue_style(
'style-main',
get_stylesheet_directory_uri() . '/style.css',
array(),
1
);
wp_enqueue_style(
'responsive-laptop',
get_stylesheet_directory_uri() . '/css/responsive/laptop.css',
array(),
1
);
wp_enqueue_style(
'responsive-tablet',
get_stylesheet_directory_uri() . '/css/responsive/tablet.css',
array(),
1
);
wp_enqueue_style(
'responsive-mobile',
get_stylesheet_directory_uri() . '/css/responsive/mobile.css',
array(),
1
);
}
/**
* Enqueues scripts for the theme.
*/
add_action('wp_enqueue_scripts', '__load_js');
function __load_js()
{
$script_version = filemtime(get_template_directory() . '/js/script.js');
wp_enqueue_script('jquery');
wp_enqueue_script(
'script-main',
get_template_directory_uri() . '/js/script.js',
array('jquery'),
$script_version,
true
);
$logo = get_field('logo', 'option')['url'];
$alt_logo = get_field('alt_logo', 'option')['url'];
wp_localize_script(
'script-main',
'global_data',
array(
'logo' => esc_url($logo),
'alt_logo' => esc_url($alt_logo),
'theme_uri' => get_template_directory_uri()
)
);
}
/**
* Removes the default Posts menu from the WordPress admin.
*/
add_action('admin_menu', '__remove_default_post_type');
function __remove_default_post_type()
{
remove_menu_page('edit.php');
}
/**
* Sets the separator for the document title in the head.
*/
add_filter('document_title_separator', '__set_title_separator');
function __set_title_separator($sep)
{
$sep = esc_html('|');
return $sep;
}
/**
* Sanitizes the title for safe output.
*/
add_filter('the_title', '__sanitize_title');
function __sanitize_title($title)
{
if ($title == '') {
return esc_html('...');
} else {
return wp_kses_post($title);
}
}
/**
* Adds schema.org attributes to navigation menu links.
*/
add_filter('nav_menu_link_attributes', '__schema_url', 10);
function __schema_url($atts)
{
$atts['itemprop'] = 'url';
return $atts;
}
/**
* Outputs a skip link for accessibility purposes.
*/
add_action('wp_body_open', '__skip_link', 5);
function __skip_link()
{
echo '' . esc_html__('Skip to the content', 'cyberfrog') . '';
}
/**
* Modifies the "Read More" link for content excerpts.
*/
add_filter('the_content_more_link', '__read_more_link');
function __read_more_link()
{
if (!is_admin()) {
return ' '
. sprintf(__('...%s', 'cyberfrog'), ' '
. esc_html(get_the_title())
. '')
. '';
}
}
/**
* Modifies the excerpt "Read More" link.
*/
add_filter('excerpt_more', '__excerpt_read_more_link');
function __excerpt_read_more_link($more)
{
if (!is_admin()) {
global $post;
return ' '
. sprintf(__('...%s', 'cyberfrog'), ' '
. esc_html(get_the_title())
. '')
. '';
}
}
/**
* Disables the big image size threshold feature.
*/
add_filter('big_image_size_threshold', '__return_false');
/**
* Limits the intermediate image sizes available for upload.
*/
add_filter('intermediate_image_sizes_advanced', '__limit_image_upload_size');
function __limit_image_upload_size($sizes)
{
unset($sizes['medium_large']);
unset($sizes['1536x1536']);
unset($sizes['2048x2048']);
return $sizes;
}
/**
* OpenGraph support for SEO
*/
function __add_open_graph_meta()
{
if (is_single()) {
global $post;
echo '';
echo '';
echo '';
}
}
add_action('wp_head', '__add_open_graph_meta');
/* ============ Useful Functions ============ */
/**
* Truncates a string to a specified length, appending an ellipsis if it exceeds that length.
* @param string $the_string The input string to truncate.
* @param int $max_length The maximum length of the string.
* @return string The truncated string.
*/
function truncate_string($the_string, $max_length)
{
if (strlen($the_string) > $max_length) {
return trim(substr($the_string, 0, $max_length)) . '...'; // Append ellipsis.
} else {
return $the_string;
}
}
/**
* Outputs a JavaScript console log for debugging.
* @param mixed $data The data to log to the console.
*/
function console_log($data)
{
echo '';
}
function modify_admin_footer()
{
echo '';
}
add_filter('admin_footer_text', 'modify_admin_footer');
// Disable support for comments and trackbacks in post types
function disable_comments_post_types_support()
{
$post_types = get_post_types();
foreach ($post_types as $post_type) {
if (post_type_supports($post_type, 'comments')) {
remove_post_type_support($post_type, 'comments');
remove_post_type_support($post_type, 'trackbacks');
}
}
}
add_action('admin_init', 'disable_comments_post_types_support');
// Close comments on the front-end
function disable_comments_status()
{
return false;
}
add_filter('comments_open', 'disable_comments_status', 20, 2);
add_filter('pings_open', 'disable_comments_status', 20, 2);
// Hide existing comments
function disable_comments_hide_existing_comments($comments)
{
$comments = array();
return $comments;
}
add_filter('comments_array', 'disable_comments_hide_existing_comments', 10, 2);
// Redirect any user trying to access comments page
function disable_comments_admin_menu_redirect()
{
global $pagenow;
if ($pagenow === 'edit-comments.php') {
wp_redirect(admin_url());
exit();
}
}
add_action('admin_init', 'disable_comments_admin_menu_redirect');
// Remove comments menu from admin sidebar
function disable_comments_admin_menu()
{
remove_menu_page('edit-comments.php');
}
add_action('admin_menu', 'disable_comments_admin_menu');
// Remove comments links from admin bar
function disable_comments_admin_bar()
{
if (is_admin_bar_showing()) {
remove_action('admin_bar_menu', 'wp_admin_bar_comments_menu', 60);
}
}
add_action('init', 'disable_comments_admin_bar');
//REMOVE ALL NOTICES
add_action('admin_notices', '__return_false');
//REMOVE ALL UPDATE NOTICES
add_filter('pre_site_transient_update_core', '__return_null');
add_filter('pre_site_transient_update_plugins', '__return_null');
add_filter('pre_site_transient_update_themes', '__return_null');
add_action('admin_init', function () {
remove_action('admin_notices', ['WC_Admin_Notices', 'template_file_check_notice']);
});
add_action('init', function () {
remove_action('admin_init', ['WC_Admin_Notices', 'template_file_check']);
});
add_action('pre_get_posts', function ($query) {
if (
!is_admin()
&& $query->is_main_query()
&& ($query->is_tax('manufacturer') || $query->is_tax('product-category'))
) {
$query->set('meta_query', [
[
'key' => 'custom_order',
'type' => 'NUMERIC',
],
]);
// Override default ordering — we'll use raw SQL
$query->set('orderby', 'none');
add_filter('posts_clauses', 'custom_product_sorting_clauses', 10, 2);
}
});
function custom_product_sorting_clauses($clauses, $query)
{
global $wpdb;
if (
!is_admin()
&& $query->is_main_query()
&& ($query->is_tax('manufacturer') || $query->is_tax('product-category'))
) {
$clauses['join'] .= "
LEFT JOIN {$wpdb->postmeta} AS custom_order_meta
ON ({$wpdb->posts}.ID = custom_order_meta.post_id AND custom_order_meta.meta_key = 'custom_order')
LEFT JOIN {$wpdb->postmeta} AS available_meta
ON ({$wpdb->posts}.ID = available_meta.post_id AND available_meta.meta_key = 'available')
LEFT JOIN {$wpdb->postmeta} AS price_meta
ON ({$wpdb->posts}.ID = price_meta.post_id AND price_meta.meta_key = '_price')
";
$clauses['orderby'] = "
CAST(custom_order_meta.meta_value AS UNSIGNED) DESC,
CASE WHEN available_meta.meta_value = '1' THEN 0 ELSE 1 END ASC,
CASE WHEN price_meta.meta_value IS NULL THEN 1 ELSE 0 END ASC,
CAST(price_meta.meta_value AS DECIMAL(10,2)) ASC
";
}
return $clauses;
}
//REMOVE DASHBOARD WIDGETS
function remove_all_dashboard_widgets()
{
global $wp_meta_boxes;
$wp_meta_boxes['dashboard']['normal'] = array();
$wp_meta_boxes['dashboard']['side'] = array();
$wp_meta_boxes['dashboard']['column3'] = array();
remove_action('welcome_panel', 'wp_welcome_panel');
}
add_action('wp_dashboard_setup', 'remove_all_dashboard_widgets');
function disable_wysiwyg_editor()
{
if (is_admin()) {
//remove_post_type_support('page', 'editor');
}
}
//add_action('admin_init', 'disable_wysiwyg_editor');
// Register Menus
function custom_theme_menus()
{
// Register multiple menus
register_nav_menus(array(
'header-menu' => __('Header Navigation', 'textdomain'),
'footer-menu' => __('Footer Navigation', 'textdomain'),
));
}
add_action('init', 'custom_theme_menus');
function add_custom_body_class($classes)
{
if (is_page() || is_single()) {
$title = get_the_title(); // Get the page title
$sanitized_title = sanitize_title($title); // Sanitize it for use as a class
$classes[] = $sanitized_title;
}
return $classes;
}
add_filter('body_class', 'add_custom_body_class');
function my_acf_init()
{
acf_update_setting('google_api_key', 'AIzaSyDgc1QE5dVD_5gs1-DwICAE0pVLQxJ1i0g');
}
add_action('acf/init', 'my_acf_init');
function theme_asset($filename)
{
return get_template_directory_uri() . '/assets/' . $filename;
}
function theme_include($filename)
{
return get_template_directory() . '/includes/' . $filename;
}
function theme_component($filename)
{
return get_template_directory() . '/components/' . $filename;
}
function admin_error($message)
{
if (is_admin()) {
echo $message;
}
}
function validate_include($required_args, $include_array)
{
if (array_diff_key(array_flip($required_args), $include_array)) {
$missing_args = array_filter($required_args, fn($arg) => !isset($popup[$arg]));
if ($missing_args) {
$error = 'undefined popup arguments: ' . implode(' ', array_map(fn($arg) => "[$arg]", $missing_args));
admin_error($error);
return false;
}
}
return true;
}
function enqueue_load_more_scripts()
{
wp_enqueue_script('load-more', get_template_directory_uri() . '/js/load-more.js', ['jquery'], null, true);
wp_localize_script('load-more', 'load_more_params', [
'ajaxurl' => admin_url('admin-ajax.php')
]);
}
add_action('wp_enqueue_scripts', 'enqueue_load_more_scripts');
function load_more_acf_relationship_posts()
{
$page = isset($_POST['page']) ? absint($_POST['page']) : 1;
$posts_per_page = isset($_POST['posts_per_page']) ? absint($_POST['posts_per_page']) : 5;
$related_posts = $_POST['related_posts'];
if ($related_posts) {
$offset = ($page - 1) * $posts_per_page;
$posts_to_display = array_slice($related_posts, $offset, $posts_per_page);
$output = '';
foreach ($posts_to_display as $data) {
if ($data['post']) {
$post = $data['post'];
} else {
$post = $data;
}
$cta_block['image']['url'] = get_the_post_thumbnail_url($post['ID']);
$cta_block['text'] = get_field('description', $post['ID']);
$cta_block['link']['title'] = get_field('link_prompt', $post['ID']);
$cta_block['link']['url'] = get_permalink($post['ID']);
ob_start();
include theme_include('cta_block.php');
$output .= ob_get_clean();
}
wp_reset_postdata();
$total_posts = count($related_posts);
wp_send_json([
'posts' => $output,
'has_more' => $offset + $posts_per_page < $total_posts,
]);
}
wp_die();
}
add_action('wp_ajax_load_more_acf_relationship_posts', 'load_more_acf_relationship_posts');
add_action('wp_ajax_nopriv_load_more_acf_relationship_posts', 'load_more_acf_relationship_posts');
function add_taxonomy_based_meta_to_product_on_save($post_id)
{
// Avoid infinite loop, autosave, and quick edit
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
return;
if (wp_is_post_revision($post_id))
return;
if (get_post_type($post_id) !== 'product')
return;
$product_categories = get_the_terms($post_id, 'product-category');
if (!empty($product_categories) && !is_wp_error($product_categories)) {
$product_category = $product_categories[0];
$importance = get_field('importance', 'term_' . $product_category->term_id);
if ($importance !== false) {
update_field('custom_order', $importance, $post_id);
}
}
}
add_action('save_post_product', 'add_taxonomy_based_meta_to_product_on_save');
// Save custom order in the FiboSearch search index
add_filter('dgwt/wcas/tnt/indexer/readable/product/data', function ($data, $product_id, $product) {
$value = get_field('custom_order', $product_id);
if (!empty($value)) {
$data['meta']['custom_order'] = $value;
}
return $data;
}, 10, 3);
add_filter('dgwt/wcas/suggestion_details/product/thumb_size', function ($size) {
// Default is 'woocommerce_thumbnail'
return 'full';
});
add_action('admin_enqueue_scripts', function ($hook) {
global $post;
// Only run on edit/add product pages
if (in_array($hook, ['post.php', 'post-new.php'], true) && isset($post->post_type) && $post->post_type === 'product') {
wp_add_inline_style(
'wp-admin', // Use the core admin stylesheet as a base
'.woocommerce-layout__header {
position: absolute !important;
top: 0 !important;
}'
);
}
}); get_header(); ?>
define('COMPONENTS_PATH', get_template_directory() . '/components/');
function add_component($slug, $options)
{
include COMPONENTS_PATH . $slug . '.php';
} ?>
foreach (get_field('components', get_queried_object()) as $component) {
$slug = $component['type'];
$options = $component[$slug];
add_component($slug, $options);
} ?>
get_footer(); ?>