Skip to content

Instantly share code, notes, and snippets.

View martinsanne's full-sized avatar

Martin Sanne Kristiansen martinsanne

View GitHub Profile
@martinsanne
martinsanne / regex-outside-brackets.php
Created September 19, 2016 09:47
Regex replace text outside brackets only
<?php
$text = 'I love eating CO2 for breakfast. <a href="http://www.co2health.com" class="co2" id="co2">CO2 health effects</a>. Here is a pic of CO2 <img src="http://site.com/co2.jpg" alt="CO2">';
$text = preg_replace('~<[^>]*>(*SKIP)(*F)|CO2~', 'CO<sub>2</sub>', $text);
echo $text;
/*
Output:
I love eating CO<sub>2</sub> for breakfast. <a href="http://www.co2health.com" class="co2" id="co2">CO<sub>2</sub> health effects</a>. Here is a pic of CO<sub>2</sub> <img src="http://site.com/co2.jpg" alt="CO2">
*/
@martinsanne
martinsanne / error-reporting.php
Created September 1, 2016 12:16
WordPress selective error reporting
<?php
/*
Name file anything you want and put in put in wp-content/mu-plugins/
Credits:
https://wycks.wordpress.com/2013/12/05/how-to-remove-error-notices-using-wordpresss-wp_debug/
*/
error_reporting(E_ALL & ~( E_NOTICE | E_USER_NOTICE | E_STRICT | E_DEPRECATED | E_USER_DEPRECATED | E_WARNING | E_CORE_WARNING | E_USER_WARNING | E_COMPILE_WARNING | E_PARSE ));
@martinsanne
martinsanne / parseurl.php
Created December 9, 2015 09:41
Parse url attributes
$subject = '<a href="#" data-type="page" data-value="1" data-title="Dickbutt"></a>';
preg_match_all(
'/<a
(?:\s+
(?:
href=["\'](?P<href>[^"\'<>]+)["\']
|
data-value=["\'](?P<value>[^"\'<>]+)["\']
|
data-title=["\'](?P<title>[^"\'<>]+)["\']
@martinsanne
martinsanne / cleanup.php
Created December 8, 2015 10:54
WordPress cleanup
<?php
function wpbase_remove_admin_bar_glitch(){
// make sure body is not relative positioned
if ( is_admin_bar_showing() ) { ?>
<style type="text/css"> body { position: static !important; } </style>
<?php }
}
add_action('wp_head', 'wpbase_remove_admin_bar_glitch');
@martinsanne
martinsanne / gist:72f48bad22d99032ba0e
Created January 30, 2015 14:51
WordPress ACF Multisite location rule
<?php
/**
*
* Adds location rule to target specific sites in a WordPress Multisite environment
* http://www.advancedcustomfields.com/resources/custom-location-rules/
*
*/
function acf_location_rule_type_blog( $choices ) {