Design & Development

A few of my favorite WP functions – collection 1

Posted on
functions.php

Some great WP functions to make your life just a little happier.

I’m always digging through past projects grabbing these snippets. Here they are, centralized!

Note! I did not write any of these, I’ve just collected them throughout my various projects. Your mileage will vary.

A new ‘Read More’, replaces the […]

|*|-php-|*|
function new_excerpt_more( $more ) {
    return ' <a class="read-more" href="'. get_permalink( get_the_ID() ) . '">Read More</a>';
}
add_filter( 'excerpt_more', 'new_excerpt_more' );

add_action( 'init', 'my_add_excerpts_to_pages' );
function my_add_excerpts_to_pages() {
     add_post_type_support( 'page', 'excerpt' );
}

Change excerpt or content length per individual basis

|*|-php-|*|
// Change excerpt length in theme - echo excerpt(25); - 
function excerpt($limit) {
      $excerpt = explode(' ', get_the_excerpt(), $limit);
      if (count($excerpt)>=$limit) {
        array_pop($excerpt);
        $excerpt = implode(" ",$excerpt).'... <a class="read-more" href="'. get_permalink( get_the_ID() ) . '">Read More</a>';
      } else {
        $excerpt = implode(" ",$excerpt);
      } 
      $excerpt = preg_replace('`\[[^\]]*\]`','',$excerpt);
      return '<p>' . $excerpt . '</p>';
    }

    function content($limit) {
      $content = explode(' ', get_the_content(), $limit);
      if (count($content)>=$limit) {
        array_pop($content);
        $content = implode(" ",$content).'... <a class="read-more" href="'. get_permalink( get_the_ID() ) . '">Read More</a>';
      } else {
        $content = implode(" ",$content);
      } 
      $content = preg_replace('/\[.+\]/','', $content);
      $content = apply_filters('the_content', $content); 
      $content = str_replace(']]>', ']]&gt;', $content);
      return '<p>' . $content . '</p>';
    }

Globally set new excerpt length

|*|-php-|*|
function custom_excerpt($title) {
    global $post;
    $text = get_field($title);
    if ( '' != $text ) {
        $text = strip_shortcodes( $text );
        $text = apply_filters('the_content', $text);
        $text = str_replace(']]>', ']]>', $text);
        $excerpt_length = 20; // 20 words
        //$excerpt_more = apply_filters('excerpt_more', ' ' . '[...]');
        $text = wp_trim_words( $text, $excerpt_length );
    }
    return apply_filters('the_excerpt', $text);
}

Images inserted into post by default are not linked to itself

|*|-php-|*|
// Makes image link none by default
update_option('image_default_link_type', 'none' );

Remove’s the p tag from inserted images into a post

|*|-php-|*|
// remove the p from around imgs (http://css-tricks.com/snippets/wordpress/remove-paragraph-tags-from-around-images/)
function filter_ptags_on_images($content){
   return preg_replace('/<p>\s*(<a .*>)?\s*(<img .* \/>)\s*(<\/a>)?\s*<\/p>/iU', '\1\2\3', $content);
}

add_filter('the_content', 'filter_ptags_on_images');

?>

Change “howdy” to something else in the dashboard

|*|-php-|*|
function howdy_message($translated_text, $text, $domain) {  
    $new_message = str_replace('Howdy', 'Welcome', $text);  
    return $new_message;  
}  
add_filter('gettext', 'howdy_message', 10, 3);

Change the class name on “active” links in the menu

|*|-php-|*|
//Replaces "current-menu-item" with "active"
function current_to_active($text){
        $replace = array(
                //List of menu item classes that should be changed to "active"
                'current_page_item' => 'active',
                'current_page_parent' => 'active',
                'current_page_ancestor' => 'active',
        );
        $text = str_replace(array_keys($replace), $replace, $text);
                return $text;
        }
add_filter ('wp_nav_menu','current_to_active');

And a few author specific functions

Hide a dashboard menu item from a specific user

|*|-php-|*|
function hide_menu() {
 global $current_user;
 $user_id = get_current_user_id();
 // echo "user:".$user_id;   // Use this to find your user id quickly

    if ($user_id == '9') {

        // To remove the whole Appearance admin menu you would use;
        remove_menu_page( 'edit.php?post_type=page' );
        remove_menu_page( 'edit.php?post_type=ads' );
    }
}

add_action('admin_head', 'hide_menu');

Add’s a CPT to a users access, in this case it’s called “Reviews”

|*|-php-|*|
function custom_post_author_archive($query) {
    if ($query->is_author)
        $query->set( 'post_type', array('reviews', 'post') );
    remove_action( 'pre_get_posts', 'custom_post_author_archive' );
}
add_action('pre_get_posts', 'custom_post_author_archive');

Allow’s users to only have access to their own media files

|*|-php-|*|
// Removes Media Access to their own
function my_files_only( $wp_query ) {
    if ( strpos( $_SERVER[ 'REQUEST_URI' ], '/wp-admin/upload.php' ) !== false ) {
        if ( !current_user_can( 'level_5' ) ) {
            global $current_user;
            $wp_query->set( 'author', $current_user->id );
        }
    }
}
add_filter('parse_query', 'my_files_only' );

Add a nicer pagination

|*|-php-|*|
function site_page_navi() {
  global $wp_query;
  $bignum = 999999999;
  if ( $wp_query->max_num_pages <= 1 )
    return;
  echo '<nav class="pagination">';
  echo paginate_links( array(
    'base'         => str_replace( $bignum, '%#%', esc_url( get_pagenum_link($bignum) ) ),
    'format'       => '',
    'current'      => max( 1, get_query_var('paged') ),
    'total'        => $wp_query->max_num_pages,
    'prev_text'    => '&larr;',
    'next_text'    => '&rarr;',
    'type'         => 'list',
    'end_size'     => 3,
    'mid_size'     => 3
  ) );
  echo '</nav>';
} /* end page navi */
Published under: WordPress
Tags: , ,
Previous Design and Development Love

Roll our own Events with Custom Post Types and Advanced Custom Fields

Next The Fold

The Fold is actually alive – it just moves around

Leave a Reply

Your email address will not be published. Required fields are marked *