-
-
Save sethrubenstein/a43e868c5d39a72b1836 to your computer and use it in GitHub Desktop.
Some nice WP filters and functions to make posts better.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
add_filter( 'excerpt_length', 'my_excerpt_length' ); | |
function my_excerpt_length( $length ) { | |
return 110; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
a.prettylink-wpmudev::before { | |
content: ''; | |
margin-right:2px; | |
position:relative; | |
top:2px; | |
width: 16px; | |
height:16px; | |
background: url('http://mysite.com/wp-content/uploads/2015/03/04/wpmuico.ico'); | |
display:inline-block; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$args = array( | |
'post_type' => 'attachment', | |
'post_status' => 'any', | |
'post_parent' => get_the_ID(), | |
'posts_per_page' => -1, | |
'fields' => 'ids'; | |
); | |
$images = new WP_Query( $args ); | |
echo do_shortcode( '[gallery ids="' . implode( ',', $images ) . '"]' ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
add_filter( 'the_content', 'my_content_insertion' ); | |
function my_content_insertion( $content ) { | |
preg_match_all('/<\/p>/', $content,$matches, PREG_OFFSET_CAPTURE); | |
$insertion_point = $matches[0][1][1]; | |
$content_before = substr( $content, 0, $insertion_point + 4 ); | |
$content_after = substr( $content, $insertion_point + 5 ); | |
$content_to_insert = get_the_post_thumbnail( $post->ID, 'post-single' ); | |
return $content_before . $content_to_insert . $content_after; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
add_filter( 'the_content', 'outdated_notice' ); | |
function outdated_notice( $content ) { | |
$notice = "<p class='notice'>This content is more than a year old, it may be outdated!</p>"; | |
$post_time = get_the_time( 'U' ); | |
$current_time = current_time( 'timestamp' ); | |
if( $current_time - $post_time >= 31556926 ) { | |
$content = $notice . $content; | |
} | |
return $content; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
add_filter( 'the_content', 'my_redaction' ); | |
function my_redaction( $content ) { | |
$search = array( | |
'frak' => '<span class="redacted">karf</span>', | |
); | |
$content = str_replace( array_keys( $search ), $search, $content ); | |
return $content; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
add_action( 'template_redirect', 'single_result' ); | |
function single_result() { | |
if ( is_search( )) { | |
global $wp_query; | |
if ( $wp_query->post_count == 1 ) { | |
wp_redirect( get_permalink( $wp_query->posts['0']->ID ) ); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function auto_link_text( $content ){ | |
$replacements = array( | |
'WordPress' => '<a href="http://wordpress.org">WordPress</a>' | |
); | |
$content = str_replace( array_keys( $replacements ), $replacements, $content ); | |
return $content; | |
} | |
add_filter( 'the_content', 'auto_link_text' ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
add_shortcode( 'iconlink', 'iconlink' ); | |
function iconlink( $atts ) { | |
$atts = shortcode_atts( array( | |
'url' => false, | |
'text' => 'link' | |
), $atts ); | |
if( empty( $atts['url'] ) ) { | |
return; | |
} | |
$url = parse_url( $atts['url'] ); | |
$domain = str_replace( '.', '-', $url['host'] ); | |
return '<a href="' . $atts['url'] . '" class="' . $domain . '">' . $atts['text'] . '</a>'; | |
} | |
add_shortcode( 'update_notice', 'update_notice' ); | |
function update_notice( $atts ) { | |
$atts = shortcode_atts( array( | |
'url' => false, | |
), $atts ); | |
if( empty( $atts['url'] ) ) { | |
return; | |
} | |
return 'This post is a bit outdated so we have updated the contents | |
with new developments and new methods. Please read <a href="' . $atts['url'] . '">the new version</a> for more information'; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment