post_content; preg_match_all('//sim', $content, $result, PREG_PATTERN_ORDER); return count($result[1]); } } //覆盖原有标签 function get_the_term_listsss($post_id, $taxonomy, $before = '', $sep = '', $after = '', $tagsLength = 0) { $terms = get_the_terms($post_id, $taxonomy); if (is_wp_error($terms)) { return $terms; } if (empty($terms)) { return false; } $links = array(); $ss = count($terms) < $tagsLength ? count($terms) : $tagsLength; for ($i = 0; $i < $ss; $i++) { $link = get_term_link($terms[$i], $taxonomy); if (is_wp_error($link)) { return $link; } $links[] = ''; } $term_links = apply_filters("term_links-{$taxonomy}", $links); if (count($terms) > $tagsLength) { return is_single() ?: $before . implode($sep, $term_links) . '...'; } else { return $before . implode($sep, $term_links) . $after; } } function get_the_tag_listss($before = '', $sep = '', $after = '', $post_id = 0, $tagsLength = 0) { $tag_list = get_the_term_listsss($post_id, 'post_tag', $before, $sep, $after, $tagsLength); return apply_filters('the_tags', $tag_list, $before, $sep, $after, $post_id); } function the_tagss($before = null, $sep = ', ', $after = '', $tagsLength = 0) { if (null === $before) { $before = __('Tags: ', 'leton'); } $the_tags = get_the_tag_listss($before, $sep, $after, null, $tagsLength); if (!is_wp_error($the_tags)) { echo $the_tags; } } //覆盖原有类别 function the_categoryss($separator = '', $parents = '', $post_id = false) { echo get_the_category_listss($separator, $parents, $post_id); } function get_the_category_listss($separator = '', $parents = '', $post_id = false) { global $wp_rewrite; if (!is_object_in_taxonomy(get_post_type($post_id), 'category')) { /** This filter is documented in wp-includes/category-template.php */ return apply_filters('the_category', '', $separator, $parents); } $categories = apply_filters('the_category_list', get_the_category($post_id), $post_id); if (empty($categories)) { /** This filter is documented in wp-includes/category-template.php */ return apply_filters('the_category', __('Uncategorized', 'leton'), $separator, $parents); } $rel = (is_object($wp_rewrite) && $wp_rewrite->using_permalinks()) ? 'rel="category tag"' : 'rel="category"'; $thelist = ''; if ('' === $separator) { $thelist .= ''; } else { $i = 0; foreach ($categories as $category) { if (0 < $i) { $thelist .= $separator; } switch (strtolower($parents)) { case 'multiple': if ($category->parent) { $thelist .= get_category_parents($category->parent, true, $separator); } $thelist .= '' . $category->name . ''; break; case 'single': $thelist .= ''; if ($category->parent) { $thelist .= get_category_parents($category->parent, false, $separator); } $thelist .= "$category->name"; break; case '': default: $thelist .= '' . $category->name . ''; } ++$i; } } return apply_filters('the_category', $thelist, $separator, $parents); } //获取列表中谋篇文章内的图片数量 function wp_content_image() { global $post; $first_img = ''; ob_start(); ob_end_clean(); $output = preg_match_all('//iU', wp_unslash($post->post_content), $matches); if (empty($output)) { $first_img = ''; } else { $first_img = $matches[1][0]; } return $first_img; }; //评论字段顺序 add_filter('comment_form_fields', 'mo_comment_fields_custom_order'); function mo_comment_fields_custom_order($fields) { $comment_field = $fields['comment']; $author_field = $fields['author']; $email_field = $fields['email']; $url_field = $fields['url']; $cookies_field = $fields['cookies']; unset($fields['comment']); unset($fields['author']); unset($fields['email']); unset($fields['url']); unset($fields['cookies']); // the order of fields is the order below, change it as needed: $fields['author'] = $author_field; $fields['email'] = $email_field; $fields['url'] = $url_field; $fields['comment'] = $comment_field; $fields['cookies'] = $cookies_field; // done ordering, now return the fields: return $fields; } //覆盖原有搜索 add_filter('request', 'uctheme_redirect_blank_search'); function uctheme_redirect_blank_search($query_variables) { if (isset($_GET['s']) && !is_admin()) { if (empty($_GET['s']) || ctype_space($_GET['s'])) { wp_redirect(home_url()); exit; } } return $query_variables; } function wpdocs_my_search_form($form) { $form = '
'; return $form; } add_filter('get_search_form', 'wpdocs_my_search_form'); //创建分类列表数组用于页面附加列表中的列表 function category_select() { $category_select = []; foreach (get_terms(array('taxonomy' => 'category', 'hide_empty' => false,)) as $category_idss) { $category_select[$category_idss->term_id] = $category_idss->name; }; return $category_select; } //创建显示通知控件 function if_siteNotice_switch_is_on() { return get_theme_mod('siteNotice_switch') == 1 ? true : false; } //创建主色调用 $mainColor1 = get_theme_mod('mainColor1', '#ff4700'); $mainColor2 = get_theme_mod('mainColor2', '#ffc5af'); //创建主题后台扩展选项 function leton_customize_register($wp_customize) { $wp_customize->add_section('sidebar_setting', array('title' => __('sidebar', 'leton'), 'priority' => 101)); $wp_customize->add_section('other_setting', array('title' => __('other', 'leton'), 'priority' => 161)); $wp_customize->remove_section('static_front_page'); $wp_customize->add_setting('mainColor1', array('default' => '#2cabff', 'transport' => 'refresh', 'sanitize_callback' => 'sanitize_hex_color')); $wp_customize->add_control(new WP_Customize_Color_Control($wp_customize, 'mainColor1', array( 'section' => 'colors', 'description' => __('Main hues include activation color and link color, etc. [#2cabff]', 'leton'), 'label' => __('main color', 'leton') ))); $wp_customize->add_setting('mainColor2', array('default' => '#c3e9ff', 'transport' => 'refresh', 'sanitize_callback' => 'sanitize_hex_color')); $wp_customize->add_control(new WP_Customize_Color_Control($wp_customize, 'mainColor2', array( 'section' => 'colors', 'description' => __('Including but not limited to the color of the button background [#c3e9ff]', 'leton'), 'label' => __('minor color', 'leton') ))); $wp_customize->add_setting('siteNotice_switch', array('sanitize_callback' => 'esc_html')); $wp_customize->add_control('siteNotice_switch', array( 'section' => 'sidebar_setting', 'label' => __('Enable website notification content', 'leton'), 'description' => __('Notify visitors who are browsing the site', 'leton'), 'type' => 'checkbox', )); $wp_customize->add_setting('siteNotice_content', array('sanitize_callback' => 'esc_html')); $wp_customize->add_control('siteNotice_content', array( 'section' => 'sidebar_setting', 'label' => __('Website notification content', 'leton'), 'type' => 'text', 'active_callback' => 'if_siteNotice_switch_is_on', )); $wp_customize->add_setting('siteNotice_speed', array('sanitize_callback' => 'esc_html')); $wp_customize->add_control('siteNotice_speed', array( 'section' => 'sidebar_setting', 'label' => __('Website notification scrolling speed', 'leton'), 'type' => 'number', 'active_callback' => 'if_siteNotice_switch_is_on', 'input_attrs' => array( 'placeholder' => __('Default value: 3', 'leton') ) )); $wp_customize->add_setting('b1', array('sanitize_callback' => 'esc_str')); $wp_customize->add_control('b1', array( 'section' => 'sidebar_setting', 'description' => '

