File: /home/khabarnavis/web/khabarnavis.com/public_html/wp-content/themes/khabarnavis/functions.php
<?php
/**
* Khabarnavis Theme Functions
*/
// Theme Setup
function khabarnavis_setup() {
// Load text domain
load_theme_textdomain('khabarnavis', get_template_directory() . '/languages');
// Add Theme Support
add_theme_support('title-tag');
add_theme_support('post-thumbnails');
add_theme_support('automatic-feed-links');
add_theme_support('html5', ['search-form', 'comment-form', 'comment-list', 'gallery', 'caption', 'style', 'script']);
add_theme_support('align-wide');
add_theme_support('custom-logo', [
'height' => 100,
'width' => 400,
'flex-height' => true,
'flex-width' => true,
]);
add_theme_support('customize-selective-refresh-widgets');
// Register nav menus
register_nav_menus([
'primary' => __('Primary Menu', 'khabarnavis'),
'footer-one' => __('Footer Column 1', 'khabarnavis'),
'footer-two' => __('Footer Column 2', 'khabarnavis'),
]);
// Add image sizes
add_image_size('khabarnavis-hero', 1200, 675, true);
add_image_size('khabarnavis-featured', 800, 533, true);
add_image_size('khabarnavis-medium', 600, 400, true);
add_image_size('khabarnavis-thumb', 400, 267, true);
add_image_size('khabarnavis-square', 150, 150, true);
}
add_action('after_setup_theme', 'khabarnavis_setup');
// Set content width
function khabarnavis_content_width() {
if (!isset($content_width)) {
$GLOBALS['content_width'] = 800;
}
}
add_action('template_redirect', 'khabarnavis_content_width');
// Enqueue Scripts & Styles
function khabarnavis_scripts() {
$ver = wp_get_theme()->get('Version');
// Google Fonts
wp_enqueue_style('khabarnavis-fonts',
'https://fonts.googleapis.com/css2?family=Hind:wght@300;400;500;600;700&family=Newsreader:ital,opsz,wght@0,6..72,400;0,6..72,700;0,6..72,900;1,6..72,400;1,6..72,700&family=Inter:wght@300;400;500;600;700&family=Work+Sans:wght@400;500;600;700&display=swap',
[], null
);
// Main stylesheet
wp_enqueue_style('khabarnavis-style', get_stylesheet_uri(), ['khabarnavis-fonts'], $ver);
// Main JS
wp_enqueue_script('khabarnavis-main', get_template_directory_uri() . '/assets/js/main.js', [], $ver, true);
// Comment reply script
if (is_singular() && comments_open() && get_option('thread_comments')) {
wp_enqueue_script('comment-reply');
}
// Pass data to JS
wp_localize_script('khabarnavis-main', 'khabarnavisData', [
'ajaxurl' => admin_url('admin-ajax.php'),
'nonce' => wp_create_nonce('khabarnavis_nonce'),
'siteurl' => get_site_url(),
]);
}
add_action('wp_enqueue_scripts', 'khabarnavis_scripts');
// Register Sidebars
function khabarnavis_sidebars() {
$defaults = [
'before_title' => '<h3 class="widget-title">',
'after_title' => '</h3>',
'before_widget' => '<div id="%1$s" class="widget %2$s">',
'after_widget' => '</div>',
];
register_sidebar(array_merge($defaults, [
'name' => __('Main Sidebar', 'khabarnavis'),
'id' => 'sidebar-main',
'description' => __('Widgets in this area appear in the right sidebar.', 'khabarnavis'),
]));
register_sidebar(array_merge($defaults, [
'name' => __('Footer Column 1', 'khabarnavis'),
'id' => 'footer-col-1',
'description' => __('Widgets in footer column 1.', 'khabarnavis'),
]));
register_sidebar(array_merge($defaults, [
'name' => __('Footer Column 2', 'khabarnavis'),
'id' => 'footer-col-2',
'description' => __('Widgets in footer column 2.', 'khabarnavis'),
]));
}
add_action('widgets_init', 'khabarnavis_sidebars');
// Custom Excerpt Length & More Link
function khabarnavis_excerpt_length($length) { return 25; }
add_filter('excerpt_length', 'khabarnavis_excerpt_length');
function khabarnavis_excerpt_more($more) {
return '… <a class="read-more" href="' . get_permalink() . '">' . __('Read more', 'khabarnavis') . '</a>';
}
add_filter('excerpt_more', 'khabarnavis_excerpt_more');
// Helper: Get category badge HTML
function khabarnavis_get_category_badge($post_id = null) {
if (!$post_id) $post_id = get_the_ID();
$cats = get_the_category($post_id);
if (!$cats) return '';
$cat = $cats[0];
$color = get_term_meta($cat->term_id, 'cat_color', true) ?: '#b90015';
return '<span class="category-badge" style="background:' . esc_attr($color) . '">' . esc_html($cat->name) . '</span>';
}
// Helper: Get read time
function khabarnavis_read_time($post_id = null) {
if (!$post_id) $post_id = get_the_ID();
$content = get_post_field('post_content', $post_id);
$word_count = str_word_count(strip_tags($content));
$minutes = max(1, ceil($word_count / 200));
return $minutes . ' ' . __('MIN READ', 'khabarnavis');
}
// Helper: Article meta
function khabarnavis_article_meta($post_id = null) {
if (!$post_id) $post_id = get_the_ID();
$date = get_the_date('', $post_id);
$author = get_the_author_meta('display_name', get_post_field('post_author', $post_id));
$read_time = khabarnavis_read_time($post_id);
return '<span class="card-meta metadata">'
. esc_html($date)
. '<span class="dot"></span>' . esc_html($author)
. '<span class="dot"></span>' . esc_html($read_time)
. '</span>';
}
// Nav walker for responsive menu
function khabarnavis_nav_walker() {
return new Walker_Nav_Menu();
}
// Add body classes
function khabarnavis_body_classes($classes) {
if (is_active_sidebar('sidebar-main') && !is_singular('post')) {
$classes[] = 'has-sidebar';
}
if (is_singular()) {
$classes[] = 'single';
}
return $classes;
}
add_filter('body_class', 'khabarnavis_body_classes');
// Thumbnail fallback
function khabarnavis_thumbnail_fallback($size = 'khabarnavis-medium', $class = '') {
$fallback = get_template_directory_uri() . '/assets/images/placeholder.jpg';
return '<img src="' . esc_url($fallback) . '" alt="" class="' . esc_attr($class) . '">';
}
// Comment template callback
function khabarnavis_comment_template($comment, $args, $depth) {
$commenter = get_comment_author($comment);
$initials = mb_strtoupper(mb_substr($commenter, 0, 2));
?>
<div id="comment-<?php comment_ID(); ?>" <?php comment_class('comment-item'); ?>>
<div class="comment-avatar"><?php echo esc_html($initials); ?></div>
<div class="comment-bubble">
<div class="comment-meta">
<span class="comment-author"><?php echo esc_html($commenter); ?></span>
<span class="comment-time metadata"><?php comment_date('j M Y'); ?></span>
</div>
<div class="comment-text">
<?php comment_text(); ?>
</div>
<div>
<?php comment_reply_link(array_merge($args, ['reply_text' => __('Reply', 'khabarnavis'), 'class' => 'comment-reply-btn', 'depth' => $depth, 'max_depth' => $args['max_depth']])); ?>
</div>
</div>
</div>
<?php
}
// Title separator
function khabarnavis_document_title_separator($sep) { return '|'; }
add_filter('document_title_separator', 'khabarnavis_document_title_separator');
// Category Color Customizer
function khabarnavis_term_meta_color() {
$screen = get_current_screen();
if ($screen && in_array($screen->taxonomy, get_taxonomies())) { ?>
<div class="form-field">
<label><?php _e('Category Color', 'khabarnavis'); ?></label>
<input type="color" name="cat_color" value="#b90015">
</div>
<?php }
}
add_action('category_add_form_fields', 'khabarnavis_term_meta_color');
function khabarnavis_save_term_color($term_id) {
if (isset($_POST['cat_color'])) {
update_term_meta($term_id, 'cat_color', sanitize_hex_color($_POST['cat_color']));
}
}
add_action('created_category', 'khabarnavis_save_term_color');
add_action('edited_category', 'khabarnavis_save_term_color');
// Theme Customizer
function khabarnavis_customize_register($wp_customize) {
// Site identity
$wp_customize->add_setting('khabarnavis_tagline', ['default' => 'खबर नवीस', 'sanitize_callback' => 'wp_kses_post']);
$wp_customize->add_control('khabarnavis_tagline', [
'label' => __('Top Bar Tagline', 'khabarnavis'),
'section' => 'title_tagline',
'type' => 'text',
]);
// Colors
$wp_customize->add_section('khabarnavis_colors', ['title' => __('Theme Colors', 'khabarnavis'), 'priority' => 40]);
$wp_customize->add_setting('primary_color', ['default' => '#b90015', 'sanitize_callback' => 'sanitize_hex_color', 'transport' => 'postMessage']);
$wp_customize->add_control(new WP_Customize_Color_Control($wp_customize, 'primary_color', [
'label' => __('Primary (Accent) Color', 'khabarnavis'),
'section' => 'khabarnavis_colors',
]));
}
add_action('customize_register', 'khabarnavis_customize_register');
// Output dynamic CSS from customizer
function khabarnavis_customize_css() {
$primary = get_theme_mod('primary_color', '#b90015');
?>
<style>
:root {
--primary: <?php echo sanitize_hex_color($primary); ?>;
--primary-container: <?php echo sanitize_hex_color($primary); ?>dd;
}
</style>
<?php
}
add_action('wp_head', 'khabarnavis_customize_css');