Code With Design

Web development, code, design, css, all around programming blog

Archive for the 'Web Development' Category

Creating multiple sidebar menus in wordpress

Creating multiple sidebar menus in wordpress

In this tutorial I won’t just be making a sidebar menu, but three additional menus that will be placed side by side in the footer. Unfortunately the code for this is not as easy as it may seem. The possibility of creating the sidebars through only your footer.php and sidebar.php is not going to happen. Fortunately the code is very simple and involves a lot of copy and pasting with a couple modifications.

For this tutorial I will be using wordpress 2.9.2 (currently the latest release)

When creating multiple “sidebars” You only need to do two things. The first thing is to edit the functions.php page, and the second is to edit the HTML where the sidebar/dynamic menu will be placed.

The first thing that we will be doing is opening our functions.php page. The code for one sidebar should look like this:

1
2
3
4
5
6
7
8
9
10
if ( function_exists('register_sidebar_widget') ){
    register_sidebar(array(
        'name'=> __('Sidebar', 'cwj'),
        'id' => 'normal_sidebar',
        'before_widget' => '<li id="%1$s" class="widget %2$s">',
        'after_widget' => '</li>',
        'before_title' => '<h2 class="widgettitle">',
        'after_title' => '</h2>',
    ));
}

The next thing we will need to do is modify this code so we can have four dynamic menus throughout our Wordpress theme. We will need to copy and paste the array and change out the name and the ID to something new and exciting. Because the menus will be placed in my footer I will be calling them “Footer1″ etc for each menu. The ID can be anything as long as it is unique to that specific menu. If you have two menus with the same ID you will only be able to add widgets to the last referenced menu.

The code should now look something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
if ( function_exists('register_sidebar_widget') ){
    register_sidebar(array(
        'name'=> __('Sidebar', 'CJDesign-dark1'),
        'id' => 'normal_sidebar',
        'before_widget' => '<li id="%1$s" class="widget %2$s">',
        'after_widget' => '</li>',
        'before_title' => '<h2 class="widgettitle">',
        'after_title' => '</h2>',
    ));
    register_sidebar(array(
        'name'=> __('Footer1', 'CJDesign-dark1'),
        'id' => 'col_sidebar1',
        'before_widget' => '<li id="%1$s" class="widget %2$s">',
        'after_widget' => '</li>',
        'before_title' => '<h2 class="widgettitle">',
        'after_title' => '</h2>',
    ));
    register_sidebar(array(
        'name'=> __('Footer2', 'CJDesign-dark1'),
        'id' => 'col_sidebar2',
        'before_widget' => '<li id="%1$s" class="widget %2$s">',
        'after_widget' => '</li>',
        'before_title' => '<h2 class="widgettitle">',
        'after_title' => '</h2>',
    ));
    register_sidebar(array(
        'name'=> __('Footer3', 'CJDesign-dark1'),
        'id' => 'col_sidebar3',
        'before_widget' => '<li id="%1$s" class="widget %2$s">',
        'after_widget' => '</li>',
        'before_title' => '<h2 class="widgettitle">',
        'after_title' => '</h2>',
    ));
}

The next stage in this tutorial is to quickly and easily insert the line of code to display your newly created dynamic menus. Just place this into the HTML where you would like it to be.

1
2
<?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar('Footer3') ) : ?>
<?php endif; ?>

You simply need to replace where I have Footer3 with the name of the sidebar that you previously created with the function.

The sidebar now looks as follows:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<div class="footer-col">
	<ul>
		<?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar('Footer1') ) : ?>
  		<?php endif; ?>
    </ul>
</div>
<div class="footer-col">
	<ul>
		<?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar('Footer2') ) : ?>
        <?php endif; ?>
    </ul>
</div>
<div class="footer-col">
	<ul>
		<?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar('Footer3') ) : ?>
        <?php endif; ?>
    </ul>
</div>

And the widgets can be modified through the Wordpress widget page.

<img>

And the final product.

<img>

posted by Caleb in Web Development, php and have No Comments

Remove images and increase load times with CSS3

Remove images and increase load times with CSS3