', 'type' => 'hidden', )); $wp_customize->add_setting('frontListForCategory', array('sanitize_callback' => 'esc_html')); $wp_customize->add_control('frontListForCategory', array( 'section' => 'sidebar_setting', 'label' => __('Page custom list', 'leton'), 'description' => __('Used to display a list of selected type articles in the lower left corner', 'leton'), 'type' => 'select', 'choices' => category_select() )); $wp_customize->add_setting('randomPostNum', array('sanitize_callback' => 'esc_html')); $wp_customize->add_control('randomPostNum', array( 'section' => 'sidebar_setting', 'label' => __('Number of random articles displayed', 'leton'), 'type' => 'number', 'input_attrs' => array( 'placeholder' => __('Default value: 12', 'leton') ) )); $wp_customize->add_setting('customPostNum', array('sanitize_callback' => 'esc_html')); $wp_customize->add_control('customPostNum', array( 'section' => 'sidebar_setting', 'label' => __('The number of custom articles is displayed', 'leton'), 'type' => 'number', 'input_attrs' => array( 'placeholder' => __('Default value: 6', 'leton') ) )); $wp_customize->add_setting('siteKeyword', array('sanitize_callback' => 'esc_html')); $wp_customize->add_control('siteKeyword', array( 'section' => 'title_tagline', 'label' => __('Home keywords', 'leton'), 'description' => __('SEO home keywords, only the home page effective, please use half corner comma separated ",".', 'leton'), 'type' => 'text' )); $wp_customize->add_setting('siteDescript', array('sanitize_callback' => 'esc_html')); $wp_customize->add_control('siteDescript', array( 'section' => 'title_tagline', 'label' => __('Home description', 'leton'), 'description' => __('SEO Home page description, only the home page.', 'leton'), 'type' => 'text' )); $wp_customize->add_setting('siteMobileLogo', array('sanitize_callback' => 'esc_html')); $wp_customize->add_control(new WP_Customize_Cropped_Image_Control($wp_customize, 'siteMobileLogo', array( 'priority' => '100', 'label' => __('mobile logo', 'leton'), 'description' => __('Please use a 256x256 resolution picture, square will do. [Mobile terminal and desktop terminal LOGO can choose to upload one of them]', 'leton'), 'section' => 'title_tagline', 'height' => 256, // cropper Height 'width' => 256, 'flex_width' => true, //Flexible Width 'flex_height' => true ))); $wp_customize->add_setting('siteDesktopLogo', array('sanitize_callback' => 'esc_html')); $wp_customize->add_control(new WP_Customize_Cropped_Image_Control($wp_customize, 'siteDesktopLogo', array( 'priority' => '101', 'label' => __('desktop logo', 'leton'), 'description' => __('Please use a 256x1000 resolution picture, square will do. [Mobile terminal and desktop terminal LOGO can choose to upload one of them]', 'leton'), 'section' => 'title_tagline', 'height' => 256, 'width' => 1000, 'flex_width' => true, 'flex_height' => true ))); $wp_customize->add_setting('shortDate', array('sanitize_callback' => 'esc_html')); $wp_customize->add_control('shortDate', array( 'label' => __('Short time format', 'leton'), 'section' => 'other_setting', 'type' => 'text', 'input_attrs' => array( 'placeholder' => 'm/d' ) )); $wp_customize->add_setting('longDate', array('sanitize_callback' => 'esc_html')); $wp_customize->add_control('longDate', array( 'label' => __('Long time format', 'leton'), 'section' => 'other_setting', 'type' => 'text', 'input_attrs' => array( 'placeholder' => 'Y-m-d' ) )); $wp_customize->add_setting('footerCopyInfo', array('sanitize_callback' => 'esc_html')); $wp_customize->add_control('footerCopyInfo', array( 'section' => 'other_setting', 'label' => __('Footer copyright information', 'leton'), 'description' => '', 'type' => 'text', 'input_attrs' => array( 'placeholder' => 'LETON © 2023' ) )); $wp_customize->add_setting('b2', array('sanitize_callback' => 'esc_str')); $wp_customize->add_control('b2', array( 'section' => 'other_setting', 'description' => '

