WordPress tip: How to get the first link in post

October 7, 2013 at 3:41 pm

Remember when I shown you how to get the first image from a post? Today, I’m going to let you know how you can easily grab the first link from the post content.

Put the function below in your functions.php file, then use it inside the loop in your template files.

function get_link_url() {
    $content = get_the_content();
    $has_url = get_url_in_content( $content );

    return ( $has_url ) ? $has_url : apply_filters( 'the_permalink', get_permalink() );
}

Thanks to Filip Stefansson for the tip!

By the way, I just created a new service called WPCAT, where I offer my help to fix or enhance your WordPress site for a cheap rate. Don’t wait and contact me right now if you need any help with WordPress!

How to change author url base on your WordPress site

October 2, 2013 at 4:23 pm

In WordPress, author profile are by default accessible using the url yoursite.com/author/name. But what if you want to use the term “profile” instead of “author” in the url? Here is a handy recipe to do so.

Pasting the following code on your functions.php file will change the default yoursite.com/author/name to yoursite.com/profile/name.
Replace profile on line 4 by any slug you want.

add_action('init', 'cng_author_base');
function cng_author_base() {
    global $wp_rewrite;
    $author_slug = 'profile'; // change slug name
    $wp_rewrite->author_base = $author_slug;
}

Thanks to Kevin Chard for the cool tip!

WordPress tip: author bio excerpt

September 26, 2013 at 7:07 pm

Would you like to be able to show a short intro of the author bio instead of his complete bio? Here’s a code snippet to make an excerpt of the bio, with a link to the author page, where you can show the full bio.

Let’s start by creating the function. The code below have to be pasted into your functions.php file.

<?php
	function author_excerpt (){	                     					
		$word_limit = 20; // Limit the number of words
		$more_txt = 'read more about:'; // The read more text
		$txt_end = '...'; // Display text end 
		$authorName = get_the_author();
		$authorUrl = get_author_posts_url( get_the_author_meta('ID'));
		$authorDescriptionShort = wp_trim_words(strip_tags(get_the_author_meta('description')), $word_limit, $txt_end.'<br /> '.$more_txt.' <a href="'.$authorUrl.'">'.$authorName.'</a>');
		return $authorDescriptionShort; 		
	}
?>

Once done, you can use the function. To do so, use the code below where you’d like to display an author bio excerpt:

<?php  if (function_exists('author_excerpt')){echo author_excerpt();} ?>

Thanks to Tim Marcher for submitting this recipe!

How to paginate WordPress like Dribbble

September 24, 2013 at 5:46 pm

Are you using Dribbble? If you checkout a user on Dribbble there are two shots or thumbnails listed on the right hand side that will paginate the previous or next shot uploaded by the user. Here is a recipe to replicate this functionality on your WordPress site.

Just paste the code below on your single.php file, where you want to display the dribbble-like pagination:

<?php $prev = get_previous_post(); $next = get_next_post();
						
	<div class="float--left folio">
		<a href="<?php echo get_permalink($prev->ID); ?>" title="<?php echo esc_attr($prev->post_title); ?>">
			<?php echo get_the_post_thumbnail($prev->ID, 'thumbnail'); ?>
		</a>
	</div>
						
	<div class="float--right folio">
		<a href="<?php echo get_permalink($next->ID); ?>" title="<?php echo esc_attr($next->post_title); ?>">
			<?php echo get_the_post_thumbnail($next->ID, 'thumbnail'); ?>
		</a>
	</div>

This recipe has been submitted by Elliott Richmond. Thanks!

Remove “Plugins” and “Other News” widgets from WordPress dashboard

September 17, 2013 at 5:02 pm

If for some reason you don’t like to see the “plugins” and “other news” dashboard widget, here’s a way to remove them using some easy WordPress hooks.

Simply paste the code below in your functions.php file. Once you saved the changes, the “plugins” and “other news” widgets will not be shown again.

//Remove unwanted widgets from Dashboard
function remove_dashboard_widgets() {
	global$wp_meta_boxes; 
	unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_plugins']);
	unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_secondary']);
}
add_action('wp_dashboard_setup', 'remove_dashboard_widgets');

Thanks to WP Guru for the code!

WordPress hack: Efficient SEO without a plugin

September 11, 2013 at 4:47 pm

Sure, there’s lots of great WordPress plugins to help you with SEO. But in case you don’t want to use a plugin, here’s a super efficient code make your blog SEO friendly.

Let’s start by pasting the code below into your functions.php file:

