· 4 years ago · Apr 06, 2021, 06:44 PM
1<?php
2/**
3 * cgz functions @link https://developer.wordpress.org/themes/basics/theme-functions/
4 * @package cgz
5 */
6
7if ( ! function_exists( 'cgz_setup' ) ) :
8 function cgz_setup() {
9 load_theme_textdomain( 'cgz', get_template_directory() . '/languages' );
10 add_theme_support( 'title-tag' );
11 add_theme_support( 'post-thumbnails' );
12 register_nav_menus(
13 array(
14 'primary' => 'Головне',
15 'foot' => 'Підвал - корисні',
16 'foottwo' => 'Підвал - ЦГЗ',
17 )
18 );
19
20 add_theme_support(
21 'html5',
22 array(
23 'search-form',
24 'comment-form',
25 'comment-list',
26 'gallery',
27 'caption',
28 'style',
29 'script',
30 )
31 );
32 // Add theme support for selective refresh for widgets.
33 //add_theme_support( 'customize-selective-refresh-widgets' );
34 }
35endif;
36add_action( 'after_setup_theme', 'cgz_setup' );
37
38//Set the content width in pixels, based on the theme's design and stylesheet.
39function cgz_content_width() {
40 $GLOBALS['content_width'] = apply_filters( 'cgz_content_width', 640 );
41}
42add_action( 'after_setup_theme', 'cgz_content_width', 0 );
43
44//Register widget area.
45
46function cgz_widgets_init() {
47 register_sidebar(
48 array(
49 'name' => esc_html__( 'Sidebar', 'cgz' ),
50 'id' => 'sidebar-right',
51 'description' => esc_html__( 'Add widgets here.', 'cgz' ),
52 'before_widget' => '<section id="%1$s" class="widget %2$s">',
53 'after_widget' => '</section>',
54 'before_title' => '<h2 class="widget-title">',
55 'after_title' => '</h2>',
56 )
57 );
58}
59add_action( 'widgets_init', 'cgz_widgets_init' );
60
61// Enqueue jqueryscripts CDN
62/*
63 function jquerycdn_method() {
64 wp_deregister_script( 'jquery' );
65 wp_register_script( 'jquery', 'https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js', false, null, true );
66 wp_enqueue_script( 'jquery' );
67}
68add_action( 'wp_enqueue_scripts', 'jquerycdn_method' );
69*/
70
71//Enqueue scripts and styles.
72function cgz_scripts() {
73 wp_enqueue_style( 'cgz-style', get_stylesheet_uri(), array(), '' );
74
75// wp_enqueue_script( 'util', get_template_directory_uri() . '/js/util.js', array(), '', true );
76// wp_enqueue_script( 'modal', get_template_directory_uri() . '/js/modal.js', array(), '', true );
77// wp_enqueue_script( 'easing', get_template_directory_uri() . '/js/easing.min.js', array(), '', true );
78// wp_enqueue_script( 'social', get_template_directory_uri() . '/js/social.js', array(), '', true );
79// wp_enqueue_script( 'lightbox', get_template_directory_uri() . '/js/lightbox.min.js', array(), '', true );
80
81 if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) {
82 wp_enqueue_script( 'comment-reply' );
83 }
84}
85add_action( 'wp_enqueue_scripts', 'cgz_scripts' );
86
87//Custom theme
88require get_template_directory() . '/inc/template-tags.php';
89require get_template_directory() . '/inc/template-functions.php';
90require get_template_directory() . '/inc/profile.php';
91
92/** head */
93remove_action( 'wp_head', 'wp_resource_hints', 2 );
94remove_action( 'wp_head', 'feed_links', 2 ); // RSS-лент записи и комментариев
95remove_action( 'wp_head', 'feed_links_extra', 3 ); // RSS-лент категорий и архивов
96remove_action( 'wp_head', 'rsd_link' ); // RSD ссылку для удаленной публикации
97remove_action( 'wp_head', 'wlwmanifest_link' ); // Windows для Live Writer
98remove_action( 'wp_head', 'wp_generator' ); // версию WordPress
99remove_action( 'wp_head', 'wp_shortlink_wp_head', 10, 0); // короткую ссылку
100remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0 ); // ссылки на предыдущую и следующую статьи
101
102// убираем баги транслитерации
103function my_cyr_to_lat_table( $ctl_table ) {
104 $ctl_table['Ы'] = 'Y';
105 $ctl_table['ы'] = 'y';
106 $ctl_table['Є'] = 'E';
107 $ctl_table['є'] = 'e';
108 $ctl_table['Ъ'] = '';
109 $ctl_table['ъ'] = '';
110 $ctl_table['Ь'] = '';
111 $ctl_table['ь'] = '';
112 return $ctl_table;
113}
114add_filter( 'ctl_table', 'my_cyr_to_lat_table' );
115
116// custom title
117/*
118add_filter( 'pre_get_document_title', function(){
119 if( is_singular() && ( $title = get_post_meta( get_the_ID(), 'cgz_titlepost', true ) ) ) {
120 return $title;
121 }
122 return '';
123} );
124*/
125
126
127// Удаляет "Рубрика: ", "Метка: " и т.д. из заголовка архива https://wp-kama.ru/function/the_archive_title
128add_filter( 'get_the_archive_title', function( $title ){
129 return preg_replace('~^[^:]+: ~', '', $title );
130});
131
132// Изменение длины обрезаемого текста
133add_filter( 'excerpt_length', function(){
134 return 20;
135} );
136
137// Читати далі…
138add_filter( 'excerpt_more', 'new_excerpt_more' );
139function new_excerpt_more( $more ){
140 global $post;
141 return ' …<br /><a class="btn-read-more" href="'. get_permalink($post) . '">Читати далі …</a>';
142}
143
144// Убираем название сайта из title
145add_filter('document_title_parts', function( $parts ){
146 if( isset($parts['site']) ) unset($parts['site']);
147 return $parts;
148});
149
150// удалим описание сайта из заголовка для главной страницы
151add_filter( 'document_title_parts', function( $title ){
152 if( isset($title['tagline']) )
153 unset( $title['tagline'] );
154 return $title;
155});
156
157// btn style comment
158add_filter( 'comment_form_defaults', function( $defaults )
159 {
160 // Edit this to your needs:
161 $button = '<input name="%1$s" type="submit" id="%2$s" class="btn btn-success %3$s" value="%4$s" />';
162 // Override the default submit button:
163 $defaults['submit_button'] = $button;
164 return $defaults;
165 }
166);
167add_filter( 'comment_form_default_fields', 'comment_form_default_add_my_fields' );
168
169// Удаляем поле "Сайт" из формы комментирования для незарегистрированных пользователей.
170function comment_form_default_add_my_fields( $fields ) {
171 unset( $fields['url'] );
172 return $fields;
173}
174
175// Удаляем стили Recentcomments
176function wpcourses_remove_recentcomments_css() {
177 global $wp_widget_factory;
178 remove_action( 'wp_head', array( $wp_widget_factory->widgets['WP_Widget_Recent_Comments'], 'recent_comments_style' ) );
179}
180add_action( 'widgets_init', 'wpcourses_remove_recentcomments_css' );
181
182// размер post - инструкция
183function customposttype_image_box() {
184 remove_meta_box('postimagediv', 'post', 'side');
185 add_meta_box('postimagediv', '730х380px max 50КБ' , 'post_thumbnail_meta_box', 'post', 'side');
186}
187add_action('do_meta_boxes', 'customposttype_image_box');
188// размер page - инструкция
189function custompagetype_image_box() {
190 remove_meta_box('postimagediv', 'page', 'side');
191 add_meta_box('postimagediv', '730х380px max 50КБ' , 'post_thumbnail_meta_box', 'page', 'side');
192}
193add_action('do_meta_boxes', 'custompagetype_image_box');
194
195
196// Отключаем виджеты
197add_action('widgets_init', 'unregister_basic_widgets' );
198function unregister_basic_widgets() {
199 unregister_widget('WP_Widget_Pages'); // Виджет страниц
200 unregister_widget('WP_Widget_Calendar'); // Календарь
201 unregister_widget('WP_Widget_Archives'); // Архивы
202// unregister_widget('WP_Widget_Links'); // Ссылки
203 unregister_widget('WP_Widget_Meta'); // Мета виджет
204// unregister_widget('WP_Widget_Search'); // Поиск
205// unregister_widget('WP_Widget_Text'); // Текст
206// unregister_widget('WP_Widget_Categories'); // Категории
207// unregister_widget('WP_Widget_Recent_Posts'); // Последние записи
208 unregister_widget('WP_Widget_Recent_Comments'); // Последние комментарии
209 unregister_widget('WP_Widget_RSS'); // RSS
210 unregister_widget('WP_Widget_Tag_Cloud'); // Облако меток
211// unregister_widget('WP_Nav_Menu_Widget'); // Меню
212 unregister_widget('WP_Widget_Media_Audio'); // Audio
213 unregister_widget('WP_Widget_Media_Video'); // Video
214// unregister_widget('WP_Widget_Media_Gallery'); // Gallery
215// unregister_widget('WP_Widget_Media_Image'); // Image
216 unregister_widget('Mega_Menu_Widget_Image_Swap');
217}
218
219
220// Удаление файлов license.txt и readme.html для защиты https://wp-kama.ru/id_7627/adminka-15-hukov-dlya-functions-php.html#avto-udalenie-license.txt-i-readme.html
221if( is_admin() && ! defined('DOING_AJAX') ){
222 add_action( 'init', 'remove_license_txt_readme_html' );
223 function remove_license_txt_readme_html(){
224
225 $license_file = ABSPATH .'/license.txt';
226 $readme_file = ABSPATH .'/readme.html';
227
228 if( file_exists($license_file) && current_user_can('manage_options') ){
229
230 $deleted = unlink($license_file) && unlink($readme_file);
231
232 if( ! $deleted )
233 $GLOBALS['readmedel'] = 'Не вдалося видалити файли: license.txt и readme.html з папки `'. ABSPATH .'`. Видаліть їх вручну!';
234 else
235 $GLOBALS['readmedel'] = 'Файли: license.txt и readme.html видалені з папки `'. ABSPATH .'`.';
236
237 add_action( 'admin_notices', function(){
238 echo '<div class="error is-dismissible"><p>'. $GLOBALS['readmedel'] .'</p></div>';
239 } );
240 }
241 }
242}
243
244
245
246
247// Отключаем панель инструментов во внешней части сайта -конфликт с запретом доступа к админке
248/*
249add_action('after_setup_theme', function(){
250 if ( ! is_admin() ) show_admin_bar( false );
251});
252*/
253
254
255// полностью отключить "Панель инструментов" в лицевой части сайта
256show_admin_bar( false );
257// запрет доступа к админке - вроде правильный
258function cgz_noadmin() {
259 if ( is_admin() && !current_user_can('manage_options' )) {
260 wp_redirect(home_url());
261 exit;
262 } }
263add_action('init', 'cgz_noadmin');
264
265
266
267// чёрный список email при регистрации
268
269function cgzuseremail_check_fields( $errors, $sanitized_user_login, $user_email ) {
270
271 if ( ! preg_match('|@mail.ru|', $_POST['user_email'] ) ) {
272 $errors->add( 'useremail_error', __( '<strong>Заборонений email</strong>', 'my_textdomain' ) );
273 }
274
275 return $errors;
276}
277
278add_filter( 'registration_errors', 'cgzuseremail_check_fields', 10, 3 );
279
280/*
281function cgzuseremail_check_fields( $errors, $sanitized_user_login, $user_email ) {
282 $blacklist = array( 'mail.ru', 'bk.ru', 'list.ru', 'inbox.ru', 'mail.ua', 'yandex.ru', 'yandex.ua', 'ya.ru', 'ya.ua' );
283
284 if ( ! preg_match( $blacklist, $_POST['user_email'] ) ) {
285 $errors->add( 'useremail_error', __( '<strong>Заборонений email</strong>', 'my_textdomain' ) );
286 }
287
288 return $errors;
289}
290
291add_filter( 'registration_errors', 'cgzuseremail_check_fields', 10, 3 );
292*/
293
294
295wp_redirect( 'http://www.example.com', 301 );
296exit;
297
298
299
300
301