', 'type' => 'hidden', )); } add_action('customize_register', 'leton_customize_register'); function l() { __('about', 'leton'); __('archive', 'leton'); __('guesbook', 'leton'); } //移除全局内联和svg覆盖 remove_action('wp_body_open', 'wp_global_styles_render_svg_filters'); //禁用表情符号 function disable_emojis() { remove_action('wp_head', 'print_emoji_detection_script', 7); remove_action('admin_print_scripts', 'print_emoji_detection_script'); remove_action('wp_print_styles', 'print_emoji_styles'); remove_action('admin_print_styles', 'print_emoji_styles'); remove_filter('the_content_feed', 'wp_staticize_emoji'); remove_filter('comment_text_rss', 'wp_staticize_emoji'); remove_filter('wp_mail', 'wp_staticize_emoji_for_email'); add_filter('tiny_mce_plugins', 'disable_emojis_tinymce'); } add_action('init', 'disable_emojis'); //过滤功能用于删除tinymce表情符号插件。 function disable_emojis_tinymce($plugins) { if (is_array($plugins)) { return array_diff($plugins, array('wpemoji')); } else { return array(); } } //启用SEO自定义字段用于新建文章添加关键词描述 $new_meta_boxes = array("keywords", "description"); function new_meta_boxes() { global $post, $new_meta_boxes; $infoName = array(__('keyword:', 'leton'), __('describe:', 'leton')); $infoDesc = array(__('Separate keywords with half-corner commas', 'leton'), __('Leave blank to display summary', 'leton')); for ($i = 0; $i < count($new_meta_boxes); $i++) { $meta_box_value = get_post_meta($post->ID, $new_meta_boxes[$i] . '_value', true); // 自定义字段标题 echo '
'; echo '

