My WordPress Incoming Links Dashboard Widget Is Empty

Help! The widget on my WordPress dashboard showing incoming links is broken – it just shows the default empty message.

This dashboard widget queries Google Blog Search so that when another blog links to your site it will show up here. It has found no incoming links… yet. It’s okay — there is no rush

Don’t worry, it should be a fairly easy fix. Go to your dashboard click on the ‘configure’ option that appears when you hover over the widget. The domain name in the query needs some special characters escaped. Let me show you with a before and after example from my own blog:

BEFORE (broken):


https://www.google.com/search?q=link:http://www.tcbarrett.com/&tbm=blg&tbs=sbd:1&output=rss

AFTER (fixed):


https://www.google.com/search?q=link:http%3A%2F%2Fwww.tcbarrett.com%2&tbm=blg&tbs=sbd:1&output=rss

There’s a little trick in there for you to spot. I suspect a bug in the actual WP code though…

Adding custom styling to WordPress custom post types admin screens

My WordPress projects usually end up using at least 3 custom post types, sometimes more than 10. Together with some extra admin screens (from external plugins[1]), the admin menu gets cluttered. All the various edit post admin screens can look very similar to the untrained eye.

What to do about!? Well, I’m not a designer, but I can certainly kick-start things by providing inspiration to my colleagues. What I need is some way of boldly showing them that we can add some styling to specific post type admin screens.

add_filter( 'admin_body_class', 'tcb_custom_post_type_body_class' );
function tcb_custom_post_type_body_class( $classes ){
  global $post;

  $post_class = get_post_type( $post->ID ) . '_admin';
  if( is_array($classes) ):
    $classes[] = $post_class;
  else :
    $classes .= " {$post_class}";
  endif;

  return $classes;
}

That will add a class to the page’s body tag, which can be targeted with CSS. Here is a rather pathetic example for my albums and artists post types:

.albums_admin{
  background-color: #0f0;
}
.artists_admin{
  background-color: #f00;
}

Good luck with that!

Restrict Post Title Word and Character Counts

Sometimes people get a bit carried away, typing in massively long strings into the post title. This is often just after they have learnt that the WordPress slug creates a pretty permalink that is good for SEO. The idea that focussing keywords is stronger than diluting them with everything under the sun seems to take a little while longer to sink in.

You can pop this little snippet in your functions.php and it will restrict the character count, word count and also automatically re-set the post status back to draft.

Restrict Post Title Length

add_action( 'publish_post', 'tcb_restrict_post_title', 11, 2 );
function tcb_restrict_post_title( $post_id, $post ){
  $title = $post->post_title;
  $MAX_WORDS    = 8;
  $MAX_CHARS    = 50;
  $err_mess     = '';
  $set_to_draft = 1;

  if( $MAX_WORDS && str_word_count( $title ) > $MAX_WORDS )
    $err_mess .= " Your post title exceeds maximum word count ({$MAX_WORDS}).";

  if( $MAX_CHARS && strlen( $title ) > $MAX_CHARS )
    $err_mess .= " Your post title exceeds the maximum character count({$MAX_CHARS}).";

  if( $err_mess ) :
    if( $set_to_draft )
      wp_update_post( array( 'ID'=>$post_id, 'post_status'=>'draft' ) );
    wp_die( __($err_mess, 'tcb') );
  endif;

  return $post_id;
}

You can toggle setting the draft status off and on easily with $set_to_draft, and if you want to be more or less restrictive on your word and characters counts, just change the numbers in $MAX_WORDS and $MAX_CHARS.

 

Add more buttons to the WordPress TinyMCE WYSIWYG post editor

WordPress uses the TinyMCE javascript library to power the WYSIWYG post editor. The core team have done a good job of selecting the most commonly needed buttons, but on occasion it can be useful to add more. In my experience, the three most commonly requested buttons are horizontal rule <hr>, superscript <sup> and subscript <sub>.

function tcb_add_tinymce_buttons( $tinyrowthree ) {
  $tinyrowthree[] = 'hr';
  $tinyrowthree[] = 'sub';
  $tinyrowthree[] = 'sup';
  return $tinyrowthree;
}
add_filter( 'mce_buttons_3', 'tcb_add_tinymce_buttons' );

This adds a third row to the editor with your three new buttons. There are more to choose from, you can visit the TinyMCE buttons page to have a look.

You may want to pair this with forcing the kitchen sink always on, so your clients can always find them..