Files
CyyWordpress/functions.php
T
2026-04-29 19:04:20 +08:00

246 lines
8.1 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
/**
* CyyWordpress functions and definitions
*
* @link https://developer.wordpress.org/themes/basics/theme-functions/
*
* @package CyyWordpress
*/
// ---------------------------------------------------------------------------
// PHP & WordPress version guards
// ---------------------------------------------------------------------------
if ( version_compare( PHP_VERSION, '8.1', '<' ) ) {
add_action(
'admin_notices',
function (): void {
echo '<div class="notice notice-error"><p>'
. esc_html__( 'CyyWordpress 主题需要 PHP 8.1 或更高版本。', 'cyywordpress' )
. '</p></div>';
}
);
return; // Do not load theme in unsupported environments.
}
global $wp_version;
if ( version_compare( $wp_version, '6.0', '<' ) ) {
add_action(
'admin_notices',
function (): void {
echo '<div class="notice notice-error"><p>'
. esc_html__( 'CyyWordpress 主题需要 WordPress 6.0 或更高版本。', 'cyywordpress' )
. '</p></div>';
}
);
return;
}
// ---------------------------------------------------------------------------
// Version constant
// ---------------------------------------------------------------------------
if ( ! defined( '_S_VERSION' ) ) {
define( '_S_VERSION', '2.0.0' );
}
// ---------------------------------------------------------------------------
// Includes each file owns its own domain
// ---------------------------------------------------------------------------
require get_template_directory() . '/inc/custom-header.php';
require get_template_directory() . '/inc/template-tags.php';
require get_template_directory() . '/inc/template-functions.php';
require get_template_directory() . '/inc/customizer.php';
require get_template_directory() . '/inc/bulmapress_navwalker.php';
require get_template_directory() . '/inc/search.php';
require get_template_directory() . '/inc/pagination.php';
require get_template_directory() . '/inc/block-patterns.php';
require get_template_directory() . '/inc/class-cyywordpress-assets.php';
require get_template_directory() . '/inc/class-cyywordpress-seo.php';
require get_template_directory() . '/inc/class-cyywordpress-schema.php';
if ( defined( 'JETPACK__VERSION' ) ) {
require get_template_directory() . '/inc/jetpack.php';
}
// ---------------------------------------------------------------------------
// Theme setup
// ---------------------------------------------------------------------------
if ( ! function_exists( 'cyywordpress_setup' ) ) :
/**
* Sets up theme defaults and registers WordPress feature support.
*/
function cyywordpress_setup(): void {
load_theme_textdomain( 'cyywordpress', get_template_directory() . '/languages' );
add_theme_support( 'automatic-feed-links' );
add_theme_support( 'title-tag' );
add_theme_support( 'post-thumbnails' );
add_theme_support( 'responsive-embeds' );
add_theme_support( 'align-wide' );
add_theme_support( 'widgets-block-editor' );
// Editor styles keeps back-end visually consistent with the front end.
add_theme_support( 'editor-styles' );
add_editor_style( 'assets/css/editor-style.css' );
add_theme_support(
'html5',
array(
'search-form',
'comment-form',
'comment-list',
'gallery',
'caption',
'style',
'script',
)
);
add_theme_support(
'custom-background',
apply_filters(
'cyywordpress_custom_background_args',
array(
'default-color' => 'ffffff',
'default-image' => '',
)
)
);
add_theme_support( 'customize-selective-refresh-widgets' );
add_theme_support(
'custom-logo',
array(
'height' => 80,
'width' => 80,
'flex-width' => true,
'flex-height' => true,
)
);
// Navigation menus.
register_nav_menus(
array(
'menu-1' => esc_html__( '主导航', 'cyywordpress' ),
'menu-2' => esc_html__( '页脚导航', 'cyywordpress' ),
'social' => esc_html__( '社交媒体链接', 'cyywordpress' ),
)
);
// Custom image sizes.
add_image_size( 'card-thumbnail', 800, 450, true );
add_image_size( 'card-thumbnail-sm', 400, 225, true );
}
endif;
add_action( 'after_setup_theme', 'cyywordpress_setup' );
// ---------------------------------------------------------------------------
// Content width
// ---------------------------------------------------------------------------
function cyywordpress_content_width(): void {
$GLOBALS['content_width'] = apply_filters( 'cyywordpress_content_width', 760 );
}
add_action( 'after_setup_theme', 'cyywordpress_content_width', 0 );
// ---------------------------------------------------------------------------
// Widget areas
// ---------------------------------------------------------------------------
function cyywordpress_widgets_init(): void {
register_sidebar(
array(
'name' => esc_html__( '文章侧边栏', 'cyywordpress' ),
'id' => 'sidebar-1',
'description' => esc_html__( '出现在文章/页面侧边的 Widget 区域。', 'cyywordpress' ),
'before_widget' => '<div id="%1$s" class="widget card %2$s" style="margin-bottom:1rem;">',
'after_widget' => '</div>',
'before_title' => '<div class="card-header"><p class="card-header-title">',
'after_title' => '</p></div><div class="card-content">',
)
);
register_sidebar(
array(
'name' => esc_html__( '页脚 Widget 1', 'cyywordpress' ),
'id' => 'sidebar-footer-1',
'description' => esc_html__( '页脚第一列 Widget 区域。', 'cyywordpress' ),
'before_widget' => '<div id="%1$s" class="widget %2$s">',
'after_widget' => '</div>',
'before_title' => '<h3 class="footer-widget-title">',
'after_title' => '</h3>',
)
);
register_sidebar(
array(
'name' => esc_html__( '页脚 Widget 2', 'cyywordpress' ),
'id' => 'sidebar-footer-2',
'description' => esc_html__( '页脚第二列 Widget 区域。', 'cyywordpress' ),
'before_widget' => '<div id="%1$s" class="widget %2$s">',
'after_widget' => '</div>',
'before_title' => '<h3 class="footer-widget-title">',
'after_title' => '</h3>',
)
);
register_sidebar(
array(
'name' => esc_html__( '页脚 Widget 3', 'cyywordpress' ),
'id' => 'sidebar-footer-3',
'description' => esc_html__( '页脚第三列 Widget 区域。', 'cyywordpress' ),
'before_widget' => '<div id="%1$s" class="widget %2$s">',
'after_widget' => '</div>',
'before_title' => '<h3 class="footer-widget-title">',
'after_title' => '</h3>',
)
);
register_sidebar(
array(
'name' => esc_html__( '导航栏右侧', 'cyywordpress' ),
'id' => 'sidebar-header',
'description' => esc_html__( '出现在导航栏右侧的 Widget 区域(搜索等)。', 'cyywordpress' ),
'before_widget' => '<div id="%1$s" class="widget navbar-item %2$s">',
'after_widget' => '</div>',
'before_title' => '<span class="screen-reader-text">',
'after_title' => '</span>',
)
);
}
add_action( 'widgets_init', 'cyywordpress_widgets_init' );
// ---------------------------------------------------------------------------
// Initialise asset, SEO and schema classes
// ---------------------------------------------------------------------------
add_action(
'after_setup_theme',
function (): void {
CyyWordpress_Assets::init();
CyyWordpress_SEO::init();
CyyWordpress_Schema::init();
}
);
// ---------------------------------------------------------------------------
// Lazy-load attributes on all attachment images
// ---------------------------------------------------------------------------
add_filter(
'wp_get_attachment_image_attributes',
function ( array $attr ): array {
$attr['loading'] = 'lazy';
$attr['decoding'] = 'async';
return $attr;
}
);
// ---------------------------------------------------------------------------
// Remove WordPress emoji CDN (performance)
// ---------------------------------------------------------------------------
remove_action( 'wp_head', 'print_emoji_detection_script', 7 );
remove_action( 'wp_print_styles', 'print_emoji_styles' );
remove_action( 'admin_print_scripts', 'print_emoji_detection_script' );
remove_action( 'admin_print_styles', 'print_emoji_styles' );