Skip to content

Instantly share code, notes, and snippets.

@golubev-alex
Last active June 5, 2024 16:23
Show Gist options
  • Save golubev-alex/44341177f41d6daab9ac19aa32c6a4d9 to your computer and use it in GitHub Desktop.
Save golubev-alex/44341177f41d6daab9ac19aa32c6a4d9 to your computer and use it in GitHub Desktop.
// пагинация
get_the_posts_pagination();
// канониклы при пагинации
add_filter('wpseo_canonical', 'removeCanonicalArchivePagination');
function removeCanonicalArchivePagination($link) {
$link = preg_replace('#\\??/page[\\/=]\\d+#', '', $link);
return $link;
}
// удаляем /category/ из урла
function remove_category( $string, $type ) {
if ( $type != 'single' && $type == 'category' && ( strpos( $string, 'category' ) !== false ) ) {
$url_without_category = str_replace( "/category/", "/", $string );
return trailingslashit( $url_without_category );
}
return $string;
}
add_filter( 'user_trailingslashit', 'remove_category', 100, 2);
// мобильное ли устройство?
if( wp_is_mobile() ){ ... }
// Получить все меню
wp_get_nav_menus()
// получить контент с тегами p
wpautop( $post->post_content )
// получить кастомные таксономии
$cat_args = array(
'orderby' => 'term_id',
'order' => 'ASC',
'hide_empty' => true,
);
$departments = get_terms('cust_team_departments', $cat_args);
// получить посты с проверкой на наличие результатов
$args = array(
'post_type' => 'cust_docs',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'cust_team_departments',
'field' => 'term_id',
'terms' => $department->term_id
)
);
$query = new WP_Query( $args );
if($query->post_count):
while ( $query->have_posts() ):
$query->the_post();
endwhile;
endif;
// для сортировки по просмотрам (включая те, у которых просмотров нет)
$args = array(
'posts_per_page' => 5,
'post_type' => 'cust_blog',
'meta_query' => [
'relation' => 'OR',
['key' => 'views', 'compare' => 'EXISTS'], // this comes first!
['key' => 'views', 'compare' => 'NOT EXISTS'],
],
'orderby' => 'meta_value_num',
'order' => 'DESC'
);
$query = new WP_Query( $args );
// редирект на нижний регистр
function custom_changed_url()
{
global $wp_query;
if ($wp_query->is_main_query() && !is_admin()) {
// Grab requested URL
$s_url = $_SERVER['REQUEST_URI'];
$params = $_SERVER['QUERY_STRING'];
// Исключаем содержание файлов
if (preg_match('/[\.]/', $s_url)) {
return;
}
$cc_url = $s_url;
// Если ссылка содержит заглавные буквы
if (preg_match('/[A-Z]/', $s_url)) {
// Заменяем заглавные буквы на маленькие
$cc_url = empty($params) ? strtolower($s_url) : strtolower(substr($s_url, 0, strrpos($s_url, '?'))) . '?' . $params;
}
if ($cc_url !== $s_url) {
// 301 редирект
header('Location: ' . $cc_url, TRUE, 301);
exit();
}
}
}
add_action('init', 'custom_changed_url');
// замена input[type="submit"] на button в contact form 7
remove_action('wpcf7_init', 'wpcf7_add_form_tag_submit', 10);
add_action( 'wpcf7_init', 'wpcf7_add_form_tag_submit_custom' );
function wpcf7_add_form_tag_submit_custom() {
wpcf7_add_form_tag( 'submit', 'wpcf7_submit_form_tag_handler_custom' );
}
function wpcf7_submit_form_tag_handler_custom( $tag ) {
$class = wpcf7_form_controls_class( $tag->type, 'has-spinner' );
$atts = array();
$atts['class'] = $tag->get_class_option( $class );
$atts['id'] = $tag->get_id_option();
$atts['tabindex'] = $tag->get_option( 'tabindex', 'signed_int', true );
$value = isset( $tag->values[0] ) ? $tag->values[0] : '';
if ( empty( $value ) ) {
$value = __( 'Send', 'contact-form-7' );
}
$atts['type'] = 'submit';
$atts['value'] = $value;
$atts = wpcf7_format_atts( $atts );
$html = sprintf( '<button %1$s>%2$s</button>', $atts, $value );
return $html;
}
// для удаления миниатюры и картинок из контента поста при удалении самого поста
function wpschool_remove_attachment_with_post( $post_id ) {
if( has_post_thumbnail( $post_id ) ) {
$attachment_id = get_post_thumbnail_id( $post_id );
wp_delete_attachment( $attachment_id, true );
}
}
add_action( 'before_delete_post', 'wpschool_remove_attachment_with_post', 10 );
add_action( 'before_delete_post', function( $id ) {
$attachments = get_attached_media( '', $id );
foreach ( $attachments as $attachment ) {
wp_delete_attachment( $attachment->ID, 'true' );
}
});
// замена пути при импорте
UPDATE wp_options SET option_value = REPLACE(option_value, 'http://test.truemisha.ru', 'https://misha.agency') WHERE option_name = 'home' OR option_name = 'siteurl';UPDATE wp_posts SET post_content = REPLACE (post_content, 'http://test.truemisha.ru', 'https://misha.agency');
UPDATE wp_postmeta SET meta_value = REPLACE (meta_value, 'http://test.truemisha.ru','https://misha.agency');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment