Skip to content

Instantly share code, notes, and snippets.

View khromov's full-sized avatar

Stanislav Khromov khromov

View GitHub Profile
@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 / acf-nowhere-location-rule.php
Created October 2, 2017 17:36
"Nowhere" location rule for Advanced Custom Fields - never matches
<?php
/*
Plugin Name: Advanced Custom Fields: Nowhere location rules
Description: Adds a "Nowhere" location rule in ACF
*/
add_filter('acf/location/rule_types', function($rules) {
$rules['Extra']['nowhere'] = 'Nowhere';
@khromov
khromov / rock-paper-scissors-js.html
Created September 3, 2017 15:52
Rock, Paper, Scissors - JS game
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width">
<title>Rock, Paper, Scissors</title>
<style>
*{margin:0;padding:0;-moz-box-sizing:border-box;-webkit-box-sizing:border-box;box-sizing:border-box}::selection{background:#fb9fe5;text-shadow:none}a{color:#0088CC;text-decoration:none;outline:0}a:hover{color:#005580}a img{border:none}img{max-width:100%}html{font-size:100%}body{padding:5%;font:300 1.25em/1.6 monospace;background:#fff;color:#000;}h1{font-family:"Helvetica Neue",sans-serif;line-height:1;font-weight:300;margin-bottom:1em;}.game{padding:2em;background:#eee;}
@khromov
khromov / deploy.php
Last active March 10, 2020 15:27
Deploying WordPress with Deployer 4
<?php
namespace Deployer;
require 'recipe/common.php';
set('ssh_type', 'native');
set('ssh_multiplexing', true);
// Set configurations
set('repository', 'git@github.com:user/repo.git');
@khromov
khromov / print-tax.php
Created March 15, 2017 22:20
Print taxonomy terms starting from specific slug in WordPress
<?php
/*
* Plugin Name: Print taxonomy terms starting from specific slug
* Description: Usage: [print_taxonomy slug="product-category"] or <?php echo do_shortcode('[print_taxonomy slug="product-category"]'); ?>
*/
add_shortcode('print_taxonomy', function($atts, $content) {
$atts = shortcode_atts( array(
'slug' => '',
), $atts, 'print_taxonomy' );
@khromov
khromov / functions.php
Created February 14, 2017 21:28
Load WPFacet facets from facets.json file in theme
<?php
//...
add_filter( 'facetwp_facets', function($facets) {
//Load facets.json file located in theme folder
$imported_facets = json_decode(file_get_contents(trailingslashit(dirname(__FILE__)) . 'facets.json'), true);
//Import facets
foreach($imported_facets['facets'] as $single_facet) {
$facets[] = $single_facet;
@khromov
khromov / deploy.php
Created June 8, 2016 07:28
Deploy WordPress with Deployer on EasyEngine
<?php
require 'recipe/common.php';
// Set configurations
set('repository', 'ssh://gogs@git.server.com:22/user/repo.git');
set('shared_files', ['public/wp-config.php']);
set('shared_dirs', ['public/wp-content/uploads']);
set('writable_dirs', []);
set('keep_releases', 10);
set('composer_command', 'composer');
@khromov
khromov / 404.php
Created May 24, 2016 20:28
Set a page with slug "404-error" as your 404 page in WordPress
<?php get_header(); ?>
<div class="container-fluid">
<div class="content">
<main role='main'>
<?php
$query_404 = new WP_Query(array(
'posts_per_page' => 1,
'post_type' => 'page',
'post_name' => '404-error'
));
@khromov
khromov / fix-domain-mapping-preview.php
Last active March 2, 2016 14:52
Set correct preview link when using WordPress MU Domain Mapping Plugin
<?php
add_filter('get_sample_permalink_html', function($return, $id, $new_title, $new_slug) {
$post_type_object = get_post_type_object(get_post_type($id));
//Append rewrite slug if it exists
if(isset($post_type_object->rewrite['slug'])) {
$url_append = '/' . $post_type_object->rewrite['slug'];
}
else {
@khromov
khromov / sync-parent-categories-acf.php
Last active August 14, 2021 23:40
Sync taxonomy categories with ACF taxonomy field in WordPress
<?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;