Redirect WordPress users during logout

Normally in WordPress when a user logs out, he is directed to the login page. But what if we want to redirect users to another page? No problem.

With the following code we can redirect users to the home page when they log out.

function jp_logout_redirect( $logout_url ) {
  return $logout_url . '&redirect_to=' . urlencode( home_url() );
}
add_filter( 'logout_url', 'jp_logout_redirect' );

 

What if we need to redirect users to a special page on our site? Just as easy.

function jp_logout_redirect( $logout_url ) {
  return $logout_url . '&redirect_to=' . urlencode( home_url() . '/our/special/page' );
}
add_filter( 'logout_url', 'jp_logout_redirect' );

Remove the WordPress logo from the admin bar

If you’re like me, you’ve accidentally clicked the WordPress logo in the admin bar a hundred times while meaning to go to the main Dashboard screen. That logo just gets in my way. I already know about WordPress, so I don’t need a link to the “about” page cluttering up my admin bar. Using the simple code below, we can remove the logo and dropdown menu and make things a little easier on us.

function jp_remove_admin_bar_logo() {
  global $wp_admin_bar;
  $wp_admin_bar->remove_menu( 'wp-logo' );
}
add_action( 'wp_before_admin_bar_render', 'jp_remove_admin_bar_logo', 0 );

Redirect WordPress users during login

Normally in WordPress when a user logs in, he is directed to some place in the administrative back end. If he is assigned the role of Subscriber, this usually sends him to his profile page, which isn’t very useful under most circumstances. If he is assigned the role of Contributor, Author, Editor or Administrator, he is directed to the Dashboard screen.

But what if we want to redirect users to a specific page in the back end or some other page on the front end? For example, maybe we have a special page set up just for Contributors. In WordPress we can redirect users based on their roles or capabilities. Here is an example.

function jp_login_redirect_contributors() {
  if ( current_user_can( 'contributor' ) ){
    return '/redirect/path';
  }
}
add_filter( 'login_redirect', 'jp_login_redirect_contributors' );

In this example, users assigned the role of Contributor are redirected to /redirect/path. WordPress has a number of roles and capabilities built in that we can target, and we can easily create custom roles and capabilities using plugins like Members.

Using these simple methods we can easily create custom workflows that make life easier in WordPress.

Multiple excerpt lengths in WordPress

The default WordPress excerpt length is 55 words. Sometimes we need to change this, and sometimes we need multiple excerpt lengths depending on where we are. For example, we might want a short excerpt on the home page and the default excerpt for everywhere else – archives, author pages, etc.

function jp_multiple_excerpt_lengths($length) {
	if ( is_front_page() ) {
		return 15;
	}

	return 55;
}
add_filter( 'excerpt_length', 'jp_multiple_excerpt_lengths' );

The example above changes the excerpt length on the front page, but WordPress comes with numerous conditional tags we can use in our themes, like is_category and is_archive.

We can even add extra conditional cases to the code above. Let’s say we want a short excerpt on the front page, a longer one on author pages, and the default everywhere else.

function jp_multiple_excerpt_lengths($length) {
	if ( is_front_page() ) {
		return 15;

	} elseif ( is_author() ) {
		return 65;

	} else {
		return 55;

	}
}
add_filter( 'excerpt_length', 'jp_multiple_excerpt_lengths' );

Prevent Contact Form 7 from loading on every page

Contact Form 7 is a great contact form plugin for WordPress, but it loads its Javascript and CSS on every page on your site, even when you don’t have a contact form there. Let’s fix that by loading the scripts and styles only on the pages that need them.

Add the following code to your theme or plugin to load the scripts and styles only on pages that have a contact form.

<?php
/**
 * Contact Form 7
 *
 * Prevent the javascript and styles from loading on every page
 * Load them only on the Contact page
 */

/* Removes the Contact Form 7 scripts and styles from all pages */
remove_action( 'wp_enqueue_scripts', 'wpcf7_enqueue_scripts' ); // Prevents the scripts from loading on all pages
remove_action( 'wp_enqueue_scripts', 'wpcf7_enqueue_styles' ); // Prevents the styles from loading on all pages

/* Adds the Contact Form 7 scripts and styles to the appropriate pages */
add_action( 'wp_enqueue_scripts', 'new_cf7_loader' ); // Loads the scripts and styles on the appropriate page(s)


/* Load the Contact Form 7 scripts and styles on the appropriate pages */
function new_cf7_loader() {
  if ( is_page( array( 'contact', 'another page' ) ) ) { // Add all pages here that contain the contact form
    wp_enqueue_style( 'contact-form-7', wpcf7_plugin_url( 'styles.css' ), array(), WPCF7_VERSION, 'all' );
    wp_enqueue_script( 'cf7.jquery.form.js', wpcf7_plugin_url( 'jquery.form.js' ), array(), WPCF7_VERSION, true );
    wp_enqueue_script( 'cf7.scripts.js', wpcf7_plugin_url( 'scripts.js' ), array(), WPCF7_VERSION, true );
  }
}
?>

It’s called “your” password for a reason

A few months ago there were reports all over the news about companies demanding the Facebook passwords of prospective employees, and still today I find people talking about it. There are various opinions about this practice; some people are for it, and some people are against it. My position is this: you should NEVER give your Facebook password to any company, for any reason, ever. In fact, you should never give any of your passwords to any company, even if it’s the password to access your work computer. Huh? Let me explain.

Many people think of a password as simply a lock that prevents unauthorized access to a system or service. That is only one function of a password. A password not only protects a company’s assets; it also protects YOU. If you give your password to someone else and something bad happens, such as an accident or intentional malicious behavior, you could be held liable. For example, let’s say you’re doing great work at your job, upper management is starting to notice you, and your supervisor feels threatened. If your supervisor knows your desktop password because “it’s company property”, this gives him or her the ability to do things on the computer on your behalf. It looks like you did it. Besides, a supervisor wouldn’t need your password on a properly-configured computer.

In the case of Facebook, I shouldn’t have to tell you why it’s a bad idea to give your password to an employer or prospective employer. But I will.

First, if you do, you’re violating the Facebook Terms of Service (section 4, paragraph 8) and your account could be suspended.

8. You will not share your password (or in the case of developers, your secret key), let anyone else access your account, or do anything else that might jeopardize the security of your account.

Second, I must restate that a password is for your protection. If you give your password to someone else, and they do something bad, by mistake or on purpose, you could be embarrassed, inconvenienced, or even held liable for malicious behavior.

“If you set the precedent that your private life is open to inspection by your employer, everything is fair game.”

Third, and most importantly, precedent. If you give your Facebook password to an employer, you’re setting a very dangerous precedent for not just you, but for all of us. What’s next? The password to your home computer so they can dig through your files and internet history to see what you think and do? A key to your front door so they can walk into your house when they please so they can see what you do in your private life? If you set the precedent that your private life is open to inspection by your employer, everything is fair game. Would you invite your employer into your home to look through your personal photo albums, eavesdrop on your private conversations, and look through your address book to see who you call friends? Of course you wouldn’t; the very notion is absurd.

There are probably dozens of reasons why you should not give your Facebook password, or any other personal password, to your employer or prospective employer, but the three listed above should be more than enough for any reasonably sane person.

Protect yourself and do not give your passwords to your employer.