Recently when working on a clients site I found that there were a lot of images being used when a quick little CSS fix could have done the trick. The basic outline of the site involved a few transparencies and a lot of drop shadows. The drop shadows were placed on the container/wrap and the text had drop shadows that had been all done in Photoshop. (Some of the drop shadows were not consistent.)

After looking at the site I told the client that within three minutes I could remove all of the images being for text, drop shadows and opacity allowing his site to load faster.

The answer was what I had expected and the following code was quickly being thrown into the site.

The Code

For the drop shadows on divs:

1
2
3
4
5
6
7
8
.container{
     width:1080px;
     margin:0 auto;
     -moz-border-radius:5px;
     -webkit-border-radius:5px;
     -moz-box-shadow: 0 2px 5px #666;
     -webkit-box-shadow: 0 2px 5px #666;
}

For the drop shadows on text:

1
2
3
4
5
h3{
	font-family: Times;
	letter-spacing: 1px;
	text-shadow: 2px 2px 2px #000;
}

For the opacity:

1
2
3
4
5
.sidebar ul{
	list-style-type:none;
	opacity: 0.9;
	width: 180px;
}
posted by Caleb in CSS, Web Development and have No Comments

PHP: Increasing a scripts runtime

PHP: Increasing a scripts runtime

Often when working with long php scripts you may need more time then is aloud. Recently when working with lirkr search engine I came across a problem where the server would time me out due to the script taking so long. The reason this was taking so long is because I had to read information from pages and load it into the search engines database word by word and check if the word already exists.

Here is a simple way to change the duration of time a script is aloud to be executed for.

1
2
3
<?php
ini_set(max_execution_time, "300");
?>

Be careful when using this script. If your code lasts longer then a few minutes then chances are the code is not very efficient, you have a infinite loop somewhere or you could just be working with a database that not a lot of people will ever have to use.

posted by Caleb in Web Development, php and have No Comments

Going through website creation part 2: Backend

Going through website creation part 2: Backend

This step mainly involves breaking the site up into sections of PHP. This will allow us to use various parts and pieces to make the site dynamic.

We may want to create a database and for this article I will be creating a very simple design that is just going to be used to display the information on the page. We will do this by adding a simple connect.php page that will contain the connection information.

1
2
3
4
5
6
7
8
<?php
 
$con = mysql_connect('database.website.com','username','password');
if (!$con) {
die('Could not connect: ' . mysql_error());
} mysql_select_db('databaseName');
 
?>

Now that we have successfully created our connect.php file we can move on to create a small piece of code we will include in the body of the page we created in the first tutorial. In this file we will be selecting information from our database.

1
2
3
4
5
6
7
8
9
10
11
<?php
 
include('connect.php');
$loader = mysql_query("SELECT * FROM table ORDER BY title") or die(mysql_error());
while($row = mysql_fetch_array($loader)) {
 
echo .$row[title].'<br>'.$row['description'].'<br>'.$row['link'].'<br>'.$row['quality'];
 
}
 
?>

Now we have successfully pulled information from our database, created a connection to the database and loaded the connection dynamically. Hopefully this will have helped you in your journey to create a dynamic website.

posted by Caleb in Web Development, php and have Comment (1)

Learning HTML in one short night

Learning HTML in one short night

Introduction

Open up your text editor because it is time to learn HTML in one night. If you are unfamiliar with HTML you should know that it is first and foremost a scripting language. Your server is going to send this file to the person’s computer. This person’s web browser; be it “Chrome”, ”Safari”, ”Opera”, ”Firefox” or “Internet Explorer” is then going to read the information and display it on screen.

This is an in depth look at HTML and how it is structured. In a future article I will include CSS with some CSS3.

HTML stands for hyper text markup language and is written in a standard text editor. (notepad for Windows, text edit for Macintosh) We are going to go ahead and start the tutorial now.

Please make sure that you have a folder, web browser and text editor open on your screen.

Building our first page

Open up your text editor; type in the following code and then save the file as “index.html” Be sure to write the extension. If it saves as “index.html.txt” then open up the folder that the file resides in, write click it go to “properties” on Window or “get info” on a Mac and remove the “.txt” extension.

