WordPress tip: Email alert for 404s

August 19, 2013 at 3:52 pm

As a conscious web developer/website owner, you probably want to keep track of 404 pages on your website or blog. Instead of having to manually dig in the log files, what about an automatic script for reporting 404 errors via email?

To implement, simply include this script at the top of your theme’s 404.php file. If your theme don’t have a 404.php file, then create it.

<?php // WP 404 ALERTS @ http://wp-mix.com/wordpress-404-email-alerts/

// set status
header("HTTP/1.1 404 Not Found");
header("Status: 404 Not Found");

// site info
$blog  = get_bloginfo('name');
$site  = get_bloginfo('url') . '/';
$email = get_bloginfo('admin_email');

// theme info
if (!empty($_COOKIE["nkthemeswitch" . COOKIEHASH])) {
	$theme = clean($_COOKIE["nkthemeswitch" . COOKIEHASH]);
} else {
	$theme_data = wp_get_theme();
	$theme = clean($theme_data->Name);
}

// referrer
if (isset($_SERVER['HTTP_REFERER'])) {
	$referer = clean($_SERVER['HTTP_REFERER']);
} else {
	$referer = "undefined";
}
// request URI
if (isset($_SERVER['REQUEST_URI']) && isset($_SERVER["HTTP_HOST"])) {
	$request = clean('http://' . $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"]);
} else {
	$request = "undefined";
}
// query string
if (isset($_SERVER['QUERY_STRING'])) {
	$string = clean($_SERVER['QUERY_STRING']);
} else {
	$string = "undefined";
}
// IP address
if (isset($_SERVER['REMOTE_ADDR'])) {
	$address = clean($_SERVER['REMOTE_ADDR']);
} else {
	$address = "undefined";
}
// user agent
if (isset($_SERVER['HTTP_USER_AGENT'])) {
	$agent = clean($_SERVER['HTTP_USER_AGENT']);
} else {
	$agent = "undefined";
}
// identity
if (isset($_SERVER['REMOTE_IDENT'])) {
	$remote = clean($_SERVER['REMOTE_IDENT']);
} else {
	$remote = "undefined";
}
// log time
$time = clean(date("F jS Y, h:ia", time()));

// sanitize
function clean($string) {
	$string = rtrim($string); 
	$string = ltrim($string); 
	$string = htmlentities($string, ENT_QUOTES); 
	$string = str_replace("\n", "<br>", $string);

	if (get_magic_quotes_gpc()) {
		$string = stripslashes($string);
	} 
	return $string;
}

$message = 
	"TIME: "            . $time    . "\n" . 
	"*404: "            . $request . "\n" . 
	"SITE: "            . $site    . "\n" . 
	"THEME: "           . $theme   . "\n" . 
	"REFERRER: "        . $referer . "\n" . 
	"QUERY STRING: "    . $string  . "\n" . 
	"REMOTE ADDRESS: "  . $address . "\n" . 
	"REMOTE IDENTITY: " . $remote  . "\n" . 
	"USER AGENT: "      . $agent   . "\n\n\n";

mail($email, "404 Alert: " . $blog . " [" . $theme . "]", $message, "From: $email"); 

?>

Thanks to WP Mix for this useful snippet!

Giveaway: 3 premium themes from ThemeFuse

August 15, 2013 at 5:05 pm

It’s been a long time without a giveaway here at WPRecipes! So today, I have 3 awesome premium WordPress themes from our partner ThemeFuse for you to win. Joining the giveaway is free and easy, so come in!

A word about ThemeFuse

ThemeFuse is one of the most popular actors in the premium WordPress themes market. They provide lots of themes with a great design and solid code.
They have lots of different designs for lots of different needs, so you should definitely have a look to their website!

How to join the giveaway?

Joining the giveaway is free and easy. First, have a look to ThemFuse themes and find the theme you’d like to win. Then, simply leave a comment below to let me know which theme you want.

In one week (thursday 22) I’ll randomly pick 3 winners. They’ll receive the chosen theme directly from ThemeFuse staff.

Good luck everyone!

WordPress tip: View all WP query variables

August 7, 2013 at 11:27 am

When coding a WordPress theme or plugin, it may be very useful to be able to output all WP Query variables. Here is a short code snippet to do it easily.

Paste the code below on any files, where you’d like to display the WP Query variables.

global $wp_query;
var_dump($wp_query->query_vars);

Thanks to WP Mix for the tip!

WordPress tip: Conditional tag for blog-related pages

July 24, 2013 at 4:18 pm

If you’re using WordPress both as a blog and as a more traditional website, you might need to be able to detect when you’re currently on a blog-related page. Here is a custom conditional tag to do so.

Copy the function below and paste it into your functions.php file:

function is_blog() {
	if (is_home() || is_singular('post') || is_post_type_archive('post'))
		return true;
	else return false;
}

Once done, you can use the function to detect if you’re currently on a blog related page:

<?php
if(is_blog()) {
    //the page is blog related
}
?>

Thanks to Nathan for the tip!

WordPress function to check if the current post is a custom post type

July 19, 2013 at 3:34 pm

Introduced in version 3.0, custom post types allows you to hold and display many different types of content. Today’s recipe is super useful for those who frequently work with custom post types: a function to check if the current post is a custom post type.

Simply paste this code into your functions.php file:

function is_custom_post_type() {
	global $wp_query;
		
	$post_types = get_post_types(array('public'   => true,'_builtin' => false),'names','and');
	
	foreach ($post_types  as $post_type ) {
		if (get_post_type($post_type->ID) == get_post_type($wp_query->post->ID)) {
			return true;
		} else {
			return false;
		}
	}
}

Once done, you can use the function as shown below. Please note that the function can be used outside the loop:

if (is_custom_post_type()) {
    //Current post is a custom post type
}

Thanks to Jonas Ethomsen for the function!

WordPress SEO: Automatically remove short words from the URL

July 16, 2013 at 5:20 pm

By default, WordPress generate the slug (url) from your post title. It do not remove any words, even shorter ones which don’t add any value to your seo. Here’s a super useful tip to automatically remove short words from your urls.

Paste the code below into your functions.php file. Once you saved the file, WordPress will automatically remove short (less than 3 characters) words from the generated permalink.

add_filter('sanitize_title', 'remove_short_words');
function remove_short_words($slug) {
    if (!is_admin()) return $slug;
    $slug = explode('-', $slug);
    foreach ($slug as $k => $word) {
        if (strlen($word) < 3) {
            unset($slug[$k]);
        }
    }
    return implode('-', $slug);
}

Thanks to Kevin Chard for this awesome piece of code!

WordPress tip: check if a post has a gallery

July 11, 2013 at 4:38 pm

Sometimes it can be useful to quickly be able to check if a post has a gallery. Here is a handy function to do it.

Simply paste the function below into your functions.php file:

function hasgallery(){
    global $post;
    return (strpos($post->post_content,'[gallery') !== false);
}

Once done, you can use it to detect if a post has a gallery.

Thanks to WordPress Stackexchange for the cool tip!

WordPress tip: Check whether a plugin is active

July 9, 2013 at 10:05 pm

When developing a WordPress theme or plugin, it can be really useful to be able to know if a specific plugin is activated. Here is a simple way to check wheher a plugin is activated or not.

Just paste the code below in your theme file, wherever you’d like to check out if a plugin is activated or not. Update the code with the plugin directory and name on line 4.

<?php 
   include_once( ABSPATH . 'wp-admin/includes/plugin.php' );

   if(is_plugin_active('plugin-directory/plugin-file.php')) {
      //plugin is activated
   }
?>

Thanks to Jean galea for this brillant tip!

WordPress tip: Force specific pages to be SSL secure

July 2, 2013 at 3:26 pm

If SSL is enabled on your webserver, you should definitely use it to protect your blog. Activating SSL on your specific pages on a WordPress blog is definitely easy: Just read on.

Just add the following snippet to the functions.php file of your WordPress theme and specify the post or page ID desired.

function wps_force_ssl( $force_ssl, $post_id = 0, $url = '' ) {
    if ( $post_id == 25 ) {
        return true
    }
    return $force_ssl;
}
add_filter('force_ssl' , 'wps_force_ssl', 10, 3);

Thanks to Kevin Chard for this recipe!

How to crop uploaded images instead of scaling them

June 26, 2013 at 5:59 pm

Would you like to crop your thumbnails instead of scaling them? If yes, I have a very handy snippet for you today. Just read on and enjoy!

Just add the code below to your functions.php file:

// Standard Size Thumbnail
if(false === get_option("thumbnail_crop")) {
     add_option("thumbnail_crop", "1"); }
     else {
          update_option("thumbnail_crop", "1");
     }

// Medium Size Thumbnail
if(false === get_option("medium_crop")) {
     add_option("medium_crop", "1"); }
     else {
          update_option("medium_crop", "1");
     }

// Large Size Thumbnail
if(false === get_option("large_crop")) {
     add_option("large_crop", "1"); }
     else {
          update_option("large_crop", "1");
      }

Thanks to wp-snippet.com for the tip!