Thoughts Of A North Wales Web Developer

About James Jackson

North Wales based web developer working for Livetech
Google+

Blog Url Plugin

When working on new sites with wordpress it is common to work on a development server and accessing it using the account name as opposed to the main url of the site. i.e. some severs use the following form http://www.example.com/~account. When developing the site you may want to link to pages within the site, put images in posts… etc. I have developed a simple plugin that adds a blog url shortcode to wordpress which dynamically pulls in the url set in wordpress’ general settings. The usage is fairly simple

[blogurl] <!-- returns the url of the blog -->
[blogurl id=12] <!-- returns the permalink of page or post with id 12 -->
[blogurl temp=true] <!-- returns the themes url -->

If you would like to find out more or have support queries, please use the comment section below.

Role Scoper plugin for wordpress returning server error 500

Role scoper is a fantastic WordPress plugin, offering CMS like user roles. It allows you to specify what pages and posts individual members or groups of members can edit.

Just ran into this error. I installed role scoper and when I tried to activate the plugin, the entire wp-admin was returning a 500 Internal Server Error. After much searching I found the following simple fix.

The problem is that my hosting, with 1and1, uses php4 by default, what I needed to do was to tell the server to use php5 instead, which is the version that role scoper needs.

  1. Delete the role scoper directory and all of it’s contents from your remote plugin directory.
  2. Open your .htaccess file and paste in the following two lines
    AddType x-mapp-php5 .php
    AddHandler x-mapp-php5 .php

Re-install role scoper and activate the plugin and it should work.

If you have come across any other errors or find that this solution doesn’t work, please comment below.

Create Your Own WordPress Shortcodes

As we delve deeper and deeper into the possibilities of WordPress and we endeavor to create weird and wonderful themes for our blogs, clients or just for fun, one thing we are all striving for is allowing our users (including ourselves) a hassle free and intuitive system with which they can interact. As of version 2.5 we have been introduced to the magic of shortcodes. Shortcodes allow us to include, often complex, code with a simple key word. For example if you attach images to a post and want to show them in the post as a Gallery you can use the following simple code in the post editor
[gallery]
This will create all the necessary (X)HTML for your gallery.
Not only are shortcodes often overlooked, but few know that you can easily create your own without messing with the WordPress core.

Creating a Simple Short Code

The first thing you will need for this is a functions.php file in your theme directory. If you don’t have one you can create one, no extra code is needed just a blank file saved as function.php.
We are going to create a shortcode that returns the url of your blog.

function myUrl() { // Define function that will be used by the shortcode
      return get_bloginfo( 'URL' ); // WordPress function that returns the URL of the blog
}
add_shortcode('blogurl', 'myUrl'); // Add the shortcode to the theme, in this format add_shortcode('shortcode_name', 'function_to_execute' )

Save and upload your functions.php and you can now use the shortcode [blogurl] which will return the full url of your/the blog.
This can be widely expanded upon, such as returning adsense code, and I plan to share some of mine in a later post.

Easy drop-down menus using css and a little javascript

A commonly used navigation system on a wide variety of websites is that of the drop-down menu. We all recognize them and as computer users we have all developed an almost instinctive understanding of there use. After a conversation with a colleague today I decided to write a short tutorial, explaining their anatomy and how to use them.

The (semantics) Basics

As we all know we should endeavor to create clean and semantic documents for online publication. As your navigation is simply a list of links, then what better element to use than the simple unordered list.

<ul id="nav">
      <li><a href="home.php">Home</a></li>
      <li><a href="about.php">About</a></li>
      <li><a href="links.php">Links</a>
            <ul>
                  <li><a href="favorites.php">Favorites</a></li>
                  <li><a href="blogroll.php">Blogroll</a></li>
            </ul>
      </li>
      <li><a href="contact.php">Contact</a></li>
</ul>

As you can see our navigation is an unordered list with an extra list in one of the list items, this creates our second level.

Add a Bit of Style

#nav{
	margin:0;
	padding:0;
	list-style-type:none;
	}
