Skip to content

Instantly share code, notes, and snippets.

@sethrubenstein
Forked from danielpataki/excerpt-length.php
Last active August 29, 2015 14:17
Show Gist options
  • Save sethrubenstein/a43e868c5d39a72b1836 to your computer and use it in GitHub Desktop.
Save sethrubenstein/a43e868c5d39a72b1836 to your computer and use it in GitHub Desktop.
Some nice WP filters and functions to make posts better.
add_filter( 'excerpt_length', 'my_excerpt_length' );
function my_excerpt_length( $length ) {
return 110;
}
$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 ) . '"]' );
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;
}
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;
}
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;
}
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 ) );
}
}
}
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' );
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