Skip to content

Instantly share code, notes, and snippets.

View Rarst's full-sized avatar
Otherwise occupied.

Andrey Savchenko Rarst

Otherwise occupied.
View GitHub Profile
@Rarst
Rarst / readme.md
Last active February 29, 2016 17:03
Exclude plugins and themes from update checks https://github.com/Rarst/update-blocker
@Rarst
Rarst / in_main_query.php
Created January 16, 2013 23:08
Mark WordPress posts in query if they are in main or secondary.
<?php
add_action( 'loop_start', 'mark_posts_with_query' );
function mark_posts_with_query( $query ) {
$in_main_query = $query->is_main_query();
foreach ( $query->posts as $post ) {
@Rarst
Rarst / .maintenance
Created April 21, 2012 12:36
WordPress maintenance mode
<?php
function is_user_logged_in() {
$loggedin = false;
foreach ( (array) $_COOKIE as $cookie => $value ) {
if ( stristr($cookie, 'wordpress_logged_in_') )
$loggedin = true;
}
return $loggedin;
}
if ( ! stristr($_SERVER['REQUEST_URI'], '/wp-admin') && ! stristr($_SERVER['REQUEST_URI'], '/wp-login.php') && ! is_user_logged_in() )
@Rarst
Rarst / append.php
Last active April 27, 2016 12:31
My xhprof/uprofiler setup, tweaked for WordPress and more easily profiling segments.
<?php
use Rarst\Profiler\Handler;
global $wp;
if ( Handler::$profiling && empty( $wp ) ) {
Handler::close();
}
<?php
$bench = new Ubench();
$total = 15000;
$count = 10;
$sql = "
SELECT l.ID, post_title, post_content, post_name, post_parent, post_author, post_modified_gmt, post_date, post_date_gmt
FROM (
SELECT {$wpdb->posts}.ID
@Rarst
Rarst / griddle.php
Last active August 24, 2016 20:53
Responsive grid layout backgrounds for Bootstrap.
<?php
if ( isset( $_GET['grid'] ) ) {
add_action( 'wp_print_styles', function () { ?>
<style type="text/css">
@media (min-width: 768px) {
body {
background: url("http://griddle.it/720px-12-30px") repeat-y center top !important;
}
@Rarst
Rarst / mustachepot.php
Last active September 29, 2016 13:47
MustachePOT extends MakePOT in WordPress tools to additionally scan for localized strings in Mustache templates.
<?php
error_reporting( E_ALL ^ E_STRICT ); // le sigh
require __DIR__ . '/wordpress/tools/i18n/makepot.php';
/**
* Additionally scans for localized strings in Mustache templates.
*/
class MustachePOT extends MakePOT {
@Rarst
Rarst / matrix.php
Last active November 11, 2016 12:20
Travis matrix generator for Yoast SEO.
<?php
$php_max = 7.0;
$php_min = 5.2;
$php_versions = array_filter( range( $php_max, $php_min + 0.1, 0.1 ), function ( $version ) {
return version_compare( $version, 5.6, '<=' ) || version_compare( $version, 7.0, '>=' );
} );
$wp_max = 4.6;
@Rarst
Rarst / dumper.php
Last active December 11, 2016 21:01
Dump included PHP files from specific path.
<?php
add_action( 'shutdown', function () {
if ( ! defined( 'WPSEO_PATH' ) ) {
return;
}
$path = str_replace( '\\', '/', WPSEO_PATH );
$includes = array_reduce( get_included_files(), function ( $plugin_includes, $include_path ) use ( $path ) {
@Rarst
Rarst / nice-labels.php
Last active May 3, 2017 06:43
"Nice numbers" implementation for pretty range of numerical labels on graph
<?php
// @link http://books.google.com/books?id=fvA7zLEFWZgC&pg=PA61&lpg=PA61#v=onepage&q&f=false
function nice_labels( $min, $max, $ticks = 5 ) {
$range = nice_number( $max, false );
$d = nice_number( $range / ( $ticks - 1 ) );
$graphmin = floor( $min / $d ) * $d;
$graphmax = ceil( $max / $d ) * $d;
$nfrac = max( array( - floor( log( $d, 10 ) ), 0 ) );