' . $infoName[$i] . '

'; // 自定义字段输入框 echo ''; echo '
'; } echo ''; } function create_meta_box() { global $theme_name; if (function_exists('add_meta_box')) { add_meta_box('new-meta-boxes', __('SEO option', 'leton'), 'new_meta_boxes', 'post', 'normal', 'high'); } } function save_postdata($post_id) { if (isset($_POST['newmetaboxes_noncename'])) { $newmetaboxes_noncename = $_POST['newmetaboxes_noncename']; } else { $newmetaboxes_noncename = ''; }; global $new_meta_boxes; if (!wp_verify_nonce($newmetaboxes_noncename, plugin_basename(__FILE__))) return; if (!current_user_can('edit_posts', $post_id)) return; for ($i = 0; $i < count($new_meta_boxes); $i++) { $data = $_POST[$new_meta_boxes[$i] . '_value']; if (get_post_meta($post_id, $new_meta_boxes[$i] . '_value') == "") add_post_meta($post_id, $new_meta_boxes[$i] . '_value', $data, true); elseif ($data != get_post_meta($post_id, $new_meta_boxes[$i] . '_value', true)) update_post_meta($post_id, $new_meta_boxes[$i] . '_value', $data); elseif ($data == "") delete_post_meta($post_id, $new_meta_boxes[$i] . '_value', get_post_meta($post_id, $new_meta_boxes[$i] . '_value', true)); } } add_action('admin_menu', 'create_meta_box'); add_action('save_post', 'save_postdata'); //注册小工具 function wpdocs_theme_slug_widgets_init() { register_sidebar(array( 'name' => __('In Left Widget Area', 'leton'), 'id' => 'in-left-widget-area', 'description' => __('A widget area located to the left', 'leton'), 'before_widget' => '
', 'after_widget' => '
', 'before_title' => '

', 'after_title' => '

', )); register_sidebar(array( 'name' => __('In Right Widget Area', 'leton'), 'id' => 'in-right-widget-area', 'description' => __('A widget area located to the right', 'leton'), 'before_widget' => '
', 'after_widget' => '
', 'before_title' => '

', 'after_title' => '

', )); } add_action('widgets_init', 'wpdocs_theme_slug_widgets_init');