Skip to content

Instantly share code, notes, and snippets.

@core2duo
Last active October 13, 2022 15:12
Show Gist options
  • Star 20 You must be signed in to star a gist
  • Fork 10 You must be signed in to fork a gist
  • Save core2duo/4fdaa66ae0f0ca220e4c to your computer and use it in GitHub Desktop.
Save core2duo/4fdaa66ae0f0ca220e4c to your computer and use it in GitHub Desktop.
Some useful WP tricks
<?php
/** Hide admin bar */
add_filter('show_admin_bar', '__return_false');
/** Custom favicon for debug mode (for easier difference) */
function debug_favicon(){
if (WP_DEBUG) {
echo '<link rel="icon" type="image/png" href="/favicon-production.png">';
}
}
add_action('admin_head', 'debug_favicon');
add_action('wp_head', 'debug_favicon');
/** Turn on thumbnails */
add_theme_support('post-thumbnails');
/** Turn on menu */
function register_my_menu() {
register_nav_menus(array(
'menu' => 'Menu'
));
}
add_action('init', 'register_my_menu');
/** Empty search string fix */
function my_request_filter($query_vars) {
if(isset($_GET['s']) && empty($_GET['s'])) {
$query_vars['s'] = " ";
}
return $query_vars;
}
add_filter('request', 'my_request_filter');
/** Set first and last <li> classes in menus */
function add_first_and_last($items) {
$parents = array();
foreach ($items as $key => $item) {
if (! array_key_exists($item->menu_item_parent, $parents)) {
$item->classes []= 'first-menu-item';
} else {
array_pop($items[$parents[$item->menu_item_parent]]->classes);
}
$parents[$item->menu_item_parent] = $key;
$item->classes []= 'last-menu-item';
}
return $items;
}
add_filter('wp_nav_menu_objects', 'add_first_and_last');
/** Add "attachment" class to images in WP editor */
function add_class_attachment_link($html){
$html = str_replace('<a ', '<a class="attachment" ', $html);
return $html;
}
add_filter ('media_send_to_editor', 'add_class_attachment_link');
/** Remove default size&cols from CF7 forms */
function remove_default_size($output) {
$output = preg_replace('/size=\"40\"/', '', $output);
$output = preg_replace('/cols=\"40\"/', '', $output);
return $output;
}
add_filter('wpcf7_form_elements', 'remove_default_size');
/** Remove spaces between <li> in menus */
function remove_spaces($output) {
$output = preg_replace('#<\/li>\s<li#iu', '</li><li', $output);
return $output;
}
add_filter('wp_nav_menu', 'remove_spaces');
/** Gets post cat slug and looks for single-[cat slug].php and applies it */
function single_template_filter ($template) {
foreach(get_the_category() as $cat) {
if (file_exists(TEMPLATEPATH . "/single-{$cat->slug}.php")) {
return TEMPLATEPATH . "/single-{$cat->slug}.php";
}
}
return $template;
}
add_filter('single_template', 'single_template_filter');
function pre ($array) {
echo '<pre>';
print_r($array);
echo '</pre>';
}
/** Smart excerpt for content */
mb_internal_encoding('UTF-8');
function smart_excerpt ($content, $symbols = 200) {
if (mb_strlen($content) < $symbols) {
return $content;
}
$content = mb_substr($content, 0, $symbols);
$content = mb_substr($content, 0, mb_strrpos($content, ' '));
$content .= '...';
return $content;
}
/** Add alts to images depends on the title */
function add_image_alts($content) {
// Remove alt that already exists
$content = preg_replace('#<img(.*?)(alt=\"(.*?)\")(.*?)>#iu', "<img$1$4>", $content);
// Add new alt depends on the title
$content = preg_replace('#<img(.*?)>#iu', "<img$1 alt=\"{$GLOBALS['post']->post_title}\">", $content);
return $content;
}
add_filter('the_content', 'add_image_alts');
add_filter('post_thumbnail_html', 'add_image_alts');
/** Remove width & height images attributes */
function remove_images_width_height($content) {
$content = preg_replace('/<img(.*?)(width=\"\d+\")(.*?)>/iu', "<img$1 $3>", $content);
$content = preg_replace('/<img(.*?)(height=\"\d+\")(.*?)>/iu', "<img$1 $3>", $content);
return $content;
}
add_filter('the_content', 'remove_images_width_height');
add_filter('post_thumbnail_html', 'remove_images_width_height');
/** Breadcrumbs */
function breadcrumbs() {
$text['home'] = 'Главная'; // текст ссылки "Главная"
$text['category'] = '%s'; // текст для страницы рубрики
$text['search'] = 'Результаты поиска по запросу "%s"'; // текст для страницы с результатами поиска
$text['tag'] = 'Записи с тегом "%s"'; // текст для страницы тега
$text['author'] = 'Статьи автора %s'; // текст для страницы автора
$text['404'] = 'Ошибка 404'; // текст для страницы 404
$show_current = 1; // 1 - показывать название текущей статьи/страницы/рубрики, 0 - не показывать
$show_on_home = 0; // 1 - показывать "хлебные крошки" на главной странице, 0 - не показывать
$show_home_link = 1; // 1 - показывать ссылку "Главная", 0 - не показывать
$show_title = 1; // 1 - показывать подсказку (title) для ссылок, 0 - не показывать
$delimiter = ' &rarr; '; // разделить между "крошками"
$before = '<span class="current">'; // тег перед текущей "крошкой"
$after = '</span>'; // тег после текущей "крошки"
global $post;
$home_link = home_url('/');
$link_before = '';
$link_after = '';
$link_attr = '';
$link = $link_before . '<a' . $link_attr . ' href="%1$s">%2$s</a>' . $link_after;
$parent_id = $parent_id_2 = isset($post) ? $post->post_parent : 0;
$frontpage_id = get_option('page_on_front');
if (is_home() || is_front_page()) {
if ($show_on_home == 1) echo '<span class="current">' . $text['home'] . '</span>';
} else {
if ($show_home_link == 1) {
echo sprintf($link, $home_link, $text['home']);
if ($frontpage_id == 0 || $parent_id != $frontpage_id) echo $delimiter;
}
if ( is_category() ) {
$this_cat = get_category(get_query_var('cat'), false);
if ($this_cat->parent != 0) {
$cats = get_category_parents($this_cat->parent, TRUE, $delimiter);
if ($show_current == 0) $cats = preg_replace("#^(.+)$delimiter$#", "$1", $cats);
$cats = str_replace('<a', $link_before . '<a' . $link_attr, $cats);
$cats = str_replace('</a>', '</a>' . $link_after, $cats);
if ($show_title == 0) $cats = preg_replace('/ title="(.*?)"/', '', $cats);
echo $cats;
}
if ($show_current == 1) echo $before . sprintf($text['category'], single_cat_title('', false)) . $after;
} elseif ( is_search() ) {
echo $before . sprintf($text['search'], get_search_query()) . $after;
} elseif ( is_day() ) {
echo sprintf($link, get_year_link(get_the_time('Y')), get_the_time('Y')) . $delimiter;
echo sprintf($link, get_month_link(get_the_time('Y'),get_the_time('m')), get_the_time('F')) . $delimiter;
echo $before . get_the_time('d') . $after;
} elseif ( is_month() ) {
echo sprintf($link, get_year_link(get_the_time('Y')), get_the_time('Y')) . $delimiter;
echo $before . get_the_time('F') . $after;
} elseif ( is_year() ) {
echo $before . get_the_time('Y') . $after;
} elseif ( is_single() && !is_attachment() ) {
if ( get_post_type() != 'post' ) {
$post_type = get_post_type_object(get_post_type());
$slug = $post_type->rewrite;
printf($link, $home_link . '/' . $slug['slug'] . '/', $post_type->labels->singular_name);
if ($show_current == 1) echo $delimiter . $before . get_the_title() . $after;
} else {
$cat = get_the_category(); $cat = $cat[0];
if ($cat->cat_ID != 1) { //Не показывать категорию "Без рубрики"
$cats = get_category_parents($cat, TRUE, $delimiter);
if ($show_current == 0) $cats = preg_replace("#^(.+)$delimiter$#", "$1", $cats);
$cats = str_replace('<a', $link_before . '<a' . $link_attr, $cats);
$cats = str_replace('</a>', '</a>' . $link_after, $cats);
if ($show_title == 0) $cats = preg_replace('/ title="(.*?)"/', '', $cats);
echo $cats;
}
if ($show_current == 1) echo $before . get_the_title() . $after;
}
} elseif ( !is_single() && !is_page() && get_post_type() != 'post' && !is_404() ) {
$post_type = get_post_type_object(get_post_type());
echo $before . $post_type->labels->singular_name . $after;
} elseif ( is_attachment() ) {
$parent = get_post($parent_id);
$cat = get_the_category($parent->ID); $cat = $cat[0];
$cats = get_category_parents($cat, TRUE, $delimiter);
$cats = str_replace('<a', $link_before . '<a' . $link_attr, $cats);
$cats = str_replace('</a>', '</a>' . $link_after, $cats);
if ($show_title == 0) $cats = preg_replace('/ title="(.*?)"/', '', $cats);
echo $cats;
printf($link, get_permalink($parent), $parent->post_title);
if ($show_current == 1) echo $delimiter . $before . get_the_title() . $after;
} elseif ( is_page() && !$parent_id ) {
if ($show_current == 1) echo $before . get_the_title() . $after;
} elseif ( is_page() && $parent_id ) {
if ($parent_id != $frontpage_id) {
$breadcrumbs = array();
while ($parent_id) {
$page = get_page($parent_id);
if ($parent_id != $frontpage_id) {
$breadcrumbs[] = sprintf($link, get_permalink($page->ID), get_the_title($page->ID));
}
$parent_id = $page->post_parent;
}
$breadcrumbs = array_reverse($breadcrumbs);
for ($i = 0; $i < count($breadcrumbs); $i++) {
echo $breadcrumbs[$i];
if ($i != count($breadcrumbs)-1) echo $delimiter;
}
}
if ($show_current == 1) {
if ($show_home_link == 1 || ($parent_id_2 != 0 && $parent_id_2 != $frontpage_id)) echo $delimiter;
echo $before . get_the_title() . $after;
}
} elseif ( is_tag() ) {
echo $before . sprintf($text['tag'], single_tag_title('', false)) . $after;
} elseif ( is_author() ) {
global $author;
$userdata = get_userdata($author);
echo $before . sprintf($text['author'], $userdata->display_name) . $after;
} elseif ( is_404() ) {
echo $before . $text['404'] . $after;
}
if ( get_query_var('paged') ) {
if ( is_category() || is_day() || is_month() || is_year() || is_search() || is_tag() || is_author() ) echo ' (';
echo __('Page') . ' ' . get_query_var('paged');
if ( is_category() || is_day() || is_month() || is_year() || is_search() || is_tag() || is_author() ) echo ')';
}
}
}
@tashemi
Copy link

tashemi commented Aug 13, 2014

Is it typo in the name of func degub_favicon()?

@core2duo
Copy link
Author

Yeah, it is. Fixed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment