Полезные строки кода для WordPress
Главная страница › Блог › Для разработчиков › Полезные строки кода для WordPress
Регистрируем и выводим свой сайдбар
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | function wj3_blog_sidebar() { register_sidebar( array( 'id' => 'sidebar', // уникальный id 'name' => 'Сайдбар в блоге', // название сайдбара 'description' => 'Перетащите сюда виджеты, чтобы добавить их в сайдбар.', // описание 'before_widget' => '<section id="%1$s" class="widget %2$s">', // по умолчанию виджеты выводятся <li>-списком 'after_widget' => '</section>', 'before_title' => '<h3 class="widget-title">', // по умолчанию заголовки виджетов в <h2> 'after_title' => '</h3>' ) ); } add_action( 'widgets_init', 'wj3_blog_sidebar' ); // вывод сайдбара (в файле sidebar.php) <?php if(is_active_sidebar('sidebar')){ ?> <?php dynamic_sidebar('sidebar'); ?> <?php } ?> |
Откладываем загрузку dashicons
1 2 3 4 | add_action( 'wp_print_styles', 'my_deregister_styles', 100 ); function my_deregister_styles() { wp_dequeue_style( 'dashicons' ); } |
Отключаем Emojis
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | 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_filter('wp_resource_hints', 'disable_emojis_remove_dns_prefetch', 10, 2); } add_action('init', 'disable_emojis'); function disable_emojis_tinymce($plugins){ if (is_array($plugins)){ return array_diff( $plugins, array( 'wpemoji' ) ); } return array(); } function disable_emojis_remove_dns_prefetch($urls, $relation_type){ if ('dns-prefetch' == $relation_type){ $emoji_svg_url_bit = 'https://s.w.org/images/core/emoji/'; foreach ($urls as $key => $url){ if (strpos( $url, $emoji_svg_url_bit) !== false){ unset($urls[$key]); } } } return $urls; } |
Ссылка на тему WordPress
1 | <?php bloginfo('template_url'); ?> |
Ссылка на главную страницу WordPress
1 | <?php echo get_site_url(); ?> |
Скрываем версию WordPress
1 | remove_action('wp_head', 'wp_generator'); |
Удаляем атрибут type у скриптов и стилей
1 2 3 4 5 | function wj3_remove_attr($tag, $handle) { return preg_replace( "/type=['\"]text\/(javascript|css)['\"]/", '', $tag ); } add_filter('style_loader_tag', 'wj3_remove_attr', 10, 2); add_filter('script_loader_tag', 'wj3_remove_attr', 10, 2); |
Отключаем верхний бар
1 | show_admin_bar(false); |
Включаем поддержку миниатюр для записей
1 | add_theme_support('post-thumbnails'); |
Подключаем скрипт ответа на комментарии
1 2 3 4 5 | function enqueue_comment_reply() { if( is_singular() && comments_open() && (get_option('thread_comments') == 1) ) wp_enqueue_script('comment-reply'); } add_action( 'wp_enqueue_scripts', 'enqueue_comment_reply' ); |
Удаляем поле «сайт» из комментариев
1 2 3 4 5 | function wj3_remove_urlcomments( $fields ) { unset( $fields['url'] ); return $fields; } add_filter( 'comment_form_default_fields', 'wj3_remove_urlcomments', 10, 1); |
Добавляем placeholder в поля комментариев
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | function wj3_add_placeholder_comments($fields) { $commenter = wp_get_current_commenter(); $req = get_option( 'require_name_email' ); $aria_req = ( $req ? " aria-required='true'" : '' ); $fields['author'] = '<p class="comment-form-author"> <input required minlength="3" maxlength="30" placeholder="Ваше имя..." id="author" name="author" type="text" value="' . esc_attr( $commenter['comment_author'] ) .'" size="30"' . $aria_req . ' /> </p>'; $fields['email'] = '<p class="comment-form-email"> <input required placeholder="Ваш e-mail..." maxlength="100" id="email" name="email" type="email" value="' . esc_attr( $commenter['comment_author_email'] ) .'" size="30"' . $aria_req . ' /> </p>'; return $fields; } add_filter('comment_form_default_fields','wj3_add_placeholder_comments'); |
Добавляем placeholder в textarea комментариев
1 2 3 4 5 6 7 8 | function wj3_add_placeholder_comments_textarea($comment_field) { $comment_field = '<p class="comment-form-comment"> <textarea required id="comment" placeholder="Ваш комментарий..." name="comment" cols="45" rows="8" maxlength="65525"></textarea> </p>'; return $comment_field; } add_filter('comment_form_field_comment','wj3_add_placeholder_comments_textarea'); |
Переносим textarea комментариев вниз
1 2 3 4 5 6 7 | function wj3_move_comments_textarea( $fields ) { $comment_field = $fields['comment']; unset( $fields['comment'] ); $fields['comment'] = $comment_field; return $fields; } add_filter( 'comment_form_fields', 'wj3_move_comments_textarea' ); |
Удаляем H2 из шаблона пагинации
1 2 3 4 5 6 7 8 | function unset_title_navigation_template( $template, $class ){ return ' <nav class="navigation %1$s" role="navigation"> <div class="nav-links">%3$s</div> </nav> '; } add_filter('navigation_markup_template', 'unset_title_navigation_template', 10, 2 ); |
Удаляем role из пагинации
1 2 3 4 5 6 7 8 | add_filter( 'navigation_markup_template', 'unset_role_pagination' ); function unset_role_pagination( $template ) { $template = ' <nav class="navigation %1$s"> <div class="nav-links">%3$s</div> </nav>'; return $template; } |
Свой шорткод
1 2 3 4 5 6 7 | add_shortcode( 'название-шорткода', 'func_name' ); function func_name( $atts ) { ob_start(); // код для вывода $myvariable = ob_get_clean(); return $myvariable; } |
Поделиться: