Developer's Roundtable Notes

Session: 15 Things You Can Do Without a Plugin

Add Google Fonts

Enqueue Google Fonts with a function:

add_action('wp_print_styles', 'dianaworks_load_fonts');

function dianaworks_load_fonts() {
     wp_register_style('googleFonts', '//fonts.googleapis.com/css?family=Ubuntu|Coda');
     wp_enqueue_style( 'googleFonts');
}

 

Custom Read More Link

Create a custom “read more” link at the end of each post.

add_filter( 'the_content_more_link', 'dianaworks_read_more_link' ); //for posts
add_filter('excerpt_more', 'dianaworks_read_more_link'); // for excerpts

function dianaworks_read_more_link( $more ) { 
     global $post;
     return '<p><a class="button" href="' . get_permalink($post->ID) . '">Read More &raquo;</a></p>';
}

 

Allow HTML in Excerpts

WordPress strips all code from excerpts. This will let you make them more readable.

remove_filter('get_the_excerpt', 'wp_trim_excerpt'); 
add_filter('get_the_excerpt', 'dianaworks_html_excerpt');

function dianaworks_html_excerpt($text) { // Fakes an excerpt if needed
    global $post;
    if ( '' == $text ) {
        $text = get_the_content('');
        $text = apply_filters('the_content', $text);
        $text = str_replace('\]\]\>', ']]&gt;', $text);
        $text = strip_tags($text,'<p><br><b><a><em><strong>'); // list of tags to allow
        $excerpt_length = 55;  // the number of words in each excerpt
        $words = explode(' ', $text, $excerpt_length + 1);
        if (count($words)> $excerpt_length) {
            array_pop($words);
            array_push($words, '. . .<p><a class="button" href="' . get_permalink($post->ID) . '">Read More &raquo;</a></p>');
            $text = implode(' ', $words);
        }
    }
    return $text;
}

 

Remove Pages from Admin Menu

Hide unnecessary pages from users

add_action( 'admin_menu', 'dianaworks_remove_menu_pages' );
 
function dianaworks_remove_menu_pages() {
	/* only let administrators see these pages */
	if (!current_user_can('activate_plugins')) {
	remove_menu_page('tools.php');	
	remove_menu_page('themes.php');	
	remove_menu_page('options-general.php');	
	remove_menu_page('plugins.php');	
	remove_submenu_page( 'users.php' , 'user-new.php' );
	remove_submenu_page( 'edit.php' , 'edit-tags.php?taxonomy=category' );
	remove_submenu_page( 'edit.php' , 'edit-tags.php?taxonomy=post_tag' );
	remove_submenu_page( 'link-manager.php' , 'edit-tags.php?taxonomy=link_category' );
	}
}

 

Remove Emoji Support

For sites where they aren’t used, don’t load the extra scripts

add_action( 'init', 'dianaworks_remove_emojicons' );

function dianaworks_remove_emojicons() 
{
    // Remove from comment feed and RSS
    remove_filter( 'the_content_feed', 'wp_staticize_emoji' );
    remove_filter( 'comment_text_rss', 'wp_staticize_emoji' ); 
    // Remove from emails
    remove_filter( 'wp_mail', 'wp_staticize_emoji_for_email' ); 
    // Remove from head tag
    remove_action( 'wp_head', 'print_emoji_detection_script', 7 ); 
    // Remove from print related styling
    remove_action( 'wp_print_styles', 'print_emoji_styles' ); 
    // Remove from admin area
    remove_action( 'admin_print_scripts', 'print_emoji_detection_script' );
    remove_action( 'admin_print_styles', 'print_emoji_styles' );
}

 

Custom Login Error Handling

Remove the hints, and customize the error message.

add_filter( 'login_errors', 'dianaworks_wordpress_errors' );

function dianaworks_wordpress_errors(){
  return 'Sorry, please try again';
}

Or, just send them to the front end. (Handy if your login is on a page)

add_action( 'wp_login_failed', 'dianaworks_front_end_login_fail' ); 

function dianaworks_front_end_login_fail( $username ) {
      wp_redirect( home_url() . '?login=failed' );  
      exit;
}

 

Allow Shortcodes in Widgets

add_filter('widget_text', 'do_shortcode');

 

Custom Excerpt Length

Create a custom length for certain locations or post types.

if ( 'my_custom_post_type' == get_post_type( get_the_ID() ) ) {

    add_filter( 'excerpt_length', 'dianaworks_custom_excerpt_length', 999 );

    function dianaworks_custom_excerpt_length( $length ) {
	return 20; // make whatever you want default is 55
    }
}

 

Add Content to the End of Each Post

add_filter ('the_content', dianaworks_footnote);

function dianaworks_footnote($content) {
	if(!is_feed() && !is_home()) {
		$content.= "<div class='subscribe'>";
		$content.= "<h4>Enjoyed this article?</h4>";
		$content.= "<p>Subscribe to our  <a href='http://
		feeds2.feedburner.com/WpRecipes'>RSS feed</a> 
		and never miss a recipe!</p>";
		$content.= "</div>";
	}
	return $content;
}

 