#nav li{
	display:block;
	float:left;
	width:100px;
	line-height:20px;
	background:#CCC;
	padding:5px;
	margin:0 5px 0 0;
	position:relative;
	}
#nav li ul{
	list-style-type:none;
	margin:0;
	padding:0;
	width:150px;
	position:absolute;
	left:0px;
	top:30px;
	display:none;
	}
#nav li:hover ul, #nav li.hover ul{
	display:block;
	}	

For this next step I will assume that you know what the bulk of the css is doing, essentially it creates your horizontal navigation and adds some (in this case boring) style.

The portion that we are interested in is what we do with the top level and lower level lists in respect to positioning. You want to position the sub navigation below it’s parent, to do this we use a little absolute positioning, setting it to the furthest left point (0px) and the top to the bottom of the list item (height 20px + 10px padding = 30px). One thing to note is that you need to position the top level relatively otherwise the list will try and position itself to the nearest relatively positioned ancestor (quite often the body tag).

Setting the display property of the sub navigation to none, hides it in its natural state. Next we invoke the hover pseudo class of the li to set the sub navigation’s display property to block. This will work now in most modern browsers, however in the usual suspects (i.e.6 et. al.) you can only use the hove pseudo class of an anchor tag. So it’s time to play with some javascript

Javascript / JQuery

In order to get this to work in all browsers, you need to account for some lack of support for the hover pseudo class on the li tag. There are a number of ways to do this, you can use the acclaimed Son of Suckerfish or, like I have, you can write your own code.

(function($) {
    $(document).ready(function(){ /* Wait for the document to have finished loading */
		$('#nav li').mouseover(function(){ /* Apply the function to the mouse over event of the li's within the nav */
			$(this).addClass('hover'); /* Add the hover class to the element */
		}).mouseout(function(){ /* Apply the function to the mouse out event of the li's within the nav */
			$(this).removeClass('hover'); /* Remove the hover class to the element */
		});
});
})(jQuery);

This code uses JQuery and it’s basic function is to add a class to the li on mouse over and remove it on mouse out. The code is explained in the comments.

And finally… an example

This post is designed to offer a basic introduction into this topic and can more than likely be improved upon or expanded, if you have any comments or suggestions then please leave a comment below.

And here is the example… have fun

Getting Started Curing Divitis

As this is the first post on this blog I would like to start as mean to go on and share a little ditty that I have found helpful during my web development career.

As we all know the <table> tag should be used for nothing but tabular data, in fact our (X)HTML documents should be used to show data only, we should not be styling anything here, everything should be done from external css files.

The problem that we all invariably meet with the transition from tables to css is fully understanding what we are trying to achieve and the way in which we should be approaching things. There are three possible aspects to creating an accessible and well designed website and they should be approached in the following order.

Write page

Including a logical heading hierarchy and sticking strictly to semantic (X)HTML, i.e. Do not include any <div> tags at this stage as there inclusion is for style purposes and this is not needed right now.

Design site

After you have decided upon the structure that your page is going to take, you can now design it around this structure. A critical consideration of this stage is to realize your own limitations and design accordingly, trust me it will look and work better this way.

Style your site

Now it is time to get out your pen and paper and write down all of those widths, heights and color codes ready for the creation of your all important css. This stage is where we can all too often fall down and begin to bloat our fantastic designs with unnecessary <div> nesting. The question is, do we really need it? For example, if your design has a flat graphic as a header and then leads straight into navigation an obvious approach would be.

<div id="head"></div>
<div id="nav">
   <ul>
      <li>Home</li>
      <li>About</li>
      <li>Contact</li>
   </ul>
</div> 

Where #head is for your graphic and #nav is for your navigation, why not simplify this and just have

<ul id="nav">
   <li>Home</li>
   <li>About</li>
   <li>Contact</li>
</ul> 

With this you can set the background image of the <ul id="nav"> to your header graphic, set the padding-top of the #nav to the height of the graphic and style the list accordingly. Also a simple display:block on #nav will allow you to add your widths and margins.