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 / 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 / gist:1220235
Created September 15, 2011 19:29
hide certain posts from search
if ( function_exists( 'wpcom_is_vip' ) && wpcom_is_vip() ) {
wpcom_vip_load_helper_wpcom();
} else {
require_once( WP_CONTENT_DIR . '/themes/vip/plugins/vip-do-not-include-on-wpcom/vip-local-development-helper/vip-local-development-helper.php' );
require_once( WP_CONTENT_DIR . '/themes/vip/plugins/vip-do-not-include-on-wpcom/vip-powered-wpcom/vip-powered-wpcom.php' );
}
wpcom_vip_load_helper();
wpcom_vip_load_plugin( 'easy-custom-fields' );
@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:2572405
Created May 1, 2012 23:39
debugging WordPress rewrite rules
<?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 ) );
@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>';