Skip to content

Instantly share code, notes, and snippets.

View tott's full-sized avatar

Thorsten Ott tott

  • Germany, Cologne area
View GitHub Profile
@tott
tott / maybe-flush-rewrite-rules.php
Created March 14, 2014 14:26
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.
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 August 29, 2015 14:05
workaround disabling xmlrpc from within wordpress mu-plugins file
/**
* 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 September 9, 2011 10:56
str_replace around pre/code tags
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 September 9, 2011 17:30
cache nav menus
<?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 December 14, 2011 12:56
Print a simple progress bar for your CLI PHP scripts
<?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 December 20, 2011 13:21
Exporter for WP_CLI
<?php
WP_CLI::addCommand('export', 'ExportCommand');
/**
* Implement export command
*
* @package wp-cli
* @subpackage commands/internals
*/
@tott
tott / gist:1506214
Created December 21, 2011 14:26
Find urls in text and do something with them
<?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 February 22, 2012 17:46
emacs setup windows
(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:3496266
Created August 28, 2012 08:42
enforce http
<?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;
@tott
tott / gist:3497438
Created August 28, 2012 11:40
Image custom field for Easy Custom Fields plugin
if ( !class_exists( "Easy_CF_Field_Image" ) ) {
class Easy_CF_Field_Image extends Easy_CF_Field {
public function print_form() {
$class = ( empty( $this->_field_data['class'] ) ) ? $this->_field_data['id'] . '_class' : $this->_field_data['class'];
$input_class = ( empty( $this->_field_data['input_class'] ) ) ? $this->_field_data['id'] . '_input_class' : $this->_field_data['input_class'];
$name = ( empty( $this->_field_data['name'] ) ) ? $this->_field_data['id'] : $this->_field_data['name'];
$id = ( empty( $this->_field_data['id'] ) ) ? $this->_field_data['id'] : $this->_field_data['id'];
$label = ( empty( $this->_field_data['label'] ) ) ? $this->_field_data['id'] : $this->_field_data['label'];
$value = $this->get();
$hint = ( empty( $this->_field_data['hint'] ) ) ? '' : '<p><em>' . $this->_field_data['hint'] . '</em></p>';