1
2
3
4
5
6
7
    <html>
    <head>
    </head>
 
    <body>
    </body>
    </html>

The code above doesn’t contain anything special which is why you don’t see anything. In order to add content we are going to add a little more code to demonstrate some content on the screen but first we need to understand what was just added.

The first part of the above code tells the browser that inside is the HTML, in fact everything is until it reaches the closing statement or the bottom of the page. The head tags as you can see “<head></head>” tell the browser where the head information is. This information usually contains the title of the page, some meta data, style tags or links to css files and a little javascrip.

The tags below this are the body tags. Inside of here is where we will be putting all of the page contents which you will see shortly.

The next step is to add a line of code to your website. It is going to be at the very top and will have the type of charset, some meta data and the doctype. For now you do not need to know what it means but know that it is good practice and allows your code to be read properly.

1
2
3
4
5
6
7
8
9
10
    <!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
    <html xmlns=”http://www.w3.org/1999/xhtml”>
    <head>
    <meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″ />
    <head>
    </head>
 
    <body>
    </body>
    </html>

Now that we have the basic structure of the page setup we can go ahead and add a title. Like everything else it is set in tags and requires no formatting.

1
2
3
<head>
<title>Code With Design</title>
</head>

Note: The naming of the title is important. It will allow search engines and bots to easily find your content and increase your page rank. Make sure it covers what the page is about but don’t make it to long. Search engines don’t like that.

Testing your page

In order to test your page you just need to find the index.html file’s location on your hard disk and double click it. The page will open up in your default browser. If it does not you are going to need to associate a browser with that type of file. You can also open up your web browser and set the URL to the location of the file on your hard disk.

When it comes to testing to see if you have valid code it is as simple as using the W3C validation service offered here. You can test a page by using the URL, copying and pasting or uploading the file directly.

The body of the page

The next step in our quest for knowledge is getting text onto the screen. In order to accomplish this there are some ways to go about doing so. First there is just plain text which I would advise not doing do to styling complications later on. The next type of text is “paragraph” text. This text is displayed in <p></p> tags and is formatted to have spaces on the top of the first line and the bottom of the bottom line. The third main kind of text is heading tags, once again name these accordingly because it will help your pages chances of getting higher in the search engine.

1
2
3
4
5
6
<body>
<h1>This will be shown at an h1 level</h1>
<p>Here is som basic fill text that is only used to display what is needed.</p>
<h2>Difference in size</h2>
<p>As you can tell there is a difference in size between the heading 1 and heading 2 tags. This difference can be changed later on using page styles.</p>
</body>

The bit of code we are going to use is called a div. Div tags can be found throughout websites and are the main structure of formatting. Div tags can be of type class or id and are used with css to change how a website appears on screen. The following is the basic structure of how a div tag works.

1
2
3
4
<div class=”this_is_where_css_is_associated”>
<h1>Article title</h1>
<p>Content goes here.</p>
</div>

Note: The div tag can be formatted with CSS to change the location of this content on the screen along with colors, and size. Also, div tags are often associated with classes over id.

Menus and links

Menus in HTML are commonly created using unordered lists and list items. These tags like as follows <ul>unordered list</ul> and <li>List item</li>. When written these tags are nested meaning that the list item tags inside of the unordered list.

1
2
3
4
<ul>
<li>Menu item 1</li>
<li>Menu item 2</li>
</ul>

It isn’t going to be much of a menu if we cannot click the item and find ourselves on another page. In order to do this we are going to need to add the following code.

1
2
<a href=”http://codewithdesign.com/”>Code With Design</a>
<a href=”some-random-page.php”>Linking internally</a>
1
2
3
4
<ul>
<li><href=”http://codewithdesign.com”>codewithdesign.com</a></li>
<li><href=”http://lirkr.com”>Search engine</a></li>
</ul>

Initially the menu is going to look very bad but there is hope for it yet. The menu is styled this way as a default. Once you add css to the web page it will begin to look better.

