Skip to content

Instantly share code, notes, and snippets.

Avatar

Stanislav Khromov khromov

View GitHub Profile
@khromov
khromov / README.md
Last active Sep 25, 2022
DiceBear avatars for WordPress
View README.md
@khromov
khromov / polylang-multilingual-sitemap.php
Last active Sep 25, 2022
Multilingual Polylang sitemaps for "XML Sitemap & Google News" plugin
View polylang-multilingual-sitemap.php
<?php
/**
* Plugin Name: Multilingual Polylang sitemaps for "XML Sitemap & Google News" plugin
* Description: For https://wordpress.org/plugins/xml-sitemap-feed/
* Version: 1.0
*/
add_action('xmlsf_tags_after', function () { // xmlsf_tags_after
//Bail early.
if (!function_exists('pll_languages_list')) {
@khromov
khromov / plugin.php
Created Sep 17, 2022
Gutenberg block templates with Advanced Custom Fields
View plugin.php
<?php
/*
Plugin Name: Plugin
Version: 1.0
*/
add_action('acf/init', function() {
// Check function exists.
if( function_exists('acf_register_block_type') ) {
@khromov
khromov / cart-cache-breaker.php
Last active Aug 14, 2022
Fixing Cart Widget showing the incorrect item when using WPML with WooCommerce, by forcing cart widget to refresh on every page load.
View cart-cache-breaker.php
/** Break html5 cart caching */
add_action('wp_enqueue_scripts', 'cartcache_enqueue_scripts', 100);
function cartcache_enqueue_scripts()
{
wp_deregister_script('wc-cart-fragments');
wp_enqueue_script( 'wc-cart-fragments', get_template_directory_uri() . '/cart-fragments.js', array( 'jquery', 'jquery-cookie' ), '1.0', true );
}
@khromov
khromov / tampermonkey-script.js
Last active Aug 12, 2022
Tampermonkey: Find driving slot Trafikverket
View tampermonkey-script.js
// ==UserScript==
// @name Find slot
// @namespace http://tampermonkey.net/
// @version 0.1
// @description Book a Förarprov with the place and type (manual/auto) that you want, then go to Boka Prov > Personbil B > Bokade Prov > Press the edit icon for the existing test. Enable the script and reload the page. The script will look for a better time in the same configuration of auto/manual and location.
// @author You
// @match https://fp.trafikverket.se/boka/*
// @icon https://www.google.com/s2/favicons?sz=64&domain=trafikverket.se
// @require https://cdnjs.cloudflare.com/ajax/libs/ion-sound/3.0.7/js/ion.sound.min.js
// @grant none
@khromov
khromov / disable-rest-api-for-anonymous-users.php
Created Oct 13, 2017
Disable WordPress REST API for anonymous users
View disable-rest-api-for-anonymous-users.php
<?php
/*
Plugin Name: Disable REST API for anonymous users
*/
/**
* Remove all endpoints except SAML / oEmbed for unauthenticated users
*/
add_filter( 'rest_authentication_errors', function($result) {
if ( ! empty( $result ) ) {
@khromov
khromov / gutenberg-locked-template.php
Created Jun 9, 2022
WordPress Gutenberg - Add a locked template to a post type
View gutenberg-locked-template.php
<?php
add_action('init', function() {
// For which posts type
$post_type_object = get_post_type_object( 'post' );
// Which allowed blocks
$post_type_object->template = [
['core/heading', ['level' => '5', 'content' => 'Default value']],
['core/paragraph'],
['core/image'],
@khromov
khromov / control-blocks-by-post-type.php
Created Jun 9, 2022
WordPress Gutenberg - Control allowed blocks by post type
View control-blocks-by-post-type.php
<?php
add_filter( 'allowed_block_types_all', function($allowed_block_types, $block_editor_context) {
if ( $block_editor_context->post->post_type === 'post' ) {
return [
'acf/my-cool-block',
'core/paragraph'
];
}
return $allowed_block_types;
}, 10, 2 );
@khromov
khromov / docker-compose.yml
Created Aug 24, 2021
docker-compose file for Redis with persistence
View docker-compose.yml
version: "3"
services:
redis:
image: "redis:6.2-alpine"
command: sh -c "redis-server --appendonly yes"
ports:
- "6379:6379"
volumes:
- ./redis-data:/data
@khromov
khromov / sync-parent-categories-acf.php
Last active Aug 14, 2021
Sync taxonomy categories with ACF taxonomy field in WordPress
View sync-parent-categories-acf.php
<?php
add_action('save_post', array($this, 'mark_parent_categories'), 11);
/**
* Marks parent categories automatically. Works with the ACF taxonomy selector as well.
*
* @return mixed
*/
function mark_parent_categories($post_id) {
global $post;