function basic_wp_seo() {
	global $page, $paged, $post;
	$default_keywords = 'wordpress, plugins, themes, design, dev, development, security, htaccess, apache, php, sql, html, css, jquery, javascript, tutorials'; // customize
	$output = '';

	// description
	$seo_desc = get_post_meta($post->ID, 'mm_seo_desc', true);
	$description = get_bloginfo('description', 'display');
	$pagedata = get_post($post->ID);
	if (is_singular()) {
		if (!empty($seo_desc)) {
			$content = $seo_desc;
		} else if (!empty($pagedata)) {
			$content = apply_filters('the_excerpt_rss', $pagedata->post_content);
			$content = substr(trim(strip_tags($content)), 0, 155);
			$content = preg_replace('#\n#', ' ', $content);
			$content = preg_replace('#\s{2,}#', ' ', $content);
			$content = trim($content);
		} 
	} else {
		$content = $description;	
	}
	$output .= '<meta name="description" content="' . esc_attr($content) . '">' . "\n";

	// keywords
	$keys = get_post_meta($post->ID, 'mm_seo_keywords', true);
	$cats = get_the_category();
	$tags = get_the_tags();
	if (empty($keys)) {
		if (!empty($cats)) foreach($cats as $cat) $keys .= $cat->name . ', ';
		if (!empty($tags)) foreach($tags as $tag) $keys .= $tag->name . ', ';
		$keys .= $default_keywords;
	}
	$output .= "\t\t" . '<meta name="keywords" content="' . esc_attr($keys) . '">' . "\n";

	// robots
	if (is_category() || is_tag()) {
		$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
		if ($paged > 1) {
			$output .=  "\t\t" . '<meta name="robots" content="noindex,follow">' . "\n";
		} else {
			$output .=  "\t\t" . '<meta name="robots" content="index,follow">' . "\n";
		}
	} else if (is_home() || is_singular()) {
		$output .=  "\t\t" . '<meta name="robots" content="index,follow">' . "\n";
	} else {
		$output .= "\t\t" . '<meta name="robots" content="noindex,follow">' . "\n";
	}

	// title
	$title_custom = get_post_meta($post->ID, 'mm_seo_title', true);
	$url = ltrim(esc_url($_SERVER['REQUEST_URI']), '/');
	$name = get_bloginfo('name', 'display');
	$title = trim(wp_title('', false));
	$cat = single_cat_title('', false);
	$tag = single_tag_title('', false);
	$search = get_search_query();

	if (!empty($title_custom)) $title = $title_custom;
	if ($paged >= 2 || $page >= 2) $page_number = ' | ' . sprintf('Page %s', max($paged, $page));
	else $page_number = '';

	if (is_home() || is_front_page()) $seo_title = $name . ' | ' . $description;
	elseif (is_singular())            $seo_title = $title . ' | ' . $name;
	elseif (is_tag())                 $seo_title = 'Tag Archive: ' . $tag . ' | ' . $name;
	elseif (is_category())            $seo_title = 'Category Archive: ' . $cat . ' | ' . $name;
	elseif (is_archive())             $seo_title = 'Archive: ' . $title . ' | ' . $name;
	elseif (is_search())              $seo_title = 'Search: ' . $search . ' | ' . $name;
	elseif (is_404())                 $seo_title = '404 - Not Found: ' . $url . ' | ' . $name;
	else                              $seo_title = $name . ' | ' . $description;

	$output .= "\t\t" . '<title>' . esc_attr($seo_title . $page_number) . '</title>' . "\n";

	return $output;
}

Once done, replace the $default_keywords (line 3) with your own and add the following code into your header.php file:

<?php echo basic_wp_seo(); ?>

Then, don’t forget to check the source of your pages and fine tune the code if needed.

Big thanks to Jeff Starr for this amazing code!

How to automatically add Gravatars for the post author

September 9, 2013 at 6:38 pm

Would you like to be able to automatically add the author gravatar to each post? It is really easy to do with this handy recipe.

Simply paste the following code where you’d like the author Gravatar to be displayed. Please note that this code must be used within the loop.

<?php echo get_avatar( get_the_author_email(), '80' ); ?>

Thanks to Emoticode for the tip!

How to load jQuery from Google CDN

August 23, 2013 at 5:31 pm

By default, WordPress load its own copy of jQuery in your theme. But what about loading the library from Google CDN? Here’s an easy way to do it.

Paste the code below into your functions.php file:

function jquery_cdn() {
   if (!is_admin()) {
      wp_deregister_script('jquery');
      wp_register_script('jquery', 'http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js', false, '1.8.3');
      wp_enqueue_script('jquery');
   }
}
add_action('init', 'jquery_cdn');

Once you saved the file, WordPress will load jQuery from Google CDN.

Thanks to WP Tips.ru for the recipe!

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!