Skip to content

Instantly share code, notes, and snippets.

View khromov's full-sized avatar

Stanislav Khromov khromov

View GitHub Profile
@khromov
khromov / theme-subresource.php
Last active August 29, 2015 14:28
WordPress Instant Articles - Hint about subresources to load from a WordPress theme or child theme - https://wordpress.org/plugins/instant-articles/
<?php
add_filter('wpinstant_subresources', function($subresources) {
//Will add link rel="subresource" for /style.css and /js/main.js in the theme folder.
$subresources[] = get_stylesheet_directory_uri() . '/style.css';
$subresources[] = get_stylesheet_directory_uri() . '/js/main.js';
return $subresources;
});
@khromov
khromov / prerender-by-meta.php
Last active September 1, 2017 07:41
WordPress Instant Articles - Prerender pages by meta value - https://wordpress.org/plugins/instant-articles/
<?php
add_filter('wpinstant_prerendered_urls', function($urls) {
if ( is_home() ) {
$args = array(
'meta_key'=> 'prefetch_on_home',
'meta_value' => true
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) {
foreach( $query->posts as $post ) {
@khromov
khromov / prerender.php
Created August 23, 2015 23:08
WordPress Instant Articles - Prerender specific pages - https://wordpress.org/plugins/instant-articles/
<?php
add_filter('wpinstant_prerendered_urls', function($urls) {
if ( is_front_page() ) {
$posts = array( 1,2,3);
foreach($posts as $post) {
$permalink = get_permalink( $post );
if($permalink) {
$urls[] = $permalink;
}
}
@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 / gist:740578d08abd86dd093e
Last active July 26, 2016 12:08
WordPress CRON

So how does WP CRON work? Whenever an admin user loads WordPress, WP checks whether any CRON jobs need to run (they are scheduled by timestamp), and if they do, WordPress makes a secondary request to itself using the spawn_cron() function. This secondary page load will load /wp-cron.php, which in turn loads another full copy of WP.

Now we're in the secondary CRON request, you can see this as being run asynchronously with the request that spawned it - spawn_cron() uses a cute little trick and sets a very low timeout value when calling the secondary CRON request, the result being both requests run in parallel, see here: https://github.com/WordPress/WordPress/blob/2f3e567f44e7bc335b6795713ae970b4f37117cd/wp-includes/cron.php#L297

@khromov
khromov / stack.sh
Created May 23, 2015 22:02
One-click LAMP stack on Debian Trusty
#!/bin/bash
#Update repo
apt-get update
#Generate mysql password
MYSQL_ROOT_PASSWORD=$(tr -cd '[:alnum:]' < /dev/urandom | fold -w8 | head -n1)
#Set the password so you don't have to enter it during installation
debconf-set-selections <<< "mysql-server mysql-server/root_password password $MYSQL_ROOT_PASSWORD"
@khromov
khromov / acf-local-json-plugin.php
Created May 7, 2015 18:44
Store Advanced Custom Field Local JSON in plugin
<?php
/*
Plugin Name: ACF Local JSON plugin
Plugin URI:
Description: Put this file in a plugin folder and create an /acf directory inside the plugin, ie: /my-plugin/acf
Author: khromov
Version: 0.1
*/
//Change ACF Local JSON save location to /acf folder inside this plugin
@khromov
khromov / wordpress-taxonomy-query-acf-taxonomy-field-example.php
Last active April 14, 2017 05:22
wordpress-taxonomy-query-acf-taxonomy-field-example
$post_id = 1; //Post ID
$post_taxonomies = get_field('assigned-taxonomies', $post_id);
$args = array(
'post_type' => 'post',
'tax_query' => array(
array(
'taxonomy' => 'your-taxonomy',
'field' => 'term_id',
'terms' => $post_taxonomies
@khromov
khromov / cpt-remove-front.php
Last active December 22, 2016 15:01
WordPress - Remove the front permastruct / base slug from a custom post type (CPT)
<?php
class Plugin
{
/**
* This will let a CPT with the slug 'story' be used without a base slug, ie:
* Before:
* /story/hello
*
* After:
* /hello
@khromov
khromov / kill-during-activation.php
Created March 30, 2015 15:36
How to kill WordPress plugin during activation if it's not compatible with the environment
<?php
/* Activation hook */
register_activation_hook( __FILE__, function() {
if(!function_exists('\get_field')) {
deactivate_plugins(__FILE__);
wp_die("Please enable Advanced Custom Fields before activating this plugin.");
}
});