Skip to content

Instantly share code, notes, and snippets.

View johndugan's full-sized avatar

John Dugan johndugan

View GitHub Profile
@johndugan
johndugan / gist:4359828
Last active April 6, 2018 20:11
WordPress: remove inline width from captions
<?php
add_shortcode('wp_caption', 'fixed_img_caption_shortcode');
add_shortcode('caption', 'fixed_img_caption_shortcode');
function fixed_img_caption_shortcode($attr, $content = null) {
// New-style shortcode with the caption inside the shortcode with the link and image tags.
if ( ! isset( $attr['caption'] ) ) {
if ( preg_match( '#((?:<a [^>]+>\s*)?<img [^>]+>(?:\s*</a>)?)(.*)#is', $content, $matches ) ) {
$content = $matches[1];
$attr['caption'] = trim( $matches[2] );
}
@johndugan
johndugan / gist:4173416
Created November 30, 2012 02:32
WordPress: edit user fields
// Add customer user fields in wp admin
function modify_contactmethods( $contactmethods ) {
// Remove unwanted fields //
unset($contactmethods['aim']);
unset($contactmethods['yim']);
unset($contactmethods['jabber']);
// Add new fields //
$contactmethods['twitter_handle'] = 'Twitter Handle (@)';
$contactmethods['linkedin_url'] = 'LinkedIn Profile (URL)';
return $contactmethods;
@johndugan
johndugan / is_blog.php
Created November 24, 2012 18:11 — forked from wesbos/is_blog.php
WordPress: is_blog()
function is_blog () {
global $post;
$posttype = get_post_type($post );
return ( ((is_archive()) || (is_author()) || (is_category()) || (is_home()) || (is_tag())) && ( $posttype == 'post') );
}
Usage:
<?php if (is_blog()) { echo 'You are on a blog page'; } ?>
@johndugan
johndugan / gist:3837750
Created October 5, 2012 02:35
PHP: for loop with continue
// If "i" is equal to 5, skip the echo statement below and continue evaluating the condition.
<?php
for($i=0; $i<=10; $i++) {
echo $i;
if($i==10) {
continue;
}
echo ", ";
}
@johndugan
johndugan / gist:3837726
Created October 5, 2012 02:26
PHP: for loop with break
// If "i" is equal to 10, break out of the loop and ignore the echo statement below.
<?php
for($i=0; $i<=10; $i++) {
echo $i;
if($i==10) {
break;
}
echo ", ";
}
@johndugan
johndugan / gist:3837644
Created October 5, 2012 01:59
PHP: while loop
<?php
$i=0;
while($i<10) {
include("test.php");
$i++;
}
?>
@johndugan
johndugan / gist:3837604
Created October 5, 2012 01:49
PHP: for loop
<?php
for($i=0; $i<10; $i++) {
include("file.php");
}
?>
@johndugan
johndugan / wp_functions_removePTagCatDesc.php
Created July 12, 2012 03:29
WordPress: remove <p> on category description
// ------------------------------------------------------------------------------
// ---------- Remove the paragraph wrapper on the category description ----------
// ------------------------------------------------------------------------------
<?php
remove_filter('term_description','wpautop');
?>
@johndugan
johndugan / wp_functions_loopShortcode.php
Created July 12, 2012 03:27
WordPress: loop shortcode for pages
// --------------------------------------------------
// ---------- Create custom loop shortcode ----------
// --------------------------------------------------
// usage: [loop cat="17" posts_per_page="6" ]
// --------------------------------------------------
<?php
add_shortcode('loop', 'shortcode_query');
function shortcode_query($atts, $content){
extract(shortcode_atts(array( // a few default values. more query params at http://codex.wordpress.org/Class_Reference/WP_Query
@johndugan
johndugan / wp_functions_excerptElipsis.php
Created July 12, 2012 03:23
WordPress: custom excerpt elipsis
// -------------------------------------------------------------------
// ---------- Change default [...] string for post excerpts ----------
// -------------------------------------------------------------------
<?php
function new_excerpt_more($more) {
return '{...}';
}
add_filter('excerpt_more', 'new_excerpt_more');
?>