Get the Word Count of Posts

function dianaworks_word_count(){
    ob_start();  // start the output buffer
    the_content();
    $content = ob_get_clean();  // dump into a variable
    return sizeof(explode(" ", $content));
}

Call with:  <?php echo $dianaworks_word_count ?>

 

Display a List of Categories

<ul class="navmenubar">
    <?php wp_list_categories('orderby=name&include=7,9,19,16,1,5,17,23'); ?>
</ul>

 

Hide WordPress Version in Header

Remove the WordPress version from your site’s header.

remove_action('wp_head', 'wp_generator');

Or, better yet – remove it from everywhere.

add_filter('the_generator', 'dianaworks_remove_version');

function dianaworks_remove_version() {
    return '';
}

Thanks, Kate, for the suggestion!

Allow Users with Duplicate Emails

add_filter('pre_user_email', 'dianaworks_skip_email_exist');

function dianaworks_skip_email_exist($user_email){
    define( 'WP_IMPORTING', 'SKIP_EMAIL_EXIST' );
    return $user_email;
}

 

Add a Custom Avatar

add_filter( 'avatar_defaults', 'dianaworks_addgravatar' );

function dianaworks_addgravatar( $avatar_defaults ) {
	$myavatar = get_bloginfo('template_directory') . '/images/avatar.jpg';
	$avatar_defaults[$myavatar] = 'New Avatar';
	return $avatar_defaults;
	}


Then create your image and upload the file to wp-content/themes/yourtheme/images/avatar.jpg (you can name it anything you want).

Remove Width and Height from Images

Make all your images responsive by removing the width and height added by WordPress.

add_filter( 'post_thumbnail_html', 'dianaworks_remove_width_attribute', 10 );
add_filter( 'image_send_to_editor', 'dianaworks_remove_width_attribute', 10 );
 
function dianaworks_remove_width_attribute( $html ) {
   $html = preg_replace( '/(width|height)="\d*"\s/', "", $html );
   return $html;
}

 

Allow PHP in a Text Widget

add_filter('widget_text','dianaworks_execute_php',100);

function dianaworks_execute_php($html){
    if(strpos($html,"<"."?php")!==false){
	ob_start();
	eval("?".">".$html);
	$html=ob_get_contents();
	ob_end_clean();
    }
 return $html;
}

 

Redirect Non Admin Users to the Front End

add_filter("login_redirect", "dianaworks_admin_login_redirect", 10, 3);

function dianaworks_admin_login_redirect( $redirect_to, $request, $user ) {
	global $user;
	if( isset( $user->roles ) && is_array( $user->roles ) ) {
		if( in_array( "administrator", $user->roles ) ) {
			return $redirect_to;
		} else {
			return home_url();
		}
	} else {
		return $redirect_to;
	}
}

 

Change Password Change Email

add_filter( 'password_change_email', 'dianaworks_change_password_mail_message', 10,  3 );

function dianaworks_change_password_mail_message( $pass_change_mail, $user, $userdata ) {
    $new_message_txt = __( 'Hi,

This notice confirms that your password was changed on ###SITENAME###.

If you did not change your password, please contact the Site Administrator at mysite.com.

This email has been sent to ###EMAIL###

Regards,
All at ###SITENAME###
###SITEURL### ' );

  $pass_change_mail[ 'message' ] = $new_message_txt;
  return $pass_change_mail;
}

 

Add Custom Text in the Admin Footer

function custom_admin_footer() {
	echo 'Website by <a href="http://diananichols.com/" title="Visit DianaNichols.com for more information" target="_blank">Diana Nichols</a>';
} 
add_filter('admin_footer_text', 'custom_admin_footer');

 

Add Links to the End of a Menu

add_filter('wp_nav_menu_items', 'dianaworks_add_admin_link', 10, 2);
 
function dianaworks_add_admin_link($items, $args){ 
    if( $args->theme_location == 'footer_menu' ){ 
        $items = $items . '<li><a title="Login" href="'. esc_url( wp_login_url( get_permalink() ) ) .'">' . __( Login) . '</a></li>'; 
    } 
    return $items; 
}

Another example

add_filter( 'wp_nav_menu_items', 'dianaworks_nav_menu_items' );

function dianaworks_nav_menu_items($items) {
    global $woocommerce;
    $cartlink = '<li class="menu-item"><a class="cart-contents" href="'. $woocommerce->cart->get_cart_url().'" title="View Shopping Cart">My Cart (' . $woocommerce->cart->cart_contents_count. ')</a></li>';
    if ( is_user_logged_in() ) { 
	$accountlink= '<li class="menu-item"><a href="'. get_permalink( get_option('woocommerce_myaccount_page_id') ).'" title="My Account">My Account</a></li>';
    } else { 
	$accountlink= '<li class="menu-item"><a href="'. get_permalink( get_option('woocommerce_myaccount_page_id') ).'" title="Login/Register">Login/Register</a></li>';
    } 
    $items = $items . $cartlink . $accountlink;
    return $items;
}

 

What to Do with Your Functions

Put them in your functions.php file?

Create a site specific plugin?

Skip to toolbar