Newer posts are loading.
You are at the newest post.
Click here to check if anything new just came in.

May 13 2013

13:00

How These 11 WordPress Scripts Will Stop North Korea And Improve Your Blog

In this post you will get 11 WordPress scripts that will help improve your blog’s performance and that may or may not stop North Korea from terrorizing us.

Either way, you will find these little WordPress hacks very useful for your daily blogging needs, which can help you save lots of precious time!

If you’re eager to enhance or improve the functionality of your WordPress site, then below are powerful WordPress scripts that will definitely give you a great head start.

How These 11 WordPress Scripts Will Stop North Korea

Okay, these probably won’t stop North Korea from wreaking havoc, but you can’t blame a man for trying!

11 WordPress Scripts to Improve Your Blog…Right Now

On a serious note, these WordPress scripts will help you work smoothly on the back-end and will make your front-end more comfortable for your readers.

1. Add Post Meta Descriptions

WordPress does not support the use of meta description tags by default. Though metas have lost their significance in SEO, they can still influence the ranking of your blog in the search engines. To make your site search engine friendly, you should open the header.php file of your theme and copy and paste the code below to the space between the opening <head> and closing </head> tags.

<meta name="description" content="
 <?php if ( (is_home()) || (is_front_page()) ) {
 echo ('Your main description goes here');
 } elseif(is_category()) {
 echo category_description();
 } elseif(is_tag()) {
 echo '-tag archive page for this blog' . single_tag_title();
 } elseif(is_month()) {
 echo 'archive page for this blog' . the_time('F, Y');
 } else {
 echo get_post_meta($post->ID, "Metadescription", true);
 }?>">

As simple as it seems, this hack can help you enhance the SEO performance of your site today.

2. Split WordPress Content into Multiple Columns

Sometimes, you may want to create additional columns in your pages, but as we know, WP does not support this from the back-end. The best way of doing this is to use snippets to split page content. Locate your theme folder and add the following block of code within the opening and closing php tags.

// split content at the more tag and return an array
 function split_content() {
 global $more;
 $more = true;
 $content = preg_split('/<span id="more-d+"></span>/i', get_the_content('more'));
 for($c = 0, $csize = count($content); $c < $csize; $c++) {
 $content[$c] = apply_filters('the_conte
 t', $content[$c]);
 }
 return $content;
 }

Once you do this and save the edited file, you will need to locate the specific theme files and comment out both the_content() and call split_content() function.

3. Redirect 404 Pages to Home Page

Although 404 Error Pages are an integral aspect of WordPress, they lower your page rankings significantly, thus making your blog or site ineffective. To help you out of your predicament, I suggest you copy the following snippet code to the .htacess file to redirect the 404 error pages to your home page.

< IfModule mod_alias.c >
RedirectMatch 301 ^/search/$ http://your-site.com/
RedirectMatch 301 ^/tag/$ http://your-site.com/
RedirectMatch 301 ^/category/$ http://your-site.com/
< /IfModule >

This hack will redirect search engine spiders way from the 404 error page to the home page and in the process, improve your rankings and site’s reputation online.

4. Define Posts Displayed in Home Page

Majority of bloggers display posts in more or less them same way on the home pages of their blogs. This is understandable since WordPress does not feature a default option to define how posts should be displayed. Fortunately, you can surmount this problem by using custom fields. To define posts in your home page, you can use either the full post or post excerpt only. You will need to access your index.php file and replace the default loop with the following custom code:

<?php if (have_posts()) :
 while (have_posts()) : the_post();
 $customField = get_post_custom_values("full");
 if (isset($customField[0])) {
 //Custom field is set, display a full post
 the_title();
 the_content();
 } else {
 // No custom field set, let's display an excerpt
 the_title();
 the_excerpt();
 endwhile;
 endif;
 ?>

5. Insert Google Maps into Pages

Have you been using iframes to embed Google maps in your contact page or sidebars? If so, chances are high that it will be rendered poorly because of the interference of the visual editor. To save yourself the trouble of using iframes, I recommend that you copy and paste the following short codes into the function.php page.

//Google Maps Shortcode
 function fn_googleMaps($atts, $content = null) {
 extract (shortcode_atts (array (
 "width" => '640',
 "height" => '480',
 "src" => ''
 ), $atts));
return '<iframe width="'.$width.'" height="'.$height.'" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="'.$src.'"></iframe>';
}

Once you copy and paste the above code to your function.php, you will need to copy the short code below into the exact place where you want Google Maps to be displayed.

[googlemap width="200" height="200" src="[url]"]

6. Display Page Categories in a Drop Down Menu

Sometimes, you may want to add a cool drop down menu that will show all existing categories in your WordPress site. Here is a simple way of doing it. Copy and paste the code below into the index.php or sidebar.php file.

<form action="<?php bloginfo('url'); ?>/" method="get">
 <?php
 $select = wp_dropdown_categories('show_option_none=Select category&show_count=1&orderby=name&echo=0');
 $select = preg_replace("#<select([^>]*)>#", "<select$1 onchange='return this.form.submit()'>", $select);
 echo $select;
 ?>
 <noscript><input type="submit" value="View" /></noscript>
 </form>

7. Display Similar Posts without Plugins

It’s also possible to display similar posts in your blog so as to draw the attention of visitors and engage them. I know you will be tempted to use a plugin to display similar posts, but there is no need to overload your WordPress blog with plug ins when you can use tags and custom codes. Here is a powerful code to do display related posts quickly. Copy and paste it to the functions.php file.

<?php
 //for use in the loop, list 5 post titles related to first tag on
 //current post
 $tags = wp_get_post_tags($post->ID);
 if ($tags) {
 echo 'Related Posts';
 $first_tag = $tags[0]->term_id;
 $args=array(
 'tag__in' => array($first_tag),
 'post__not_in' => array($post->ID),
 'showposts'=>5,
 'caller_get_posts'=>1
 );
 $my_query = new WP_Query($args);
 if( $my_query->have_posts() ) {
 while ($my_query->have_posts()) : $my_query->the_post(); ?>
 <p><a href="<?php the_permalink() ?>" rel="bookmark" title="
 Permanent Link to <?php the_title_attribute(); ?>">
 <?php the_title(); ?></a></p>
 <?php
 endwhile;
 }
 }
 ?>

8. Change WP Login Logo

If you are tired of seeing the same old default “log in” logo, then you can change that to display a logo of your choice. To change the logo, you need to create and copy the new custom logo.png image into the image file in your root folder then copy and paste the following code into your functions.php file.

// login page logo
 function custom_login_logo() {
 echo '<style type="text/css">h1 a { background: url('.get_bloginfo('template_directory').'/companylogo.png) 50% 50% no-repeat !important; }</style>';
 }
 add_action('login_head', 'custom_login_logo');

9. Display Number of Facebook Fans

If you have a Facebook fan page then chances are high that you are thinking of displaying the total number of Facebook likes to visitors who access your WordPress site.

<?php
 $page_id = "YOUR PAGE-ID";
 $xml = @simplexml_load_file("http://api.facebook.com/restserver.php?method=facebook.fql.query&query=SELECT%20fan_count%20FROM%20page%20WHERE%20page_id=".$page_id."") or die ("a lot");
 $fans = $xml->page->fan_count;
 echo $fans;
 ?>

In order for this code to work, you only need to replace your current page ID with your personal Facebook page id.

10. Create Custom Widgets

As much as WordPress themes come with various widgets, some users may want to add custom widgets that meets the needs of their blogs. If you are one of them, then here is a handy snippet for this purpose. Copy and paste the following code into your functions.php file.

class My_Widget extends WP_Widget {
 function My_Widget() {
 parent::WP_Widget(false, 'Our Test Widget');
 }
 function form($instance) {
 // outputs the options form on admin
 }
 function update($new_instance, $old_instance) {
 // processes widget options to be saved
 return $new_instance;
 }
 function widget($args, $instance) {
 // outputs the content of the widget
 }
 }
 register_widget('My_Widget');

11. Display Google Users’ Search Terms

Have you been trying to monitor and understand the search patterns of prospects and visitors who access your website from Google Search?If so, then you need a script that will display all terms that were searched by clients who came across your site. Paste the following code outside the header section.

<?php
 $refer = $_SERVER["HTTP_REFERER"];
 if (strpos($refer, "google")) {
 $refer_string = parse_url($refer, PHP_URL_QUERY);
 parse_str($refer_string, $vars);
 $search_terms = $vars['q'];
 echo 'Welcome Google visitor! You searched for the following terms to get here: ';
 echo $search_terms;
 };
 ?>

Conclusion

Though these codes appear simple, they are powerful code snippets that you can use to customize WordPress websites. You can use these codes to change the layout and functionality of your theme instantaneously and save yourself the trouble of line-by-line coding.

Do you know any WordPress scripts I have missed?

January 26 2012

21:00

5 Must-Know Tips for Better WordPress User Management

WordPress is a great tool to use for managing content. It’s really simple for developers, for managers and for users to use, especially when using a great theme. But if there is one thing that I think that needs to improve yet is its user management settings. I mean, even basic functions need to be coded, like editing a user’s profile fields, add roles, edit capabilities, export user data. All of those need plugins or a little coding to get working.

Here we’ll see 5 amazing and simple tips that you can use right now and greatly improve your user management and you can use all this additional data to serve your loyal readers better and provide more focused content.

We’ll cover custom user fields to history functionality, and while we’re at it you’ll learn a lot about WordPress user management functions.

So, let’s rock!

What will we see?


We’ll focus on user specific data, so what we’ll see is:

  1. How to add extra registration fields
  2. How to add and remove profile fields
  3. Export the user data
  4. Bulk export users data
  5. Create a web history of the user

1. How to add extra registration fields


Sometimes we want to know our users better. Where they live, if they want to receive our newsletter, or just store their IP. Well, to get this working the easiest way is to use one of these:

PieRegister

It allows you to customize the user registration screen. Pretty useful and easy to change.

What I like most is its simplicity. If you want to just get started, this one is your best choice.

s2Member

On the other side, you may use a plugin like s2member which gives you full control over user’s capabilities and data. But it requires a little more setup and will change a lot of things in your wp-admin. So you can edit each posts visibility for free or premium users.

Maybe it’s too much if you just want to add more user registration fields, but it gives you much better control over user data and content.

2. How to add and remove profile fields


Truth be told, the default user profile fields are more markup than anything useful. But WordPress has an easy way to edit them.

Let’s say instead of Yahoo IM, you want to add a Twitter field. Or just remove it. Well, this can be done with the filter ‘user_contactmethods’, which can be added as plugin or in your theme’s functions.php file. Do it this way:

<!--?php--><?php
	function wd_edit_fields( $contactmethods ) {
		//new fields
		$contactmethods['twitter'] = 'Twitter';
		$contactmethods['tumblr'] = 'Tumblr';
		$contactmethods['skype'] = 'Skype';

		unset($contactmethods['aim']);
		unset($contactmethods['jabber']);
		unset($contactmethods['yim']);

		return $contactmethods;
	}
	add_filter('user_contactmethods','wd_edit_fields',10,1);
?>

3. Export the user data


Now we have saved user data, but it can’t be seen. What we’ll need is a function to export this when we want (in a custom page, for instance).

Our function will get user data directly from the wp_usermeta table, and remove unnecessary data (used just by WordPress, but keep them if you want to export all user data).

This function will do the trick:

<!--?php--><?php
	function get_all_user_meta( $id ) {
		$to_remove = array("first_name", "last_name", "nickname", "rich_editing", "comment_shortcuts", "admin_color", "show_admin_bar_front", "wp_capabilities", "wp_user_level", "wp_dashboard_quick_press_last_post_id", "wp_user-settings", "wp_user-settings-time", "closedpostboxes_post", "metaboxhidden_post"); //all default and undesired fields
		$remove = "";
		if (! empty($to_remove) && is_array($to_remove) ) {
			foreach ($to_remove as $item) {
				$remove .= "AND  meta_key != '$item' ";
			}
		}
		global $wpdb;
		$qry = "SELECT meta_key, meta_value FROM wp_usermeta WHERE user_id = $id $remove";
		$myrows = $wpdb->get_results( $qry );

		return $myrows;
	}

	$data = get_all_user_meta( 1 );
	foreach( $data as $field ) {
		if( ! empty ($field->meta_value) )
			echo "<strong>".$field->meta_key."</strong>: ".$field->meta_value."<br />";
	}
?>

4. Bulk export user data


Now if you want to move from WordPress to another CMS you may need to hire a programmer just to figure out how to export all user data. Ok, we’ll save you from this crazy work and give you a function to see all users with all their custom data!

What we have to do is get the previous function and run it for every user_id inside the WordPress wp_users table. Then you can import this content or format it according to your new CMS standards.

<!--?php--><?php
	function get_all_user_meta( $id ) {
		$to_remove = array("first_name", "last_name", "nickname", "rich_editing", "comment_shortcuts", "admin_color", "show_admin_bar_front", "wp_capabilities", "wp_user_level", "wp_dashboard_quick_press_last_post_id", "wp_user-settings", "wp_user-settings-time", "closedpostboxes_post", "metaboxhidden_post"); //all default and undesired fields
		$remove = "";
		if (! empty($to_remove) && is_array($to_remove) ) {
			foreach ($to_remove as $item) {
				$remove .= "AND  meta_key != '$item' ";
			}
		}
		global $wpdb;
		$qry = "SELECT meta_key, meta_value FROM wp_usermeta WHERE user_id = $id $remove";
		$myrows = $wpdb->get_results( $qry );

		return $myrows;
	}

	function get_all_user_ids() {
		global $wpdb;
		$qry = "SELECT ID FROM wp_users";
		$myrows = $wpdb->get_results( $qry );

		return $myrows;
	}

	$users = get_all_user_ids();

	foreach( $users as $user ) {
		$data = get_all_user_meta( $user->ID );
		echo "<h2>User #".$user->ID." data</h2>";
		foreach( $data as $field ) {
			if( ! empty ($field->meta_value) )
				echo "<strong>".$field->meta_key."</strong>: ".$field->meta_value."<br />";
		}
	}
?>

5. Create a web history of the user


Sometimes user behavior is completely nonlinear. I know, you publish your posts with a carefully thought out order, but people just click everywhere and sometimes they stop thinking “which one was the cool post I was reading 5 minutes ago?”.

Same thing may happen if you run an e-commerce store over WordPress. Users navigate through products and sometimes the very first one was the best for them.

So for these times, I would recommend you build a user history.

First you should define how many items you want to store, and this will depend on your site’s size. We’ll do this with the last 30 visited pages (easily changed).

Then, you’ll need to add this snippet to your functions.php or plugin file, then you just call show_history and you’ll be fine :)

<?php
add_filter('wp_head', 'wd_history');
function wd_history() {
	$current_user = wp_get_current_user();

	// logged in?
	if ( ! empty ($current_user->ID) ) {
		$i = get_user_meta($current_user->ID, "wd_i", true);
		$i++;

		//update fields
		update_user_meta($current_user->ID, "wd_i", $i);
		update_user_meta($current_user->ID, "wd_page_".$i, "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]");
		//delete older
		delete_user_meta( $current_user->ID, "wd_page_".($i-30) );
	}
}

function show_history() {
	$current_user = wp_get_current_user();

	// logged in?
	if ( ! empty ($current_user->ID) ) {
		$i = get_user_meta($current_user->ID, "wd_i", true);

		echo "<ul>";
		$end = $i - 30;
		while( $i > $end ) {
			$url = get_user_meta($current_user->ID, "wd_page_".$i, true);
			$pid = url_to_postid( $url );
			if ( ! empty ($pid) ) {
				$page = get_post($pid);
				$title = $page->post_title;
			} else {
				$title = "Home";
			}

			echo "<li><a href='$url'>$title</a></li>";

			$i--;
		}
		echo "</ul>";
	}
}
show_history();
?>

Other experiments


Did you know that it’s possible to create shopping cart functionality with user meta data? Well, we haven’t covered this because the article would get too big, but we will cover this soon :)

Now it’s your turn!


What do you think? Is there another user management function that you want to share with us? Just comment! :)

Older posts are this way If this message doesn't go away, click anywhere on the page to continue loading posts.
Could not load more posts
Maybe Soup is currently being updated? I'll try again automatically in a few seconds...
Just a second, loading more posts...
You've reached the end.
(PRO)
No Soup for you

Don't be the product, buy the product!

close
YES, I want to SOUP ●UP for ...