Skip to content

Instantly share code, notes, and snippets.

@campusboy87
campusboy87 / replace_thumbnail.php
Last active December 14, 2018 12:45
Если отсутствует миниатюра записи, то подменяет её другой миниатюрой с указанным ID.
<?php
function replace_thumbnail( $id, $object_id, $meta_key ) {
if ( '_thumbnail_id' === $meta_key && 'post' === get_post_type( $object_id ) ) {
remove_filter( 'get_post_metadata', __FUNCTION__ );
$default_id_img = 91;
$id = has_post_thumbnail( $object_id ) ? $id : $default_id_img;
<?php
function wpcf7_remove_assets() {
add_filter( 'wpcf7_load_js', '__return_false' );
add_filter( 'wpcf7_load_css', '__return_false' );
}
add_action( 'wpcf7_init', 'wpcf7_remove_assets' );
function wpcf7_add_assets( $atts ) {
<?php
$data = wp_remote_get( 'https://www.tvasites.com/cmswebparts/insite3/api/exporttowordpress.aspx' );
$data = wp_remote_retrieve_body( $data );
$output = [];
if ( preg_match_all( '/{([^}]*)}/', $data, $matches ) ) {
foreach ( $matches[1] as $item ) {
$lines = preg_split( '/\\r\\n?|\\n/', $item, null, PREG_SPLIT_NO_EMPTY );
<?php
function cr_get_data_crypto( $type = '', $symbol = '' ) {
$symbol = $symbol ? $symbol : cr_get_crypto_symbol();
$cache_key = $symbol . '-coinsnapshot';
$data = wp_cache_get( $cache_key );
if ( $symbol && false === $data ) {
$file = CRYPT_REVIEWS_PATH . "data-import/coins/$symbol-coinsnapshot.json";
$data = is_file( $file ) ? json_decode( file_get_contents( $file ) ) : [];
@campusboy87
campusboy87 / add_rest_post_view_counter.php
Created November 19, 2018 18:51
Adds a counter to the rest request post. The functionality of the plugin "Post Views Counter" is used.
<?php
add_filter( 'rest_prepare_post', 'add_rest_post_view_counter', 10, 3 );
/**
* @param WP_REST_Response $response The response object.
* @param WP_Post $post Post object.
* @param WP_REST_Request $request Request object.
*
* @return mixed
@campusboy87
campusboy87 / acf_filter_field_groups_in_term.php
Created November 7, 2018 19:59
Оставляет указанные группы полей ACF на странице указанных терминов в админке, а на остальных удаляет.
<?php
/**
* Оставляет указанные группы полей ACF на странице указанных терминов в админке, а на остальных удаляет.
*
* @param array $field_groups
*
* @return array
*/
function acf_filter_field_groups_in_term( $field_groups ) {
<?php
add_action( 'init', function () {
if ( ! isset( $_GET['acf-convert'] ) ) {
return;
}
$posts = get_posts( [
'post_type' => 'event',
'numberposts' => - 1,
@campusboy87
campusboy87 / delete_empty_terms_from_menu.php
Last active October 10, 2018 00:15
Removes the terms with no posts from the menu.
<?php
add_filter( 'wp_nav_menu_objects', 'delete_empty_terms_from_menu', 10, 2 );
function delete_empty_terms_from_menu( $items, $args ) {
// Проверяем, что область меню primary, чтобы не обрабатывать лишние меню
if ( $args->theme_location === 'primary' ) {
foreach ( $items as $key => $item ) {
// Проверяем, что пункт меню относится к термину и что у него нет постов, и удаляем таковые.
if ( 'taxonomy' == $item->type && get_term( $item->object_id )->count === 0 ) {
<?php
// Вариант с Рубриками
add_action( 'pre_handle_404', 'fix_fake_category_page' );
function fix_fake_category_page( $false ) {
global $wp_query;
if ( is_archive() && get_query_var('category_name') === '0' ) {
// if ( is_archive() && ! term_exists( get_query_var( 'category_name' ) ) ) {
@campusboy87
campusboy87 / posts_columns_ind_and_image.php
Last active January 5, 2021 15:11
Добавляет в таблицу с записями колонки с ID записи и её миниатюрой.
<?php
// Регистрируем свои колонки (столбцы). Обязательно.
add_filter( 'manage_post_posts_columns', function ( $columns ) {
$my_columns = [
'id' => 'ID',
'image' => 'Миниатюра',
];
return array_slice( $columns, 0, 1 ) + $my_columns + $columns;