Header rules are not as common as they once were do to the increase in proper CSS they were quickly replaced with borders on div tags. If you are looking for a heading rule then you can add the simple code below.

1
<hr></hr>

Note: There is not content inside of this header ruling for a reason. All that is required are the two tags and a line will be created.

Tables

Note: It is best practice to not use tables at all unless you are puling the information form a database. If you choose to make your website layout using tables note that it isn’t considered common practice, it will increase load times, they are messier to work with and all around a bad idea.

Tables are created using the <table></table> tags and consist of nested tags inside. The basic table will look something like this.

1
2
3
4
5
6
7
8
9
10
<table cellspacing=”5px” border=”1px”>
<tr>
<td>Row 1 col 1</td>
<td>Row 1 col 2</td>
</tr>
<tr>
<td>Row 2 col 1</td>
<td>Row 2 col 2</td>
</tr>
</table>

This table was made using cell spacing and a border just to let you see how it is all aligned. Like I have previously stated these are not a good idea unless displaying data and even then there are better ways then tables.

Forms

These are html components that can be used to send information to other pages on the site. These components range in abilities and are often used to pass a username and password to a site. The general way that it is written is as follows:

1
2
3
    <form action=”" method=”" enctype=”multipart/form-data” name=”form-name” id=”form-id”>
 
    </form>

Going through the top line it is obvious that we are going to need to add components within the form tags. The forms id and name should be changed to your choosing are commonly named the same. (ie: name=”form1″ id=”form1″) The action of the form is where you want the information to be posted. This usually means that it will be sent to PHP page.

Components for forms

Components are usually enclosed in label tags which means that you can add generic text that will be grouped with the item itself.

text area

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
    <label>
    <input type=”text” name=”text” id=”text”>
    <label>
 
    <label>
    <input type=”textarea” name=”textarea” id=”textarea”>
    <label>
 
    <label>
    <input type=”checkbox” name=”chkbox” id=”chkbox”>
    <label>
 
    <label>
    <input type=”radio” name=”radio” id=”radio”>
    <label>

Using the above format you can create items such as:

file input field “file”
hidden field to pass data without a field appearing on screen “hidden”
A submit button used to post the form “submit”

posted by Caleb in Web Development and have No Comments

Pre planning websites

Pre planning websites

Recently I was asked how I pre plan websites. This is how.

The first thing that is taken into the creation of any site is user access and ease of use. A lot of people are trying to create new and exciting things which I am all for but do not do this at your users experience’s expense. Keep it simple stupid is one of the greatest things when it comes to a design. A designer once stated that the best user experience is one they hardly notice. This is why it is a good idea to always think about them using your site over fun and exciting things you can add.

The second thing that you need to consider is the space requirements for everything that you need. Depending on the website you are going to want to keep the users coming back for more. The best way to do this is to not fill the area up with advertisements, a few are ok but if you have a horizontal ad space on the header don’t put one in your first post. Ads inside of content just make the readers annoyed and whatever you do, do not place and add in the very top right sidebar. This is a high clicking zone and you are going to want users to stay in your site as much as possible. Pre plan your menus, headers, content, footers and anything else you are adding into the site in an appropriate manner. Take margins into consideration because spacing can completely throw off a design.

The third thing you are going to want to accomplish is the overall design and layout of your items. This means that if you use a font for one size of header you should keep it consistent with that header inside of that area of the site. Meaning, if you are creating a blog, every post should have the same style of header. This does not mean that you should keep all of the heading fonts the same throughout the site. Some of the best designs on the net have different fonts in the header, sidebar and post content. Now getting away from fonts and back into the idea of layout and design I should add that the layout should stay to its theme. This means that you should not be changing the size of drop shadows between items and text, backgrounds should stay the same pertaining to their selected area and high traffic areas should contain a large amount of contrast. It will make the users eye go to that direction.

Simple rules you need to follow in order to give your site that design it needs:

Keep it simple stupid.
Be careful with ad placement.
Consistency is key.

posted by Caleb in Web Development and have No Comments

Creating a top button

Creating a top button

Introduction

