Skip to content

Instantly share code, notes, and snippets.

View khromov's full-sized avatar

Stanislav Khromov khromov

View GitHub Profile
@khromov
khromov / service-worker.ts
Created April 10, 2023 21:17
SvelteKit service worker example
/// <reference types="@sveltejs/kit" />
/// <reference no-default-lib="true"/>
/// <reference lib="esnext" />
/// <reference lib="webworker" />
// https://kit.svelte.dev/docs/service-workers#type-safety
const sw = self as unknown as ServiceWorkerGlobalScope;
import { build, files, version } from '$service-worker';
@khromov
khromov / bundle-size.yaml
Last active November 15, 2023 17:17
GitHub Actions Bundle size comparison
name: 'Bundle Size Comparison'
on:
pull_request:
branches:
- master
jobs:
build:
runs-on: ubuntu-latest
@khromov
khromov / functions.php
Last active November 5, 2023 23:45
Cache a slow WP_Query in WordPress
<?php
/**
* Function that gets recent posts
*/
function get_recent_posts() {
//No cache
if(!wp_cache_get('my_complex_query_result')) {
//This is the super slow query.
@khromov
khromov / disable-rest-api-for-anonymous-users.php
Created October 13, 2017 15:55
Disable WordPress REST API for anonymous users
<?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 / spp.php
Last active April 13, 2023 17:36
Count post view counts in WordPress via AJAX (Compatible with caching plugins)
<?php
/*
Plugin Name: Simple Popular Posts Lite
Plugin URI: -
Description: -
Version: 2015.03.01
Author: khromov
Author URI: http://snippets.khromov.se
License: GPL2
*/
@khromov
khromov / focused.ts
Last active February 19, 2023 20:20
Svelte window focused action
// Example: <div use:focused={(e) => console.log('Focused, event:', e)}>
export default function focused(node: HTMLElement, callback: Function) {
const handleFocusEvents = (event: Event) => {
callback(event)
};
window.addEventListener('online', handleFocusEvents);
window.addEventListener('focus', handleFocusEvents);
@khromov
khromov / .dockerignore
Last active January 11, 2023 20:04
SvelteKit staged build Dockerfile
node_modules
README.md
.svelte-kit
npm-debug.log*
yarn-debug.log*
yarn-error.log*
.pnpm-debug.log*
.env*.local
.env.local
.env
@khromov
khromov / README.md
Last active January 3, 2023 02:53
DiceBear avatars for WordPress
@khromov
khromov / polylang-multilingual-sitemap.php
Last active October 9, 2022 21:15
Multilingual Polylang sitemaps for "XML Sitemap & Google News" plugin
<?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 September 17, 2022 21:37
Gutenberg block templates with Advanced Custom Fields
<?php
/*
Plugin Name: Plugin
Version: 1.0
*/
add_action('acf/init', function() {
// Check function exists.
if( function_exists('acf_register_block_type') ) {