Skip to content

Instantly share code, notes, and snippets.

@renakdup
Last active December 12, 2022 23:59
Show Gist options
  • Save renakdup/030aed2c3910b4ae3984dfb45c0ff838 to your computer and use it in GitHub Desktop.
Save renakdup/030aed2c3910b4ae3984dfb45c0ff838 to your computer and use it in GitHub Desktop.
WordPress
<?php
// Wordpress Disable Comments (add to function.php)
add_action('admin_init', function () {
// Redirect any user trying to access comments page
global $pagenow;
if ($pagenow === 'edit-comments.php') {
wp_redirect(admin_url());
exit;
}
// Remove comments metabox from dashboard
remove_meta_box('dashboard_recent_comments', 'dashboard', 'normal');
// Disable support for comments and trackbacks in post types
foreach (get_post_types() as $post_type) {
if (post_type_supports($post_type, 'comments')) {
remove_post_type_support($post_type, 'comments');
remove_post_type_support($post_type, 'trackbacks');
}
}
});
// Close comments on the front-end
add_filter('comments_open', '__return_false', 20, 2);
add_filter('pings_open', '__return_false', 20, 2);
// Hide existing comments
add_filter('comments_array', '__return_empty_array', 10, 2);
// Remove comments page in menu
add_action('admin_menu', function () {
remove_menu_page('edit-comments.php');
});
// Remove comments links from admin bar
add_action('init', function () {
if (is_admin_bar_showing()) {
remove_action('admin_bar_menu', 'wp_admin_bar_comments_menu', 60);
}
});
<?php // enqueues script and style
function rs_enqueues() {
wp_register_style( 'rs_bootstrap', get_template_directory_uri() . '/css/bootstrap-custom.css', false, null );
wp_enqueue_style( 'rs_bootstrap' );
wp_register_script( 'rs_masonry', get_template_directory_uri() . '/js/masonry.pkgd.js', array( 'jquery' ), '1.0.8', true );
wp_enqueue_script( 'rs_masonry' );
wp_deregister_script( 'jquery' );
wp_deregister_script( 'jquery-migrate' );
$inline_style = "
.class{
background-color: ....;
}
";
wp_add_inline_style( 'rs_main', $inline_style );
}
add_action( 'wp_enqueue_scripts', 'rs_enqueues', 100 );
<?php /**
* Function substr the excerpt by type.
*
* @param $type string Type for substr string.
* @param $post post element
*/
function rs_excerpt_length( $type, $post = false ) {
if ( ! $post ) {
global $post;
}
$string = get_the_excerpt( $post );
if ( 'news' == $type ) {
if ( strlen( $string ) > 80 ) {
$string = mb_substr( $string, 0, 80, 'UTF-8' ) . '...';
}
} elseif ( 'bignews' == $type ) {
if ( strlen( $string ) > 99 ) {
$string = mb_substr( $string, 0, 99, 'UTF-8' ) . '...';
}
}
echo $string;
}
<?php
/**
* Функция для замены ссылки на папку UPLOADS для localhost
**/
function hh_replace_upload_url_path( $option ) {
global $hg_currnet_region;
if ( $_SERVER[ 'REMOTE_ADDR' ] === '127.0.0.1' ) {
return 'http://site.ru/wp-content/uploads';
}
}
add_filter( 'pre_option_upload_url_path', 'hh_replace_upload_url_path' );
<?php
//Pagination navigation for archives of posts
function rs_pagination( $wp_query = false, $query_var = 'paged', $anchor_link = '' ) {
if ( ! $wp_query ) {
global $wp_query;
}
if ( $anchor_link ) {
$anchor_link = '#' . $anchor_link;
}
$big = 999999999; // need an unlikely integer
$current = max( 1, get_query_var( $query_var ) );
$links = paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) . $anchor_link ) ),
'format' => '?' . $query_var . '=%#%',
'current' => $current,
'prev_text' => '<span class="glyphicon glyphicon-menu-left"></span>Назад',
'next_text' => 'Вперед <span class="glyphicon glyphicon-menu-right"></span>',
'total' => $wp_query->max_num_pages,
) );
/*if ($current != 1) {
$links = '<a class="first page-numbers" href="' . esc_url( get_pagenum_link( 1 ) . $anchor_link ) . '"> Назад</a>' . $links;
}
if ($current != $wp_query->max_num_pages) {
$links = $links . '<a class="first page-numbers" href="' . esc_url( get_pagenum_link( $wp_query->max_num_pages ) . $anchor_link ) . '">Вперед <span class="glyphicon glyphicon-menu-right"></span></a>';
}*/
$links = str_replace( '?' . $query_var . '=1', '', $links );
return $links;
}
<?php
// Заменяет битые ссылки на кратинки в WordPress на заглушки.
// Удобно, например на тестовом сайте, чтобы не переносить фотки с боевого.
namespace Renak;
// Инициализация
if (! defined('RENAK_IMAGE_PLACEHOLDER') && RENAK_IMAGE_PLACEHOLDER === true) {
add_action('init', [ImagePlaceholder::class, 'init']);
}
/**
* Class ImagePlaceholder
* Класс заполняющий отсутствующие изображения на изображения заглушки
*/
class ImagePlaceholder
{
public static function init()
{
add_filter('wp_get_attachment_image_src', [self::class, 'set_placeholder']);
}
/**
* Заменяем отсутствующие изображения на заглушки
*
* @param array $image
*
* @return mixed
*/
public static function set_placeholder($image)
{
if (! $image) {
return $image;
}
$src = $image[0];
$width = $image[1];
$height = $image[2];
// Переводим абсолютный URL в относительный путь по серверу.
$path = $_SERVER['DOCUMENT_ROOT'] . wp_make_link_relative($src);
if (! file_exists($path)) {
$image[0] = self::get_image_placeholder($width, $height);
}
return $image;
}
/**
* Получаем ссылку на изображения заглушку
*
* @param $width
* @param $height
*
* @return string
*/
public static function get_image_placeholder($width, $height)
{
$images_type = [
'animals',
'arch',
'nature',
'people',
'tech',
'any',
];
$images_filter = [
'', // without filter
'grayscale', // Черно-белые
'sepia', // Sepia
];
$image_url = "https://placeimg.com/{$width}/{$height}/" .
array_rand($images_type) . '/' .
array_rand($images_filter);
return $image_url;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment