9781430237891_CO-01.jpg

Chapter 12

Using WordPress to Jumpstart Development

Up to this point in the book, we’ve covered a lot of the bases for building a website from scratch. We’ve talked about the most fundamental building blocks of the web and how to combine those together to get up and running with a website. We’ve covered using JavaScript to add some interactivity to our pages and to make things dynamic. We’ve even briefly looked at various server-side languages and systems that can help to make a website a truly dynamic system.

It’s all quite daunting and overwhelming, isn’t it? This chapter is going to let you in on a little trick that will give you everything that we’ve talked about, but also have you up and running in a matter of minutes (give yourself quite a few of those minutes the first time around—you’re still learning, after all). In this chapter, we’re going to take a look at an excellent open source blogging and content-management system called WordPress.

This chapter builds on our knowledge up to this point. We don’t have the time or space dive into the details of using PHP and database in this book, yet that is exactly what we’re going to use to leverage WordPress in this chapter. We still won’t be diving into all the gritty details; but by the end of this chapter, you will have a completely functional database-driven website.

Introducing WordPress

WordPress is a really great piece of software that was built using many of the technologies that we’ve already discussed. The current version has valid HTML and CSS, and it is written using PHP and powered by a MySQL database. You might not have keyed in on the best part of those last two sentences, though: WordPress has already been built! You don’t have to spend hours (or days) reinventing the wheel and writing code that will let you build a database-powered website; you just have to install WordPress, adjust some settings, and customize it as much or as little as you see fit.

This chapter will run through downloading and installing WordPress locally, on the desktop machine. This is always a good starting place—you always want to have a place to test your website before uploading it to the web host you’ve selected.

Installing WordPress

Before we get on with installing anything, we’ve got to make sure that we have the required software ­available to run things. It would take an entire chapter to run through how to manually install PHP, MySQL, and a web server, so we’re going to take a shortcut here.

Whether you’re on Windows or a Mac, there’s an extremely easy way to run all three of these ­components:

  • www.mamp.info/en/index.html : MAMP (and its big brother, MAMP Pro) on the Mac gives you a one-click download and setup of Apache, MySQL, and PHP. It even bundles a nice graphical user interface for configuring your servers. MAMP is free, while MAMP Pro has a small licensing fee (for our purposes, all we need is MAMP).
  • www.wampserver.com/en/ : WampServer provides similar functionality for Windows users. It provides one-click downloading and installation, and you’re up and running.

The authors are on a Mac, so we’ll be describing the MAMP setup process. However, WampServer is just as easy to download and install, so all these instructions broadly apply to that software, as well. With MAMP, once you’ve completed your installation, head over to your /Applications folder, open up the MAMP folder, and double-click MAMP.app. You should see something similar to what’s shown in Figure 12-1.

9781430237891_Fig12-01.jpg

Figure 12-1. MAMP is up and running, and it provides a complete testing environment for developing PHP- and MySQL-based websites.

The green lights in the Status window on the left indicate that Apache Server (with PHP) and MySQL Server are up and running. Take a minute to go brag to your friends that you just set up a complete website testing environment. Done? Okay, onward! Chances are that when you started up MAMP, it also started up your web browser and took you to the MAMP Start page. Click the phpInfo link along the top of that page.

Hey, that’s great news! It means that you’ve now got PHP up and running properly. Next, click phpMyAdmin. You might remember that we mentioned phpMyAdmin in Chapter 11 of this book. phpMyAdmin is a piece of software, written entirely in PHP, that allows you to interact with your MySQL database server. You’re going to need this in order to create the database needed by WordPress—in fact, let’s take care of that right now.

Creating a database and user

Follow these steps to create the database and user profile:

  1. Click the Databases tab and, at the bottom of the page under create new database, type in wordpress. When you eventually host this website live on the Internet, you’ll want to use something more descriptive than what you’ve got here, but this will do for now.
  2. Select utf8_bin as the collation (that creates our database with Unicode support) and click the Create button. You’ll see a message saying that the database was created successfully.
  3. You’re ready to create a user who can access that database—click the Privileges tab, and then click the Add a new User link.
  4. It’s time to add a username (the authors used the extremely creative “wordpress” as our username). For the host, select local. The authors let phpMyAdmin generate a password, but feel free to set up your own.
  5. Note the username and password you select; you’re going to need them when installing WordPress.
  6. Under the Database for user section, select the Grant all privileges on database “wordpress” option, and then click the Go button. Your settings should look something like what is shown in Figure 12-2.

9781430237891_Fig12-02.jpg

Figure 12-2. Database settings for out testing environment

Grab a copy of WordPress

You’ve got your preliminary setup done. Now it’s time to grab a copy of WordPress and get that set up and running:

7. Head over to http://wordpress.org/download/ and download the latest version.

8. Download the zip file and uncompress it. MAMP keeps its website files (i.e., its document root) at /Applications/MAMP/htdocs/ by default. Copy the entire contents of your wordpress zip file into that folder.

9. Now, if you load http://localhost:8888 (again, that’s the default for MAMP, but you can specify a different port number), you should see something like what’s displayed in Figure 12-3. This is perfect; it means that you have WordPress installed and that it’s being processed by PHP and served to you by Apache (have you broken a sweat, yet?). It is also telling you that you have some configuring to do, so it’s time to get started on that.

10. Click the Create Configuration File button.

11. The next screen says that WordPress needs some information about your database (see Figure 12-4). No problem: You set that up earlier and noted of all of the relevant information! Click the Let’s go! button.

12. Enter your database name (again, the authors used wordpress, but you might have used something else), the username (again, we just used “wordpress”), and your password. The database host in this case will be localhost (i.e., the same machine you’re working on), and you can just leave the table prefix as-is. In the end, you should end up with something like Figure 12-5. Click the Submit button.

13. If all goes according to plan, you’ll get a friendly message saying that you were able to connect to the database and that this part of the installation is done (if you didn’t get that, go back and double-check your username, password, and database name). Click the Run the install button.

14. You’re on the last step now, which involves setting up a few settings and creating an account on WordPress (see Figure 12-6). Your site title is just that—it’s what will appear on your website when people visit. For the sake of this chapter, let’s carry on with the Summer Smash example used in previous chapters. For a username, select something other than “admin” (for security reasons) and then select a good, strong password. Setting up good security here is important because anybody with administrative access to your WordPress website will be able to change anything he wants on the website—an ounce of prevention is worth a pound of cure! Enter your e-mail address (this is so that you can reset your password, not to sign you up for any mailing lists), and then click the Install WordPress button.

9781430237891_Fig12-03.jpg

Figure 12-3. The newly installed, but not yet configured WordPress

9781430237891_Fig12-04.jpg

Figure 12-4. The WordPress Welcome screen

9781430237891_Fig12-05.jpg

Figure 12-5. The WordPress Database settings—this screen looks daunting, but this is all information you provided in the previous section, so it’s all readily available.

9781430237891_Fig12-06.jpg

Figure 12-6. Setting up a WordPress administrator account and selecting a good password

Again, you’ll receive a friendly confirmation that everything is set up and ready to go. Click the Log in ­button.

A little customization

By this point, you’ve come a long way. You’ve set up a web server, an application server, and a relational database management system (or a database server, if you prefer). You’ve also created a database, installed one of the world’s most popular content-management systems, and performed some basic configuration. That’s not bad for a few minutes of work!

If you’re following along, you’re probably looking at a WordPress login box that is asking for your username and password. Let’s delay logging in for a minute, though, and instead go take a look at the website as it sits right now. Click the link at the bottom of the page that reads, Back to Summer Smash 2012.

Not bad, right? Figure 12-7 shows your default WordPress website without any additional customization or styling. Do you want to be further impressed? Try resizing your browser window down, so that it’s really tall and narrow. Notice how the page format changes, depending on your viewport. What you have here is a responsive website! The default theme in WordPress is, in fact, a responsive theme, and it provides a ton of customization options (as you’ll see in a second).

9781430237891_Fig12-07.jpg

Figure 12-7. The default Wordpress theme is a completely responsive design. It’s not very smashing, but it’s a great place to start.

Feel free to click around and look at a few of the pages. By default, WordPress is set up to be a weblog. What you’re seeing on the home page is a listing of posts made. Posts support comments, so that visitors to the website can respond to what they read. These posts are also searchable, and they are automatically organized by category and chronologically by month (you can see links for all of this in the sidebar of the page).

Finally, WordPress provides an RSS feed of your entries and comments. This means that visitors to the website can subscribe to your news feed using a news reader. Click the Log in link in the sidebar and head back to the Login page. Enter your username and password, so you can check out some of the options available.

The Dashboard

Once logged in, you’re presented with the WordPress Dashboard (see Figure 12-8) and a nice getting started message. This message is a relatively recent addition to WordPress, but it is super helpful because a lot of the links listed here walk you through exactly what you’ll need to do for the site. Let’s start in the left column by clicking the Select your tagline and timezone link.

9781430237891_Fig12-08.jpg

Figure 12-8. The WordPress Dashboard: This is the perfect place to start customizing your website.

You may recall that in earlier chapters we used “This Summer’s Most Smashing Event” as the site’s tagline. Let’s put that in here. Select your timezone and set any date and time formatting options you want. Click the Save Changes button when you’re done and head back to the Dashboard. Next, click the Turn comments on or off link.

This screen is largely about customizing blog-type settings. If you don’t plan on running a blog (or using the blog functionality for a “news” section on your website), then you can safely skip over this section. If you are going to use WordPress’ blogging abilities, then it’s worthwhile to take a couple of seconds to set up how you’d like to deal with comments.

Most of the settings on this page are pretty self-explanatory. You can be as easy going or as strict as you’d like with who can post a comment on your site. The top section, Default article settings, requires a little explanation. This section will allow WordPress to automatically “notify” websites (usually other blogs) that you’ve linked to them. Also, it will automatically create a link on the other weblog leading back to your particular post (it’s a bit like magic). This can be great for generating inbound traffic from other websites; however, it is also a mechanism that can be used to spam your website with links to other websites (just a heads up). The basic rule of online communities is that, if you’re going to have one (i.e., allowing folks to comment on your site is implicitly taking steps to build a community), then you have to participate and moderate it.

Tweak any other settings you like, and then head back to the Dashboard! Your next step is to create a few pages for your website, and then change your home page over to something other than the default.

  1. Click the Pages link in the sidebar, and then click the Add New button. WordPress provides some basic editing tools to put together your page.
  2. You’re not going to bother authoring anything too wonderful at this stage, so just enter “Summer Smash Home” as your page title and leave the body blank for the time being.
  3. If you left comments on in the previous step, you’ll probably want to turn them off for this page (who wants to have a bunch of anonymous comments on her home page?). Click the Screen Options link in the upper-right corner and put a check in the box next to Discussion.
  4. Uncheck the Allow comments and Allow trackbacks & pingbacks options. All of the other settings on the page can be left as-is, so click Publish. You should receive a confirmation that the page is published, as shown in Figure 12-9.
  5. Click the All Pages link in the sidebar.

9781430237891_Fig12-09.jpg

Figure 12-9. A published page (in this case, our future home page)

You’ve now got two pages on your website, the sample page that was created by default by WordPress, and the home page you just created. Hover over the sample page and click the Trash link (you won’t be needing that page). At the top of the screen, you can see that the page was moved to the trash and that you can undo that last action (that’s reassuring, just in case you delete something you’d rather keep).

You are going to use WordPress’ blog functionality; you just don’t want it to be your home page. To that end, you want to create a second page called “Summer Smash News” that you can use for posting updates (see if you can create the page yourself!).

Next up is another Settings page: the Reading Settings page. Click Settings and then Reading in the sidebar. Under the Front Page Displays section, select the second option, and then assign your Home page as the home page and set your News page to handle posts. Remember to click the Save Changes button!

Let’s pause for a second and take a quick look at what you’ve got. Click the words “Summer Smash 2012” in the bar at the top of the page, and you’ll be taken to a preview of what your website looks like. You’ll see a masthead graphic with a navigation menu below it (it currently contains two links: Home and Summer Smash News). Things are coming along, but you’ve got a few more things you need to do.

Extending WordPress

WordPress gets used in a lot of places for a lot of different things. The built-in functionality is pretty great, but when you need to take things a step further, you’ll need to call in a little help. The following features can really help you get the most out of WordPress:

  • Plugins : These allow you to add additional functionality to your website (e.g., a members’ area, a discussion forum, or a registration form).
  • Themes : These are pre-packaged styles for WordPress websites that allow you to completely change the look of your site with just a few clicks.

We’re already using a theme in WordPress; by default, WordPress comes with a great, responsive, standards-compliant theme called Twenty Eleven (this changes from time to time, but at the time of writing, Twenty Eleven is the default). You can buy additional themes, install free ones, develop your own, or just tweak/customize an existing theme (this is what you’ll do for Summer Smash).

Changing the look of WordPress

It’s easy to change the look-and-feel of WordPress. Follow these steps to do so:

  1. Click Summer Smash 2012 in the top bar to get back to the Dashboard. For now, you should focus your attention on the last column in the top section. Click the Choose light or darklink.
  2. Summer Smash, despite being a smashing event, is a little bit darker. You’re looking for an edgier feel with the site, so start by changing your theme over to dark.
  3. The default link color for dark is a great orange color, but that’s a little too Halloween-y for this site. Instead, change it to a nice, deep (but still contrasting) red. Click the orange color, and you’ll get a handy color picker (the authors went with #e4211f, but feel free to pick whatever color you like). Also, leave your default layout with the content on the left, just as you did when developing with the form in previous chapters.
  4. Save those changes and preview the site again (see Figure 12-10). We should have mentioned this earlier, but don’t be dismayed if your screens have different masthead images than the screenshots in this book—Twenty Eleven rotates through a series of images.
  5. Next, it’s time to tweak the background a bit. Head back to the Dashboard and click the next link in the last column. You’re going to use a background image to give the page a little texture. Subtlepatterns.com is a great online resource of really tasteful background textures (that are free to use). We grabbed one called “Vertical cloth” ( http://subtlepatterns.com/patterns/vertical_cloth.png ), but feel free to get a little creative if you like!
  6. Those masthead images just aren’t quite right. Head back to the dashboard and click Select a new header image.
  7. The masthead is a place where you can really reinforce what this site is about. You need an image that just screams smash. Here’s one ( www.sxc.hu/photo/1181196 ) from a website called stock.xchng that is a great source for free stock images. That source image is a little big (WordPress indicates that the image should be 1000 pixels by 288 pixels, but it’s easy enough to do a little resizing and cropping. If you don’t have an image editor on your computer, don’t fret. WordPress has you covered there, as well. Just upload the image, and you’ll be able to crop it right on screen.

9781430237891_Fig12-10.jpg

Figure 12-10. After switching to the dark variant of the default theme, your website is starting to take shape.

Going beyond

OK, the site you have now looks much different than what you started with—but what if you’d like to go further still? For example, assume you want to flex your CSS muscles and make some changes of your own. WordPress is more than happy to let you have at the source files. Click the Appearance link in the sidebar, and then click Editor.

The first file that opens up is exactly what you’re looking for: the style sheet. It’s quite big already; there is a lot of style information in Twenty Eleven (the current theme). The question is, where on earth should you start?

You’re already familiar with what style declarations look like; you just need to know what you’re applying your style to, as well as any corresponding class names/ids. Let’s begin with a simple goal: to give the “content area” a bit of a glow to set it apart from the background of the page. We should mention that this chapter is just a starting point. You could delete everything and start over from scratch, writing your own custom style declarations for the various elements on your pages. However, working with what’s already here provides you with a shortcut and gives you a chance to make use of your skills from the preceding chapters.

One way to approach this would be to scroll through the style sheet looking for a likely suspect (div#content, perhaps?). You can’t be sure that the page author used an id of content though, or that he even used a div. Therefore, you need to use a tool called that is built-in to most browsers these days: the Web Inspector. Firefox, Safari, and Chrome all have this tool built-in. It can also be installed optionally with the developer tools for IE, but don’t use IE—seriously.

Now load up the preview site for Summer Smash 2012. Next, right-click the content area and select the Inspect Element option (that’s what the menu says in Chrome, it might be a little different in your browser of choice). You should see something similar to what is shown in Figure 12-11 (depending on where you clicked).

9781430237891_Fig12-11.jpg

Figure 12-11. The Web Inspector tool in Chrome helps you to quickly identify various elements on a page, so that you can know what to target in your style sheet.

You’ll see a preview of your page up top and the markup for your page in the bottom section. If you scroll your mouse over the various elements in the bottom of the page, they’ll be highlighted in the top of the page. The Web Inspector is a ridiculously useful tool for checking out a page’s structure and identifying what styles are applied to a particular element.

In this case, you’re trying to find the outer-most element that encases your content. The div with an id of page looks like a good suspect. There is a trick you should be aware of when using WordPress, though; different pages may have different structures, so you should check this across a few of your pages. Begin by clicking the News page. The div#page is still there. You should also check one more thing: click “Hello World” (the sample post) just to make sure that post isn’t div#post or something like that.

Everything looks good so far. Use div#page to apply your glow, and then head back to Appearance ➤ Editor and look for #page in the style sheet. You’re going to put a box shadow on this element to give it a bit of a glow, so look for the following (remember to click Update File after you make your changes):

 #page {

  margin: 2em auto;

  max-width: 1000px;

 }

Now incorporate the following additions:

 #page {

  margin: 2em auto;

  max-width: 1000px;

  -moz-box-shadow: 0 0 20px rgba(255,255,255,0.3);

  -webkit-box-shadow: 0 2 20px rgba(255,255,255,0.3);

  box-shadow: 0 0 20px rgba(255,255,255,0.3);

 }

Box shadow is a new CSS3 property, so you need to use some of those vendor-specific prefixes for the time being. Let’s make one more edit and change the header font to the one that was picked out in the earlier chapter: Frijole. First, you have to include the style sheet for Frijole, which goes in the Header of the page. To do this, go to Appearance ➤ Editor, click Header (header.php), and paste in this line:

 <link href="http://fonts.googleapis.com/css?family=Frijole" rel="stylesheet">

You want this line to come before the main style sheet, so paste it in immediately above it. Click Update File when you’re done. Now you need to hop back to the style sheet, so click Stylesheet (style.css) in the right column. Again, you need to figure out what the title and tagline elements are, so head back and inspect them (it might be helpful if you just open a second browser window to save yourself the trouble of jumping back and forth so often). It looks like your main header is an <h1> with an id of site-title, and your tagline is an <h2> with an id of site-description. They’re both enclosed within an hgroup, albeit within a div with the id of branding. You can use the cascade to apply your font to both of these elements with a single declaration. Look for the declaration #branding hgroup in the style sheet to see some existing margin declarations. Now add your font-family declaration:

 #branding hgroup {

  margin: 0 7.6%;

  font-family: "Frijole", Arial, sans-serif;

 }

Next, click “Update File” and head over to take a look at your page. The nice smashing font is back.

Go beyond with plugins

Previously, we spent a few chapters designing a great registration form for our event—wouldn’t it be great if we could use that and make it submit somewhere? WordPress itself doesn’t have baked-in support for form handling (aside from letting visitors submit comments on a page, but we don’t want that for our Registration form!). However, let’s see if we can add something to WordPress that will help us out in that department.

The first order of business is to create a page for the Registration form. Follow these steps to do so:

  1. Go to the Admin page, click Pages, and then choose Add New. Title this page “Registration” and, as before, turn off comments and trackbacks/pingbacks.
  2. Grab the completed markup for the form from Chapter 4 and paste it in. By default, you’ll be looking at the Visual editor, so toggle over to the HTML editor (in the upper-right corner) before pasting. Click Publish.
  3. Next, click Plugins. You can see that there are already two plugins installed in WordPress (neither of which is active, though). The Akismet plugin will protect your blog against spammers. If you’re going to be opening up comments or trackbacks on any page of your website, we highly recommend that you take a few moments to configure the Akismet plugin for your site. Hello Dolly is just a test plugin, and you can delete it.
  4. Neither of these plugins does what you need in terms of forms processing though, so you need to find something to fill that role. Click Add New under Plugins in the left bar. You’re looking for a form processor, so try doing a search for that. This search returns a lot of results, but after a quick scan of the search results, it looks like a plugin called Filled In might just fit the bill. Click the Install Now link below this plugin, and then click the Activate link.
  5. Chances are that you’ll have to do a little configuring before this plugin works, so click Tools and then Filled In in the sidebar. OK, it looks like you will have to create a new form. Call it Registration and click Create. You can also see a pile of other configuration options (feel free to check those out on your own); for now, you’re just going to do the bare minimum to get things working. Click Registration in the list of forms.

You want to accomplish a couple of things with this form:

  • You want the results of this form to be e-mailed to you.
  • You want the registrant to see a nice “Thank You” message after clicking the Register button.

Follow these steps to set up this form:

  1. Under the Post Processors section, select Send as email and click Add. Next, click the link that appears, enter your e-mail address, and click the Save button.
  2. Under the Result Processor section, click the Display a ‘thank-you’ message option and click Add. Next, click the link that appears and enter your thank you message.
  3. Check the box for “Auto Formatting” and “Replace entire page, and then click the Save button.

OK, it’s great that our form will do all of that, but how do we link this up with the form we put together in previous chapters? The answer lies in the Help pages.

If you click the Help link in the upper-right corner and read over the Filled In Documentation, then you can easily link a form to this form processor just by giving the form an id of (in this case) Registration. Follow these steps to finish linking this new form up with the form we created in previous chapters:

  1. Head back to your Registration page (i.e., click Pages and then Registration from the list).
  2. If you did a straight copy of the form code, remove the action=… attribute from the form, and replace it with id="Registration".
  3. Click the Update button and then look at your form. You’ll notice that the formatting is a bit funny because you haven’t copied over any of the style information from Chapters 5 and 6; however, you can do that in a second. For now try filling it in and then clicking the Register now! button.

You’ll notice a couple of things In Figure 12-12, you can see a nice Confirmation page with your thank you message displayed; it looks as though everything worked properly. However, chances are that you didn’t receive an e-mail message with the registration information. That’s OK; on our desktop computers, we don’t have a mail server set up to handle sending those messages. Once the website is uploaded to a real web host, however, that part will work just fine. In the meantime, how can you be sure that something actually happened?

9781430237891_Fig12-12.jpg

Figure 12-12. The Confirmation screen that is displayed once somebody registers for Summer Smash.

Now head back to our Admin page, click “Tools” and then the Filled In link. Next to your Registration form, you should see a 1 under the Succeeded column. Click that 1, and you’ll be able to access the entry.

Wrapping things up

In just a few short pages, you’ve gone from zero to having a fully functional, database-driven website. You’ve taken the form that was created in previous chapters and added processing capabilities (remember to add the style declarations, as well, so that the form is properly formatted!). You’ve customized your WordPress theme and given it the personality that you developed in previous chapters.

Our next steps will be to work out the content of the other pages on our website, to test them out, and then to move our site over to a proper host, so that it’s available 24/7.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset