Uncategorized Web development Wordpress

WordPress Plugin: 404 Simple Redirect

You can download my WordPress Plugin: 404 Simple Redirect clicking here.

What does the plugin?

This plugin hooks the normal WordPress workflow in order to determine if the request is processing will cause a 404 HTTP error. In that case it prevents WordPress to do any other processing and sends the user to the page defined in the plugin options.

How do I install de plugin?

Just download it from the WordPress plugins repository, activate it and enjoy!

Plugin info

Current version: 1.0
Author: Jordi Plana
Release log:
1.0

  • First version
Web development Wordpress

WordPress Multisite: How to query posts from any site from the network

Scenario

WordPress Multisite allows us to have a network of blogs. Many times the sites from the network may be related, and also the information they contain.

Problem

What to do if you are developing a theme, or a plugin, for one of your sites and you want to query posts from another site?

Solution

You can use the function switch_to_blog() to change the database we are using. A way to do this would be:

  1. Save current blog_id
  2. Change blog_id with switch_to_blog function
  3. Do the query (query_posts, get_posts, etc)
  4. Restore original blog_id

Here is an example code snippet:

global $blog_id;
$original_blog_id = $blog_id;

//$new_blog_id should contain the blog_id we want to query
switch_to_blog( $new_blog_id );

//query instructions ...

switch_to_blog( $original_blog_id );

Comments, questions, suggestions?

Web development Wordpress

WordPress: How to redirect 404 errors to plain HTML pages

Scenario

Sites with high volume of content may have some moved, deleted or badly referenced content. This situation will cause a 404 http error.

Problem

In high performance sites the previously described situation can be a resource problem. It is not necessary that we load completely the site to show a 404 error page.

Solution

Loading a plain HTML page will result easier for our web server than loading a complete CMS instance.

With a few lines of code we can hook the WordPress normal loading in order to determine if a 404 error has been produced. If we detect this situation we will make a redirection to a plain HTML page, interrupting the normal WordPress loading process.

Copy and paste the next code snippet into your functions.php, in your theme folder. Create a new plain HTML, name it 404.html and upload it to the site’s root.

add_action('wp','determine_if_404'); 

function determine_if_404(&$arr){
    global $wp_query;

    if($wp_query->is_404){
        header('Location: '.get_bloginfo('siteurl').'/404.html');
        die;
    }
}

Comments, questions, suggestions?

Web development Wordpress

WordPress: Widgets anywhere using Custom Sidebars

Scenario

WordPress uses the theme defined sidebars as widget containers. In most of the themes you will find that sidebars are located on right/left hand of the post content. Maybe we will want to define sidebars in another position of the theme.

Problem

For example if we want to show the language selector widget in the header area. We should be able to define a custom sidebar in the header of the theme.

Solution

First of all we need to register a new sidebar in functions.php, in the root folder of our theme. We will use the register_sidebar WordPress function to do it. You can copy and paste the following code snippet:

<?php
//We hook our action on widgets_init
add_action('widgets_init','jplana_widgets_init');

function jplana_widgets_init(){
    $args = array(
        'name'          => 'Header Widget Zone',
        'id'            => 'header-widget-zone',
        'description'   => '',
        'before_widget' => '<div class="side-nav header-widget-zone">',
        'after_widget'  => '</div>',
        'before_title'  => '<h2 class="widgettitle">',
        'after_title'   => '</h2>' );
    register_sidebar($args);
}
?>

Now we have registered our new sidebar and we should be able to see it at the backend side. You can find the new sidebar in Appeareance > Widgets. We add and configure all widgets we want to show in that zone.

The next step is to render the new sidebar. We will use the WordPress function dynamic_sidebar. In our example, we open header.php and paste the next code snippet:

<?php if ( ! dynamic_sidebar( 'header-widget-zone' ) ) {} ?>

Now we can see our new sidebar showing the previous configured widgets.

Questions, comments, suggestions?

Web development Wordpress

WordPress: Add Category taxonomy support to Pages

Scenario

WordPress has a native taxonomy called Category, that is related with posts.

Problem

Sometimes we may want to add Category taxonomy support for other post types, in addition of posts.

Solution

A possible solution is to register the taxonomy Category and relate it with another post type (in the example I use pages).
Copy and paste the next piece of code in functions.php, in your theme’s root.

add_action('admin_init', 'reg_tax');
function reg_tax() {
      register_taxonomy_for_object_type('category', 'page');
      add_post_type_support('page', 'category');
}
Web development Wordpress

WordPress Plugin: Open Graph Metatags for Facebook

You can download my WordPress Plugin: Open Graph Metatags for Facebook clicking here.

What are Open Graph Metatags?

Open Graph is a protocol for customizing which information will be scrapped from your site, on Facebook sharing.

What does the plugin?

The plugin just adds the Open Graph metatags on your header, based on the title, content and thumbnail of your post.

How do I install de plugin?

Just download it from the WordPress plugins repository, activate it and enjoy!

Plugin info

Current version: 1.25
Author: Jordi Plana
Release log:
1.25

  • Added extra process to get first image attachment as sharing thumbnail, in case of post-thumbnail ausence.

1.21

  • Fixed a bug about thumbnail sharing.

1.1

  • Added extra sanitization of description
  • Added more Open Graph tags: type, url, locale and sitename.

TODO:

  • Develop an user interface to manually introduce which content will be shown.
  • Integration with WordPress SEO by Yoast plugin.
  • Integration with WPML.
Web development Wordpress

WordPress: How to add Open Graph tags for Facebook sharing

You can download my WordPress Plugin: Open Graph Metatags for Facebook clicking here.

Scenario

Nowadays most of the sites have social button for sharing its content. One of the most popular social network is Facebook.

Problem

By default most of the WordPress themes aren’t adapted to print Open Graph tags. This tags allow us to choose which information is scrapped by Facebook when someone shares a link.

Solution

There are some plugins that may cover this lack of functionality. I would suggest the ‘do it yourself’ solution, because of it’s performance.

You can find more information about Open Graph protocol in http://ogp.me.

Copy and paste the next code snippet inside the <head> tags, under the wp_head() function call. In most of the themes you will find this place in header.php, under the root folder of the theme.

<?php
//@JordiPlana.com OpenGraph Header Tags for Facebook
global $post;
if(is_singular()){
	?>
	<meta property="og:title" content="<?php echo $post->post_title; ?>" />

	<meta property="og:description"	content="<?php
		if(!empty($post->post_excerpt)){
			$exc = $post->post_excerpt;
		}else{
			$exc = strip_tags($post->post_content);
			if(strlen($exc)>160){
				$pos = absint(strpos($exc, ' ',160));
				if($pos==0){
					//if there's not any space we split
					$pos = 160;
				}
				$exc = substr($exc,0,$pos).' ...';
			}
		}
		echo $exc;
	?>" />

	<?php
	$thumb = get_post_thumbnail_id($post->ID);
	if(!empty($thumb)){
		?>
		<meta property="og:image" content="<?php
			//get the post thumbnail src
			$thumb = wp_get_attachment_image_src($thumb);
			if(!empty($thumb[0])){
				echo $thumb[0];
			} ?>" />
		<?php
	}
}
?>
Web development Wordpress

WordPress: How to update database URLs in a recently moved installation

Scenario

During the development stage we set up a test enviroment, so the client can see the new features. We can test the application in a server configuration real close to the final one. We test PHP configurations, Squid rules and we test the cache.

Once the development is done we establish a release date. This date we install the application in the final enviroment and we release it to the public.

Problem

The migration process between servers, often requires changing the WordPress installation URL. This means we have to update the database. We should replace all the ocurrences of the old domain with the new one.

Solution

We use to execute a SQL sentence that replaces old domain URLs with new domain ones. You can copy the next code to notepad, replace old_domain.com and new_domain.com with your values.
After that copy the sentence to your favourite database manager (phpMyAdmin, HeidiSQL, Navicat, etc…) and execute it.

Caution:
Before doing any database modification be sure to make a backup. In case of error you should restore the backup.

Remember that if your WordPress installation has plugins that uses the PHP serialize() function to store Array with data in the wp_options table, it’s possible that they stop working. This is because the URL replacement breaks the serialized Arrays index (because of they are based on the string length).

#posts
UPDATE wp_posts SET guid = REPLACE (guid, 'old_domain.com/', 'new_domain.com/');
UPDATE wp_posts SET post_excerpt = REPLACE (post_excerpt, 'old_domain.com/', 'new_domain.com/');
UPDATE wp_posts SET post_content = REPLACE (post_content, 'old_domain.com/', 'new_domain.com/');
UPDATE wp_postmeta SET meta_value = REPLACE (meta_value, 'old_domain.com/', 'new_domain.com/');

#comments
UPDATE wp_comments SET comment_author_url = REPLACE (comment_author_url, 'old_domain.com/', 'new_domain.com/');
UPDATE wp_comments SET comment_content = REPLACE (comment_content, 'old_domain.com/', 'new_domain.com/');
UPDATE wp_commentmeta SET meta_value = REPLACE (meta_value, 'old_domain.com/', 'new_domain.com/');

#links
UPDATE wp_links SET link_url = REPLACE (link_url, 'old_domain.com/', 'new_domain.com/');
UPDATE wp_links SET link_rss = REPLACE (link_rss, 'old_domain.com/', 'new_domain.com/');

#options
UPDATE wp_options SET option_value = REPLACE (option_value, 'old_domain.com/', 'new_domain.com/');

#usermeta
UPDATE wp_usermeta SET meta_value = REPLACE (meta_value, 'old_domain.com/', 'new_domain.com/');

#posts
UPDATE wp_posts SET guid = REPLACE (guid, 'old_domain.com', 'new_domain.com');
UPDATE wp_posts SET post_excerpt = REPLACE (post_excerpt, 'old_domain.com', 'new_domain.com');
UPDATE wp_posts SET post_content = REPLACE (post_content, 'old_domain.com', 'new_domain.com');
UPDATE wp_postmeta SET meta_value = REPLACE (meta_value, 'old_domain.com', 'new_domain.com');

#comments
UPDATE wp_comments SET comment_author_url = REPLACE (comment_author_url, 'old_domain.com', 'new_domain.com');
UPDATE wp_comments SET comment_content = REPLACE (comment_content, 'old_domain.com', 'new_domain.com');
UPDATE wp_commentmeta SET meta_value = REPLACE (meta_value, 'old_domain.com', 'new_domain.com');

#Links
UPDATE wp_links SET link_url = REPLACE (link_url, 'old_domain.com', 'new_domain.com');
UPDATE wp_links SET link_rss = REPLACE (link_rss, 'old_domain.com', 'new_domain.com');

#options
UPDATE wp_options SET option_value = REPLACE (option_value, 'old_domain.com', 'new_domain.com');

#usermeta
UPDATE wp_usermeta SET meta_value = REPLACE (meta_value, 'old_domain.com', 'new_domain.com');