Skip to content

Instantly share code, notes, and snippets.

Thorsten Ott tott

Block or report user

Report or block tott

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@tott
tott / maybe-flush-rewrite-rules.php
Created Mar 14, 2014
Flush rewrite rules when it's necessary. This could be put in an init hook or the like and ensures that the rewrite rules option is only rewritten when the generated rules don't match up with the option.
View maybe-flush-rewrite-rules.php
function maybe_flush_rules() {
global $wp_rewrite;
$rewrite_rules = get_option( 'rewrite_rules' );
foreach( $rewrite_rules as $rule => $rewrite ) {
$rewrite_rules_array[$rule]['rewrite'] = $rewrite;
}
$maybe_missing = $wp_rewrite->rewrite_rules();
$missing_rules = false;
$rewrite_rules_array = array_reverse( $rewrite_rules_array, true );
foreach( $maybe_missing as $rule => $rewrite ) {
@tott
tott / gist:a7638cb9b602425b3c87
Last active Aug 29, 2015
workaround disabling xmlrpc from within wordpress mu-plugins file
View gist:a7638cb9b602425b3c87
/**
* Disable xmlrpc
*/
if ( defined( 'XMLRPC_REQUEST' ) && XMLRPC_REQUEST ) {
include_once(ABSPATH . WPINC . '/class-IXR.php');
class blocking_xmlrpc_server {
function serve_request() {
$response = new IXR_Error( 405, sprintf( __( 'XML-RPC services are disabled on this site.' ) ) );
die( $response->getXml() );
@tott
tott / gist:1205944
Created Sep 9, 2011
str_replace around pre/code tags
View gist:1205944
function str_replace_around_pre_code( $_search, $_replace, $content ) {
$new_content = '';
// deal with matches inside pre and code tags. we don't want to have links in sourcecode so only process around this.
if ( preg_match_all( "/(.*)(<(pre|code).*>.+<\/(pre|code)>)(.*)/msiU", $content, $content_split, PREG_PATTERN_ORDER ) ) {
foreach ( $content_split[0] as $cnt_key => $raw ) {
foreach( $_search as $key => $search ) {
$content_split[1][$cnt_key] = str_replace( $search, $_replace[$key], $content_split[1][$cnt_key] );
$content_split[5][$cnt_key] = str_replace( $search, $_replace[$key], $content_split[5][$cnt_key] );
}
$new_content .= $content_split[1][$cnt_key] . $content_split[2][$cnt_key] . $content_split[5][$cnt_key];
@tott
tott / gist:1206819
Created Sep 9, 2011
cache nav menus
View gist:1206819
<?php
/**
* Wrapper function around wp_nav_menu() that will cache the wp_nav_menu for all tag/category
* pages used in the nav menus
*/
function cached_nav_menu( $args = array(), $prime_cache = true ) {
global $wp_query;
$queried_object_id = empty( $wp_query->queried_object_id ) ? 0 : (int) $wp_query->queried_object_id;
@tott
tott / progress-bar.php
Created Dec 14, 2011
Print a simple progress bar for your CLI PHP scripts
View progress-bar.php
<?php
$time1 = time(); // set the start time
dosomethingnasty();
function dosomethingnasty() {
for( $i=0; $i < 300; $i++ ) {
progress( $i, 300, 40 );
sleep( 1 );
}
@tott
tott / export.php
Created Dec 20, 2011
Exporter for WP_CLI
View export.php
<?php
WP_CLI::addCommand('export', 'ExportCommand');
/**
* Implement export command
*
* @package wp-cli
* @subpackage commands/internals
*/
@tott
tott / gist:1506214
Created Dec 21, 2011
Find urls in text and do something with them
View gist:1506214
<?php
if ( preg_match_all( '/(?P<protocol>(?:(?:f|ht)tp|https):\/\/)?(?P<domain>(?:(?!-)(?P<sld>[a-zA-Z\d\-]+)(?<!-)[\.]){1,2}(?P<tld>(?:[a-zA-Z]{2,}\.?){1,}){1,}|(?P<ip>(?:(?(?<!\/)\.)(?:25[0-5]|2[0-4]\d|[01]?\d?\d)){4}))(?::(?P<port>\d{2,5}))?(?:\/(?P<script>[~a-zA-Z\/.0-9-_]*)?(?:\?(?P<parameters>[=a-zA-Z+%&\&amp;\'\(\)0-9,.\/_ -]*))?)?(?:\#(?P<anchor>[=a-zA-Z+%&0-9._]*))?/x', $text, $data ) ) {
foreach ( $data['0'] as $url_key=>$url ) {
$domain = $data['sld'][$url_key].".".$data['tld'][$url_key];
$host = $data['domain'][$url_key];
$script = $data['script'][$url_key];
// for now we only want domains
//$urls[] = array('url'=>$url, 'host'=>$host, 'domain'=>$domain, 'script'=>$script);
$domains[$domain] = 0; // we do it like this to make sure we have only one hit per domain/context
@tott
tott / gist:1886279
Created Feb 22, 2012
emacs setup windows
View gist:1886279
(defun setup-my-windows ()
(interactive)
(select-window (get-largest-window))
(delete-other-windows)
(set-window-dedicated-p (selected-window) 1)
(split-window-horizontally)
(other-window 1)
;; top right
;;(set-window-dedicated-p (selected-window) 1)
@tott
tott / gist:2572405
Created May 1, 2012
debugging WordPress rewrite rules
View gist:2572405
<?php
add_action( 'generate_rewrite_rules', 'debug_action_generate_rewrite_rules', 1, 1 );
// debug http://core.trac.wordpress.org/browser/trunk/wp-includes/rewrite.php#L1592
// this should only be hit if the rewrite_rules option is empty.
// http://core.trac.wordpress.org/browser/trunk/wp-includes/rewrite.php#L1616
function debug_action_generate_rewrite_rules( $rules ) {
global $debug_action_rules;
error_log( __FUNCTION__ . ' : ' . __LINE__ );
error_log( var_export( $_SERVER, true ) );
error_log( "Rules Option: " . var_export( get_option( 'rewrite_rules' ), true ) );
View gist:3496266
<?php
add_action( 'plugins_loaded', 'bypass_wp_login_for_pw_protected_posts' );
function bypass_wp_login_for_pw_protected_posts() {
// this functionality is a fork of http://core.trac.wordpress.org/browser/trunk/wp-login.php#L385
// keep this in sync with Core
$action = isset( $_REQUEST['action'] ) ? $_REQUEST['action'] : 'login';
if ( 'postpass' <> $action )
return;
You can’t perform that action at this time.