Last active
June 10, 2017 13:01
-
-
Save dcondrey/8d33696204b71191835f to your computer and use it in GitHub Desktop.
My collection of custom functions for use with WP. Manipulating post content and titles, article content and titles, seo, cleanup, etc, misc..
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
<?php | |
/** | |
* Limit previous/next post title character length and append a ellipsis if trunicated | |
*/ | |
$previous_post = get_adjacent_post( false, '', true ); | |
$next_post = get_adjacent_post( false, '', false ); | |
$previous_post_title= get_the_title($previous_post); | |
$next_post_title = get_the_title($next_post); | |
$max_length = 75; | |
$previous_post_length = strlen($previous_post_title); | |
$next_post_length = strlen($next_post_title); | |
if ($previous_post_length > $max_length) { | |
$previous_post_excerpt = substr($previous_post_title, 0, $max_length) . '...'; | |
} else { | |
$previous_post_excerpt = $previous_post_title; | |
} | |
if ($next_post_length > $max_length) { | |
$next_post_excerpt = substr($next_post_title, 0, $max_length) . '...'; | |
} else { | |
$next_post_excerpt = $next_post_title; | |
} | |
?> | |
<nav class="c-nav"> | |
<div class="prev"> | |
<a href="<?php echo make_href_root_relative( get_permalink( $previous_post ) ); ?>" class="prev">Previous Post<br><span><?php echo $previous_post_excerpt; ?></span></a> | |
</div> | |
<div class="next"> | |
<a href="<?php echo make_href_root_relative( get_permalink( $next_post ) ); ?>" class="next">Next Post<br><span><?php echo $next_post_excerpt; ?></span></a> | |
</div> | |
</nav> | |
<?php | |
/** | |
* Limit Article title character length and append an ellipsis if trunicated, allow the full length of the title to display on the singlearticle page | |
*/ | |
$the_title = esc_attr ( the_title_attribute ( 'echo=0' ) ); | |
$max_length = 38; | |
$title_length = strlen ( $the_title ); | |
if ( $title_length > $max_length ) { | |
$title_excerpt = substr ( $the_title, 0, $max_length ) . '...'; | |
} else { | |
$title_excerpt = $the_title; | |
} | |
/** | |
* Call the first paragraph of article as excerpt and then slide open the full article (afer first paragraph) without AJAX | |
*/ | |
function get_first_paragraph(){ | |
global $post; | |
$str = wpautop( get_the_content() ); | |
$str = substr( $str, 0, strpos( $str, '</p>' ) + 4 ); | |
$str = strip_tags($str, '<a><strong><em>'); | |
return '<p>' . $str . '</p>'; | |
} | |
function get_the_post(){ | |
global $post; | |
$str = wpautop( get_the_content() ); | |
$str = substr( $str, (strpos( $str, '</p>'))); | |
return $str; | |
} | |
?> | |
<script> | |
jQuery ( document ).ready ( function () { | |
jQuery ( ".content" ).hide (); | |
jQuery ( ".entry-footer" ).hide (); | |
jQuery ( ".more" ).click ( function () { | |
jQuery ( this ).parent ().next ( '.content' ).show ( "blind", {direction : "vertical"}, 750 ); | |
jQuery ( this ).next ( '.entry-footer' ).show ( "fast" ); | |
jQuery ( this ).hide (); | |
} ); | |
jQuery ( ".close" ).click ( function () { | |
jQuery ( this ).parent ( ".content" ).hide ( "blind", {direction : "vertical"}, 750 ); | |
jQuery ( ".more" ).show (); | |
} ); | |
} ); | |
</script> | |
<span class="excerpt"> | |
<?php echo get_first_paragraph(); ?> | |
<span class="more">+ more</span> | |
</span> | |
<span class="content"> | |
<?php echo get_the_post(); ?> | |
<?php | |
$content = get_the_content(); | |
print substr( $content, ( $lithograph_theme_options['excerpt_length'] + 1 ) ); | |
?> | |
<span class="close">- close</span> | |
<?php get_template_part( 'content', 'footer' ); ?> | |
</span> |
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
<?php | |
/** | |
* Rename Posts, Articles | |
*/ | |
function litho_posts_articles() { | |
global $menu; | |
global $submenu; | |
$menu[5][0] = __("Articles", 'litho'); | |
$submenu['edit.php'][5][0] = __("Articles", 'litho'); | |
$submenu['edit.php'][10][0] = __("New Article", 'litho'); | |
echo ''; | |
} | |
function litho_posts_articles_label() { | |
global $wp_post_types; | |
$labels = &$wp_post_types['post']->labels; | |
$labels->name = __("Articles", 'litho'); | |
$labels->singular_name = __("Article", 'litho'); | |
$labels->add_new = __("New Article", 'litho'); | |
$labels->add_new_item = __("New Article", 'litho'); | |
$labels->edit_item = __("Edit Article", 'litho'); | |
$labels->new_item = __("Article", 'litho'); | |
$labels->view_item = __("View Article", 'litho'); | |
$labels->search_items = __("Search Articles", 'litho'); | |
$labels->not_found = __("No Article Found", 'litho'); | |
$labels->not_found_in_trash = __("No Articles found in Trash", 'litho'); | |
} | |
add_action( 'init', 'litho_posts_articles_label' ); | |
add_action( 'admin_menu', 'litho_posts_articles' ); | |
/** | |
* Remove default taxonomies Categories and Tags | |
*/ | |
function unregister_taxonomy() { | |
global $wp_taxonomies; | |
$taxonomy = 'taxonomy_to_remove'; | |
if ( taxonomy_exists($taxonomy) ) { | |
unset( $wp_taxonomies[$taxonomy] ); | |
} | |
} | |
add_action( 'init', 'unregister_taxonomy' ); | |
/** | |
* Convert hexdec color string to rgb(a) string | |
* @param $color | |
* @param bool $opacity | |
* | |
* @return string | |
*/ | |
function hex2rgba( $color, $opacity = false ) { | |
$default = 'rgba(255,255,255,0.5)'; | |
//Return default if no color provided | |
if ( empty( $color ) ) return $default; | |
//Sanitize $color if "#" is provided | |
if ( $color[0] == '#' ) $color = substr( $color, 1 ); | |
//Check if color has 6 or 3 characters and get values | |
if ( strlen( $color ) == 6 ) { | |
$hex = array ( $color[0] . $color[1], $color[2] . $color[3], $color[4] . $color[5] ); | |
} elseif ( strlen( $color ) == 3 ) { | |
$hex = array ( $color[0] . $color[0], $color[1] . $color[1], $color[2] . $color[2] ); | |
} else return $default; | |
//Convert hex to rgb | |
$rgb = array_map( 'hexdec', $hex ); | |
//Check if opacity is set(rgba or rgb) | |
if ( $opacity ) { | |
if ( abs( $opacity ) > 1 ) $opacity = 1.0; | |
$output = 'rgba(' . implode( ",", $rgb ) . ',' . $opacity . ')'; | |
} else $output = 'rgb(' . implode( ",", $rgb ) . ')'; | |
return $output; | |
} | |
/** | |
* Google Plus Authorship | |
*/ | |
add_action('wp_head', 'litho_googleplus_authorship'); | |
function litho_googleplus_authorship() { | |
echo "<link rel='author' href='https://plus.google.com/s/....... />"; | |
} | |
/** Filters the page title appropriately depending on the current page */ | |
add_filter( 'wp_title', 'lithograph_filter_wp_title', 10, 2 ); | |
if ( ! function_exists( 'lithograph_filter_wp_title' ) ) { | |
function lithograph_filter_wp_title( $title, $sep ) { | |
global $paged, $page; | |
if ( is_feed() ) { | |
return $title; | |
} | |
$title .= get_bloginfo( 'name' ); | |
$site_description = get_bloginfo( 'description', 'display' ); // Add the site description for the home/front page. | |
if ( $site_description && ( is_home() || is_front_page() ) ) { | |
$title = "$title $sep $site_description"; | |
} | |
// Add a page number if necessary. | |
if ( $paged >= 2 || $page >= 2 ) { | |
$title = "$title $sep " . sprintf( __( 'Page %s', 'litho' ), max( $paged, $page ) ); | |
} | |
return $title; | |
} | |
} | |
function basic_wp_seo() { | |
global $page, $paged, $post; | |
$default_keywords = 'wordpress, plugins, themes, design, dev, development, security, htaccess, apache, php, sql, html, css, jquery, javascript, tutorials'; // customize | |
$output = ''; | |
// description | |
$seo_desc = get_post_meta( $post->ID, 'mm_seo_desc', true ); | |
$description = get_bloginfo( 'description', 'display' ); | |
$pagedata = get_post( $post->ID ); | |
if ( is_singular() ) { | |
if ( ! empty( $seo_desc ) ) { | |
$content = $seo_desc; | |
} else if ( ! empty( $pagedata ) ) { | |
$content = apply_filters( 'the_excerpt_rss', $pagedata->post_content ); | |
$content = substr( trim( strip_tags( $content ) ), 0, 155 ); | |
$content = preg_replace( '#\n#', ' ', $content ); | |
$content = preg_replace( '#\s{2,}#', ' ', $content ); | |
$content = trim( $content ); | |
} | |
} else { | |
$content = $description; | |
} | |
$output .= '<meta name="description" content="' . esc_attr( $content ) . '">' . "\n"; | |
// keywords | |
$keys = get_post_meta( $post->ID, 'mm_seo_keywords', true ); | |
$cats = get_the_category(); | |
$tags = get_the_tags(); | |
if ( empty( $keys ) ) { | |
if ( ! empty( $cats ) ) { | |
foreach ( $cats as $cat ) { | |
$keys .= $cat->name . ', '; | |
} | |
} | |
if ( ! empty( $tags ) ) { | |
foreach ( $tags as $tag ) { | |
$keys .= $tag->name . ', '; | |
} | |
} | |
$keys .= $default_keywords; | |
} | |
$output .= "\t\t" . '<meta name="keywords" content="' . esc_attr( $keys ) . '">' . "\n"; | |
// robots | |
if ( is_category() || is_tag() ) { | |
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1; | |
if ( $paged > 1 ) { | |
$output .= "\t\t" . '<meta name="robots" content="noindex,follow">' . "\n"; | |
} else { | |
$output .= "\t\t" . '<meta name="robots" content="index,follow">' . "\n"; | |
} | |
} else if ( is_home() || is_singular() ) { | |
$output .= "\t\t" . '<meta name="robots" content="index,follow">' . "\n"; | |
} else { | |
$output .= "\t\t" . '<meta name="robots" content="noindex,follow">' . "\n"; | |
} | |
// title | |
$title_custom = get_post_meta( $post->ID, 'mm_seo_title', true ); | |
$url = ltrim( esc_url( $_SERVER['REQUEST_URI'] ), '/' ); | |
$name = get_bloginfo( 'name', 'display' ); | |
$title = trim( wp_title( '', false ) ); | |
$cat = single_cat_title( '', false ); | |
$tag = single_tag_title( '', false ); | |
$search = get_search_query(); | |
if ( ! empty( $title_custom ) ) { | |
$title = $title_custom; | |
} | |
if ( $paged >= 2 || $page >= 2 ) { | |
$page_number = ' | ' . sprintf( 'Page %s', max( $paged, $page ) ); | |
} else { | |
$page_number = ''; | |
} | |
if ( is_home() || is_front_page() ) { | |
$seo_title = $name . ' | ' . $description; | |
} elseif ( is_singular() ) { | |
$seo_title = $title . ' | ' . $name; | |
} elseif ( is_tag() ) { | |
$seo_title = 'Tag Archive: ' . $tag . ' | ' . $name; | |
} elseif ( is_category() ) { | |
$seo_title = 'Category Archive: ' . $cat . ' | ' . $name; | |
} elseif ( is_archive() ) { | |
$seo_title = 'Archive: ' . $title . ' | ' . $name; | |
} elseif ( is_search() ) { | |
$seo_title = 'Search: ' . $search . ' | ' . $name; | |
} elseif ( is_404() ) { | |
$seo_title = '404 - Not Found: ' . $url . ' | ' . $name; | |
} else { | |
$seo_title = $name . ' | ' . $description; | |
} | |
$output .= "\t\t" . '<title>' . esc_attr( $seo_title . $page_number ) . '</title>' . "\n"; | |
return $output; | |
} | |
// Use HTML5 FIGURE and FIGCAPTION for images and captions | |
function im_cleaner_captions( $output, $attr, $content ) { | |
if ( is_feed() ) { | |
return $output; | |
} | |
$defaults = array ( | |
'id' => '', | |
'align' => 'alignnone', | |
'width' => '', | |
'caption' => '' | |
); | |
$attr = shortcode_atts( $defaults, $attr ); | |
if ( 1 > $attr['width'] || empty( $attr['caption'] ) ) { | |
return $content; | |
} | |
$attributes = ( ! empty( $attr['id'] ) ? ' id="' . esc_attr( $attr['id'] ) . '"' : '' ); | |
$attributes .= ' class="wp-caption ' . esc_attr( $attr['align'] ) . '"'; | |
$output = '<figure' . $attributes . '>'; | |
$output .= do_shortcode( $content ); | |
$output .= '<figcaption class="wp-caption-text"><p>' . $attr['caption'] . '</p></figcaption>'; | |
$output .= '</figure>'; | |
return $output; | |
} | |
add_filter( 'img_caption_shortcode', 'im_cleaner_captions', 10, 3 ); | |
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
<?php | |
if ( extension_loaded( "zlib" ) && ( ini_get( "output_handler" ) != "ob_gzhandler" ) ) { | |
add_action( 'wp', create_function( '', '@ob_end_clean();@ini_set("zlib.output_compression", 1);' ) ); | |
} | |
if ( ! defined( 'WP_POST_REVISIONS' ) ) { | |
define( 'WP_POST_REVISIONS', 5 ); | |
} | |
function disable_dashboard_widgets() { | |
remove_action('welcome_panel', 'wp_welcome_panel'); | |
remove_meta_box ( 'dashboard_recent_comments', 'dashboard', 'core' ); // Comments Widget | |
remove_meta_box ( 'dashboard_incoming_links', 'dashboard', 'core' ); // Incoming Links Widget | |
remove_meta_box ( 'dashboard_plugins', 'dashboard', 'core' ); // Plugins Widget | |
// remove_meta_box('dashboard_quick_press', 'dashboard', 'core' ); // Quick Press Widget | |
remove_meta_box ( 'dashboard_recent_drafts', 'dashboard', 'core' ); // Recent Drafts Widget | |
remove_meta_box ( 'dashboard_primary', 'dashboard', 'core' ); // | |
remove_meta_box ( 'dashboard_secondary', 'dashboard', 'core' ); // | |
// removing plugin dashboard boxes | |
remove_meta_box ( 'yoast_db_widget', 'dashboard', 'normal' ); // Yoast's SEO Plugin Widget | |
} | |
add_action( 'admin_menu', 'disable_dashboard_widgets' ); | |
function litho_adminmenus() { | |
remove_submenu_page( 'themes.php', 'theme-editor.php' ); | |
remove_submenu_page( 'themes.php', 'plugin-editor.php' ); | |
remove_menu_page( 'tools.php' ); | |
} | |
function litho_udpatenotice() { | |
remove_action( 'admin_notices', 'update_nag', 3 ); | |
} | |
function litho_clean_rss() { | |
wp_die( __( 'No feed available,please visit our <a href="' . get_bloginfo( 'url' ) . '">homepage</a>!' ) ); | |
} | |
/** | |
* Disable self-ping | |
* @param $links | |
*/ | |
function litho_clean_ping( &$links ) { | |
$home = get_option( 'home' ); | |
foreach ( $links as $l => $link ) { | |
if ( 0 === strpos( $link, $home ) ) { | |
unset( $links[ $l ] ); | |
} | |
} | |
} | |
function litho_clean_widgets() { | |
unregister_widget( 'WP_Widget_Pages' ); | |
unregister_widget( 'WP_Widget_Calendar' ); | |
unregister_widget( 'WP_Widget_Archives' ); | |
unregister_widget( 'WP_Widget_Links' ); | |
unregister_widget( 'WP_Widget_Meta' ); | |
unregister_widget( 'WP_Widget_Search' ); | |
unregister_widget( 'WP_Widget_Text' ); | |
unregister_widget( 'WP_Widget_Categories' ); | |
unregister_widget( 'WP_Widget_Recent_Comments' ); | |
unregister_widget( 'WP_Widget_RSS' ); | |
unregister_widget( 'WP_Widget_Tag_Cloud' ); | |
} | |
function litho_dashboard() { | |
global $wp_admin_bar; | |
$wp_admin_bar->remove_menu( 'wp-logo' ); | |
$wp_admin_bar->remove_menu( 'about' ); | |
$wp_admin_bar->remove_menu( 'wporg' ); | |
$wp_admin_bar->remove_menu( 'documentation' ); | |
$wp_admin_bar->remove_menu( 'support-forums' ); | |
$wp_admin_bar->remove_menu( 'feedback' ); | |
$wp_admin_bar->remove_menu( 'view-site' ); | |
} | |
function litho_clean_jetpack() { | |
wp_deregister_style( 'AtD_style' ); | |
wp_deregister_style( 'jetpack-carousel' ); | |
wp_deregister_style( 'grunion.css' ); | |
wp_deregister_style( 'the-neverending-homepage' ); | |
wp_deregister_style( 'infinity-twentyten' ); | |
wp_deregister_style( 'infinity-twentyeleven' ); | |
wp_deregister_style( 'infinity-twentytwelve' ); | |
wp_deregister_style( 'noticons' ); | |
wp_deregister_style( 'post-by-email' ); | |
wp_deregister_style( 'publicize' ); | |
wp_deregister_style( 'sharedaddy' ); | |
wp_deregister_style( 'sharing' ); | |
wp_deregister_style( 'stats_reports_css' ); | |
wp_deregister_style( 'jetpack-widgets' ); | |
wp_dequeue_script( 'devicepx' ); | |
} | |
/** | |
* Disable WP generating multiple image sizes | |
* @param $sizes | |
* | |
* @return mixed | |
*/ | |
function litho_original_images( $sizes ) { | |
unset( $sizes['thumbnail'] ); | |
unset( $sizes['medium'] ); | |
unset( $sizes['large'] ); | |
return $sizes; | |
} | |
/** | |
* Remove version from scripts and styles | |
* @param $src | |
* | |
* @return mixed | |
*/ | |
function litho_versioning( $src ) { | |
$parts = explode( '?', $src ); | |
return $parts[0]; | |
} | |
/** | |
* Remove WP version from head and RSS | |
* @return string | |
*/ | |
function litho_wpversioning() { | |
return ''; | |
} | |
add_action( 'admin_menu', 'litho_adminmenus', 999 ); | |
add_action( 'admin_menu', 'litho_udpatenotice' ); // Admin update notifications | |
add_action( 'do_feed', 'litho_clean_rss', 1 ); // RSS feeds | |
add_action( 'do_feed_atom', 'litho_clean_rss', 1 ); // Atom feeds | |
add_action( 'do_feed_rdf', 'litho_clean_rss', 1 ); // RDF feed | |
add_action( 'do_feed_rss', 'litho_clean_rss', 1 ); // RSS feed | |
add_action( 'do_feed_rss2', 'litho_clean_rss', 1 ); // RSS2 feed | |
add_action( 'pre_ping', 'litho_clean_ping' ); // Self ping | |
add_action( 'widgets_init', 'litho_clean_widgets', 1 ); // Widget styles | |
add_action( 'wp_before_admin_bar_render', 'litho_dashboard' ); | |
add_action( 'wp_enqueue_scripts', 'litho_clean_jetpack', 20 ); | |
add_action( 'wp_print_styles', 'litho_clean_jetpack' ); // Jetpack styles | |
add_filter( 'intermediate_image_sizes_advanced', 'litho_original_images' ); | |
add_filter( 'login_errors', create_function( '$a', "return 'Error';" ) ); // Login errors | |
add_filter( 'show_admin_bar', '__return_false' ); // Admin bar | |
add_filter( 'script_loader_src', 'litho_versioning', 15, 1 ); // Scripts version | |
add_filter( 'style_loader_src', 'litho_versioning', 15, 1 ); // Styles version | |
add_filter( 'the_generator', 'litho_wpversioning' ); // WP version | |
add_filter( 'use_default_gallery_style', '__return_false' ); // Gallery styles | |
add_filter( 'xmlrpc_enabled', '__return_false' ); // XMLRPC | |
remove_action( 'wp_head', 'feed_links', 2 ); | |
remove_action( 'wp_head', 'feed_links_extra', 3 ); | |
remove_action( 'wp_head', 'rsd_link' ); // remove Really Simple Discovery protocol from header | |
remove_action( 'wp_head', 'wlwmanifest_link' ); // remove Windows Live Writer from header | |
remove_action( 'wp_head', 'wp_shortlink_wp_head' ); // remove WP post and page shortlinks | |
remove_filter( 'comment_text', 'make_clickable', 9 ); // Auto Linking URLs in comments |
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
/* Post Format Navigation. */ | |
$formats = get_terms( 'post_format' ); | |
if ( ! is_wp_error( $formats ) ) { | |
print '<ul>' . "\n"; | |
foreach ( $formats as $format ) { | |
$href = get_term_link( $format, 'post_format' ); | |
$text = get_post_format_string( str_replace( 'post-format-', '', $format->slug ) ); | |
print '<li><a href="' . $href . '">' . $text . '</a></li>' . "\n"; | |
} | |
print '</ul>' . "\n"; | |
} | |
/** | |
* Define default terms for custom taxonomies in WordPress 3.0.1 | |
* | |
* @author Michael Fields http://wordpress.mfields.org/ | |
* @props John P. Bloch http://www.johnpbloch.com/ | |
* | |
* @since 2010-09-13 | |
* @alter 2010-09-14 | |
* | |
* @license GPLv2 | |
*/ | |
function mfields_set_default_object_terms( $post_id, $post ) { | |
if ( 'publish' === $post->post_status ) { | |
$defaults = array( | |
'post_tag' => array( 'taco', 'banana' ), | |
'monkey-faces' => array( 'see-no-evil' ), | |
); | |
$taxonomies = get_object_taxonomies( $post->post_type ); | |
foreach ( (array) $taxonomies as $taxonomy ) { | |
$terms = wp_get_post_terms( $post_id, $taxonomy ); | |
if ( empty( $terms ) && array_key_exists( $taxonomy, $defaults ) ) { | |
wp_set_object_terms( $post_id, $defaults[$taxonomy], $taxonomy ); | |
} | |
} | |
} | |
} | |
add_action( 'save_post', 'mfields_set_default_object_terms', 100, 2 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment