Skip to content

Instantly share code, notes, and snippets.

View kovshenin's full-sized avatar

Konstantin Kovshenin kovshenin

View GitHub Profile
@kovshenin
kovshenin / plugin.php
Created October 26, 2012 07:55
Settings API Demo
<?php
/**
* Plugin Name: My Plugin
* Plugin Description: Settings API Demo
*/
add_action( 'admin_menu', 'my_admin_menu' );
function my_admin_menu() {
add_options_page( 'My Plugin', 'My Plugin', 'manage_options', 'my-plugin', 'my_options_page' );
}
<?php
/**
* Plugin Name: Static Templates
*
* If most of your site content is in .php template files, and you're tired of
* creating new pages, assigning them page templates, creating page templates
* then doing it all over again on production, this plugin is for you.
*
* Examples:
*
@kovshenin
kovshenin / array-callables.php
Created November 17, 2015 12:53
Some array callables for WordPress
<?php
function __array_append( $item ) {
return function( $array ) use ( $item ) {
$array[] = $item;
return $array;
};
}
function __array_set( $key, $value ) {
return function( $array ) use ( $key, $value ) {
@kovshenin
kovshenin / image-shortcode.php
Created March 6, 2012 06:41
Image shortcode for WordPress
<?php
/**
* Image shortcode callback
*
* Enables the [kovshenin_image] shortcode, pseudo-TimThumb but creates resized and cropped image files
* from existing media library entries. Usage:
* [kovshenin_image src="http://example.org/wp-content/uploads/2012/03/image.png" width="100" height="100"]
*
* @uses image_make_intermediate_size
*/
@kovshenin
kovshenin / something.php
Created July 10, 2012 07:17
Yes, you can use printf and sprintf in WordPress too!
<?php
// Dirty, easy to miss a ' or " or .
echo '<a href="' . get_permalink() . '" class="link">' . get_the_title() . '</a>';
// Clean, easier to read
printf( '<a href="%s" class="link">%s</a>', get_permalink(), get_the_title() );
// Almost as clean, and more secure, maybe a little paranoic :)
printf( '<a href="%s" class="link">%s</a>', esc_url( get_permalink() ), esc_html( get_the_title() ) );
<?php
add_action( 'muplugins_loaded', function() {
if ( defined( 'WP_CLI' ) && WP_CLI ) {
return;
}
if ( empty( $_SERVER['HTTP_HOST'] ) ) {
return;
}
@kovshenin
kovshenin / test-ajax-scripts.php
Created November 29, 2012 07:27
Lazy load a script enqueued in a shortcode during and AJAX callback? Sure!
<?php
/**
* Plugin Name: My Plugin
*/
class My_Plugin_Test_AJAX_Scripts {
function __construct() {
add_action( 'wp_enqueue_scripts', array( $this, 'wp_enqueue_scripts' ) );
add_action( 'wp_head', array( $this, 'wp_head' ), 99 );
# Inside the server {} block
location @imgproxy {
proxy_ssl_server_name on;
proxy_ssl_name imgproxy.ondigitalocean.app;
proxy_set_header Host imgproxy.ondigitalocean.app;
proxy_set_header If-Modified-Since "";
proxy_set_header ETag "";
proxy_set_header Cache-Control "";
# In the main {} context
map $http_user_agent $imgproxy {
"~imgproxy" 1;
default 0;
}
map $http_accept $webp_suffix {
"~image/webp" "@webp";
default "";
<?php
/**
* Pressjitsu Remote HTTP Cache
*
* Many plugins like to perform remote HTTP requests for front-end visits,
* sometimes without even employing transient caching. We think that *all* HTTP
* requests should run in the background, and thus be always served from cache,
* even if the cache is stale.
*
* Configure cachable hosts via the pj_http_cache_hosts filter.