Buttons that stick to the sides of the browser are becoming more and more popular. Most of it started with a companies feedback button which turned into people using them for their own convenience. In this article I am going to go over the CSS code for making it stick to the side.

The first thing that is required is an anchor. We can add this to the top of the page and it will be located just below the first div (or above your header) You can really locate this where ever you would like your readers eyes to be. Some people prefer the title of the first blog post.)

Once the code has been placed it should look like this:

1
2
3
<body>
<a name="top" id="top"></a>
<div class="header">

Anchors

Now that we have placed our anchor there is something you need to know if unfamiliar. In order to link to an anchor all you need to do is add a ‘name’ and an ‘id.’ When linking to an anchor you will be required to have an ‘href’ like so:

1
<a href="#top">Top</a>

Notice how the ‘href’ is simple a pound sign followed by the name of the anchor. If you would like a contact button the you can do something similar to the following anchor:

1
<h3><a href="http://codewithdesign.com/contact.php#contact">Contact</a>

Depending on where the anchor for contact is the user can be linked to a page and moved directly to the contact form. This just allows you to skip information and the only reason this is used as an example is because I wanted you to know exactly how you can use anchors across page.

Styling the page

Now that we have our two anchors we will get into the portion of styling that will make this button stick to the side. The first thing we are going to be doing is enclosing the link in a div. The results of this will look as such:

1
<div class="sidelinks-container"><h3><a href="#top">Top</a></h3></div>

The placement of this code is completely irrelevant since we will be adding a position property to it. I threw it directly under my anchor to keep it all together if changes are eventually needed.

Now we need to begin the actual styling of the div. This will require two separate styling, one for the div itself and the other will be for the ‘<h3>’ tag.

The first thing that we will be adding to our style will be the positioning and size of the box. As noted earlier the position and how the container acts with the sides of the browser is key. Because it is important we will start by adding the following lines of code to the div.

1
2
3
4
5
6
7
8
.sidelinks-container {
position:fixed;
left:0px;
width:50px;
height:100px;
bottom:40px;
overflow:hidden;
}

As we go through the code you will notice that everything is standard. The position is fixed so even though the div is outside of the container it is still properly displayed on the screen. The left property forces the box to the left side of the screen. The width and height are required properties. If the width was not set it would expand across the screen and overlap the content. The height it required so the box does not collapse on the contents that are within. The final important property is the ‘bottom’ style, this will force the box to position itself a certain distance from the bottom of the browser. In our case it will be ‘40px’ away.

The next important piece of code will change the h3 properties. When it comes down to it you can actually just add class to the link or throw it in a span but I chose to stick with ‘<h3>’ because I had originally created it with the default h3 font style.

The code for the next chunk of CSS will look as follows:

1
2
3
4
5
6
7
.sidelinks-container h3{
-webkit-transform: rotate(-90deg);
-moz-transform: rotate(-90deg);
padding:0px;
font:32px Verdana, Arial, Helvetica, sans-serif;
line-height:50px;
}

Live demo

By going through the code we will notice that everything is font styling with an added little text rotation that works with IE. Now that we have most of the CSS and the HTML we can throw it all together and see how well it works out.

Below there will be a copy of the html used in this example along with the CSS. By clicking the image you can see a demo version of this page in action. Simply scroll down and click the fixed div on the left.

The CSS

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
.sidelinks-container h3{
-webkit-transform: rotate(-90deg);
-moz-transform: rotate(-90deg);
padding:0px;
font:32px Verdana, Arial, Helvetica, sans-serif;
line-height:50px;
}
.sidelinks-container {
position:fixed;
left:0px;
width:50px;
height:100px;
padding:0px;
bottom:40px;
background: #444;
-moz-border-radius-topright:5px;
-webkit-border-top-right-radius:5px;
-moz-border-radius-bottomright:5px;
-webkit-border-bottom-right-radius:5px;
overflow:hidden;
 
}

The HTML

1
2
3
<body>
<a name="top" id="top"></a>
<div><h3><a href="#top">Top</a></h3></div>

That is all, you are done.

posted by Caleb in CSS, Web Development and have No Comments