Skip to content

Instantly share code, notes, and snippets.

@VicVicos
Last active November 22, 2021 18:57
Show Gist options
  • Select an option

  • Save VicVicos/b7322fe04a5821e3f098206202d431fa to your computer and use it in GitHub Desktop.

Select an option

Save VicVicos/b7322fe04a5821e3f098206202d431fa to your computer and use it in GitHub Desktop.
WP functions
<?php
function get_posts_from_category($idCat) {
// параметры по умолчанию
$args = array(
'numberposts' => -1,
'category' => $idCat,
'orderby' => 'date',
'order' => 'DESC',
'include' => array(),
'exclude' => array(),
'meta_key' => '',
'meta_value' =>'',
'post_type' => 'post',
'suppress_filters' => true,
);
$posts = get_posts( $args );
wp_reset_postdata();
return $posts;
}
function do_post($id) {
remove_filter( 'the_content', 'wpautop' );
$data = get_post($id);
$arrayChar = array('\n', '\r\n', '\r', chr(10), chr(13));
return apply_filters('the_content', str_replace($arrayChar, '', trim($data->post_content)));
}
// Отключение форматирования
function my_formatter($content) {
$new_content = '';
$pattern_full = '{(\[raw\].*?\[/raw\])}is';
$pattern_contents = '{\[raw\](.*?)\[/raw\]}is';
$pieces = preg_split($pattern_full, $content, -1, PREG_SPLIT_DELIM_CAPTURE);
foreach ($pieces as $piece) {
if (preg_match($pattern_contents, $piece, $matches)) {
$new_content .= $matches[1];
} else {
$new_content .= wptexturize(wpautop($piece));
}
}
return $new_content;
}
remove_filter('the_content', 'wpautop');
remove_filter('the_content', 'wptexturize');
add_filter('the_content', 'my_formatter', 99);
//***
//Add valid elemets tynyMCE
function filter_tiny_mce_before_init( $options ) {
if ( ! isset( $options['extended_valid_elements'] ) ) {
$options['extended_valid_elements'] = '';
} else {
$options['extended_valid_elements'] .= ',';
}
if ( ! isset( $options['custom_elements'] ) ) {
$options['custom_elements'] = '';
} else {
$options['custom_elements'] .= ',';
}
$options['extended_valid_elements'] .= 'div[contenteditable|class|id|style],';
$options['custom_elements'] .= 'div[contenteditable|class|id|style],';
$options['extended_valid_elements'] .= 'a[href|target=_blank],strong/b,div[align],br,noindex';
$options['custom_elements'] .= 'a[href|target=_blank],strong/b,div[align],br,noindex';
return $options;
}
add_filter('tiny_mce_before_init', 'filter_tiny_mce_before_init');
// ***
// Скрипты и стили
function theme_scripts() {
wp_enqueue_script('jquery');
wp_enqueue_script('bootstrap', 'https://cdn.jsdelivr.net/bootstrap/3.3.7/js/bootstrap.min.js', array('jquery'), '3.3.7', true);
wp_enqueue_script('slick', get_template_directory_uri() . '/assets/js/slick.js', array('jquery'), '1.6.0', true);
wp_enqueue_script('fancybox', get_template_directory_uri() . '/assets/js/jquery.fancybox.min.js', array('jquery'), '1.0.0', true);
wp_enqueue_script('scripts', get_template_directory_uri() . '/assets/js/script.min.js', array('jquery'), '1.0.0', true);
wp_enqueue_script('forSeo', get_template_directory_uri() . '/assets/js/forSeo.js', array('jquery'), '1.0.0', true);
wp_enqueue_script('respond', 'https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js', array('jquery'), '1.0.0', true);
wp_enqueue_script('html5shiv', 'https://oss.maxcdn.com/libs/html5shiv/3.7.2/html5shiv.js', array('jquery'), '1.0.0', true);
wp_script_add_data( 'respond', 'conditional', 'lt IE 9' );
wp_script_add_data( 'html5shiv', 'conditional', 'lt IE 9' );
//
wp_enqueue_style('fonts', 'https://fonts.googleapis.com/css?family=Roboto|Roboto+Slab:700|Ubuntu:400,400i,700');
wp_enqueue_style('bootstrap', 'https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css');
wp_enqueue_style('font-awesome', 'https://maxcdn.bootstrapcdn.com/font-awesome/4.4.0/css/font-awesome.min.css');
wp_enqueue_style('slick', get_template_directory_uri() . '/assets/css/slick.css');
wp_enqueue_style('bootstrap', get_template_directory_uri() . '/jquery.fancybox.min.css');
wp_enqueue_style( 'styles', get_template_directory_uri() . '/assets/css/styles.min.css');
wp_enqueue_style( 'overrite', get_template_directory_uri() . '/assets/css/overrite.css');
//wp_enqueue_style('fonts-rouble', 'http://fonts.googleapis.com/css?family=PT+Sans:regular,italic,bold,bolditalic');
}
add_action('wp_enqueue_scripts', 'theme_scripts');
// Add defer attribute to scripts
function defer_scripts($tag, $handle, $src) {
$handleList = array(
'template_front_libs_app'
);
if (in_array($handle, $handleList)) {
return str_replace(' src', ' defer src', $tag);
}
return $tag;
}
add_filter('script_loader_tag', 'defer_scripts', 10, 3);
// Add the preload attribute to the style files.
function preload_style($tag, $handle, $href, $media) {
$handleList = array(
'template_frontend_libs_style',
);
$preloadTag = "";
if (in_array($handle, $handleList)) {
$preloadTag = "<link rel=\"preload\" href=\"" . $href . "\" as=\"style\" media=\"" . $media . "\" />";
}
return $preloadTag . $tag;
}
add_filter('style_loader_tag', 'preload_style', 10, 4);
// Добавляем атрибут defer к скриптам
function defer_scripts($tag, $handle, $src) {
$handleList = [
//list handles
];
if (in_array($handle, $handleList)) {
// return str_replace( ' src', ' async src', $tag );
return str_replace(' src', ' defer src', $tag);
}
return $tag;
}
add_filter('script_loader_tag', 'defer_scripts', 10, 3);
// Добавляем атрибут preload к файлам стилей
function preload_style($tag, $handle, $href, $media) {
$handleList = [
//list handles
];
if (in_array($handle, $handleList)) {
$preloadTag = "<link rel=\"preload\" href=\"" . $href . "\" as=\"style\" />";
}
return $preloadTag . $tag;
}
add_filter('style_loader_tag', 'preload_style', 10, 4);
function remove_trash_script () {
// Отрубаем скрипты
}
function remove_trash_styles () {
$arrStyleTrash = array('genericons','jetpack_css');
// Отрубаем стили
wp_deregister_style($arrStyleTrash);
}
//add_filter( 'wp_print_scripts', 'remove_trash_script', 40);
//add_filter( 'wp_print_styles', 'remove_trash_styles', 40);
register_nav_menus(array(
'index' => __('Меню на главной', 'indexMenu'),
// 'sidebar' => __('Меню в sidebar', 'sidebarMenu'),
));
add_theme_support('post-thumbnails');
//add_action('wp_head', 'add_scriptmetrics', 99);
add_action('wp_footer', 'add_scriptmetrics', 99);
function add_scriptmetrics(){
echo get_option_theme('yandex_metrics');
echo get_option_theme('google_analytics');
}
if( extension_loaded('zlib') && ini_get('output_handler') != 'ob_gzhandler' ){
add_action('wp', function(){ @ ob_end_clean(); @ ini_set('zlib.output_compression', 'on'); } );
}
add_filter( 'plugin_action_links', 'set_required_plugins', 10, 2 );
public function set_required_plugins($actions, $plugin_file)
{
// Удаляет действие "Редактировать" у всех плагинов
unset( $actions['edit'] );
// Удаляет действие "Деактивировать" у важных для сайта плагинов
$important_plugins = array(
'carbon-fields/carbon-fields-plugin.php',
'kama-thumbnail/kama_thumbnail.php',
'contact-form-7/wp-contact-form-7.php',
'vbv_breadcrumbs-master/vbv_breadcrumbs.php',
'slick-slider-itb/slick-slider-itb.php',
'itb-settings/itb-settings.php',
);
if ( in_array( $plugin_file, $important_plugins ) ) {
unset( $actions['deactivate'] );
}
return $actions;
}
// Информация кол-ве запросов и используемой памяти
add_filter('wp_footer', 'performance');
function performance() {
$stat = sprintf('SQL: %d за %.3f sec. %.2f MB', get_num_queries(), timer_stop(0, 3), (memory_get_peak_usage() / 1024 / 1024) );
echo "<!-- $stat -->"; // скрыто
//echo $stat;
}
add_filter('admin_footer_text', 'wp_usage');
function wp_usage() {
printf( ('SQL: %d за %s сек. '), get_num_queries(), timer_stop(0, 3) );
if ( function_exists('memory_get_usage') ) echo round( memory_get_usage()/1024/1024, 2 ) . ' mb ';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment