• Feed RSS

The Latest Updates to jQuery Mobile

Not too long ago, the jQuery team released jQuery Mobile 1.2. This new release has some fantastic changes! In this article, we're going to review some of the new widgets available to developers, take a look at changes made to existing widgets, and glance over a few impactful enhancements that could affect your jQuery Mobile application. Let's get started, shall we?

Firstly, we'll need to download the bits and bytes. Head over to the jQuery Mobile download page and download the option that best meets your needs. Alternately you can just use the boilerplate code provided below.
Additionally, now might be a good time to quickly discuss the Download Builder that the jQuery Mobile team is building. It's still in Alpha, but it allows you to customize your download to include only the parts of jQuery Mobile that you require, and nothing more. You can exclude specific events, transitions, form elements, or widgets that you don't care about. It's meant for the developers who are ultra-concerned with grasping the highest level of performance out of his or her application.

Widgets

The beating heart of any jQuery Mobile application are its widgets. Arguably they're the most visible part of the page, and the part that allows users to interact with the page in such an easy manner. The jQuery Mobile team has spent countless hours testing, building, and refining their widgets to make sure that they're the best they can be. In version 1.2, the team has really knocked it out of the park with a widget that developers have been asking for since the framework was first released: popup modals.

Popups

A popup modal is a small section of the page that overlays other parts of the page. They're most typically used as tooltips, or to display photos, maps, and additional content. This information is usually not important enough to warrant another page, but is still important to need displaying by itself. The way jQuery Mobile 1.2 has implemented popups is perfect. Let's learn how easy it is to add popups to a project.
A quick note, for the sake of brevity: all of the code samples below will use the following boilerplate HTML. The jQuery Mobile CSS and JS files (including jQuery) are hotlinked using the jQuery CDN for your convenience.
<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>jQuery Mobile 1.2</title>
    <link rel="stylesheet"  href="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.css" />
    <script src="http://code.jquery.com/jquery-1.8.0.min.js"></script>
    <script src="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.js"></script>
</head>
<body>
    <div data-role="content">
        <!-- your content goes here -->
    </div>
</body>
</html>
Adding a popup to a page in your application is a two-step process. First you'll need some means of triggering the popup. This is generally a link, or button, or something the user interacts with. To trigger element, we add the following attribute:
data-rel="popup"
Second, you need the content to be displayed. This could range from a single div to a menu, a form, or even photos. The content element gets its own attribute:
data-role="popup"
Finally, for simple link buttons, the href attribute must correspond to the id of the content to be displayed. Let's review the complete implementation.
<a href="#simplepopup" data-rel="popup">Open Popup</a>
<div data-role="popup" id="simplepopup">
    <p>This is a completely basic popup, no options set.<p>
</div>
What you'll see on the page should look something similar to this:
Simple popup

Tooltips

Now that you know how to create a popup, let's look at other possibilities. One common use is a tooltip; help text or expanded text shown when a user clicks on a button. Set up the code the same as before:
<a href="#tooltip" data-rel="popup" data-role="button">Find out more</a>
<div data-role="popup" id="tooltip" data-theme="e">
    <p>You found out more!.</p>
</div>
This time, we're styling the resulting content using the e swatch from jQuery Mobile to give it a more pleasant appearance. This helps us remember that popups are first class jQuery Mobile citizens, and can be treated just like anything else on the page.

Menus

Let's move on to something a bit more complicated: a menu. That's a popular approach to page navigation. Give the user the a menu right at their fingertips. So what does that look like with popups?
<a href="#menu" data-rel="popup" data-role="button">Menu</a>
<div data-role="popup" id="menu" data-theme="a">
    <ul data-role="listview" data-theme="c" data-inset="true">
        <li data-role="divider" data-theme="a">My Menu</li>
        <li>Unlinked</li>
        <li><a href="methods.html">Linked</a></li>
        <li><a href="methods.html">With count</a><span class="ui-li-count">42</span></a></li>
    </ul>
</div>
And the resulting output should resemble this:
Popup menu
You can also use 1.2's collapsible lists in popups. Here's a quick example:
<a href="#nestedmenu" data-rel="popup" data-role="button">Nested Menu</a>
<div data-role="popup" id="nestedmenu" data-theme="none">
    <div data-role="collapsible-set" data-theme="b" data-content-theme="c" data-collapsed-icon="arrow-r" data-expanded-icon="arrow-d" style="margin:0; width:250px;">
        <div data-role="collapsible" data-inset="false">
            <h2>Colors</h2>
            <ul data-role="listview">
                <li><a href="#">Red</a></li>
                <li><a href="#">Blue</a></li>
            </ul>
        </div>
        <div data-role="collapsible" data-inset="false">
            <h2>Shapes</h2>
            <ul data-role="listview">
                <li><a href="#">Circle</a></li>
                <li><a href="#">Square</a></li>
            </ul>
        </div>
    </div>
</div>
And the results:
Popup menu
It's worth noting that data-inset="true" is required on the listview or the corners of your list will show up. Try and you'll see.

Forms

Another popular UX approach is to show a login form hovering over the top of a page. That's now possible with jQuery Mobile popups. Here's a simple username/password form.
<a href="#login" data-rel="popup" data-position-to="window" data-role="button">Login</a>
<div data-role="popup" id="login" data-theme="a">
    <form style="padding:10px 20px;">
        <h3>Please sign in</h3>
        <label for="un" class="ui-hidden-accessible">Username:</label>
        <input type="js" name="user" id="un" placeholder="username" />
        <label for="pw" class="ui-hidden-accessible">Password:</label>
        <input type="password" name="pass" id="pw" placeholder="password" />
        <button type="submit" data-theme="b">Sign in</button>
    </form>
</div>
Which gives you:
Popup menu
By default, popups center themselves over the object which triggered them. There's an optional attribute which you'll see in the next three examples. It's data-position-to="window" and you apply it to the element which triggers the popup. Try adding that to the Login button above to see what happens.

Dialogs

A common need for web applications is the ability to interact with the user. We just reviewed one approach: a login form. But, sometimes, you need to prompt the user with questions. A dialog is a perfect solution for this; and what do you know, popups have you covered! So what does that code look like? Here's a simple example:
<a href="#dialog" data-rel="popup" data-position-to="window" data-role="button" data-transition="pop">Dialog</a>
<div data-role="popup" id="dialog" data-overlay-theme="a" data-theme="c">
    <div data-role="header" data-theme="a">
        <h1>Delete Page?</h1>
    </div>
    <div data-role="content" data-theme="d">
        <h3>Are you sure you want to delete this page?</h3>
        <p>This action cannot be undone.</p>
        <a href="#" data-role="button" data-inline="true" data-rel="back" data-theme="c">No</a>
        <a href="#" data-role="button" data-inline="true" data-rel="back" data-theme="b">Yes, Delete it</a>
    </div>
</div>
Popup menu
In the popup content container, note another new attribute for your use: data-overlay-theme="a". This attribute is what applies the shaded background to the dialog box. It's affected by your theme, so be careful when working with themes created with ThemeRoller.

Photos

I can't count the number of times I've seen jQuery Mobile developers ask for some better way of handling image galleries. While popups aren't the perfect solution for large numbers of images, they're great for a handful of images that need to be viewed larger. Even better, it’s incredibly easy; check it out:
<a href="#photo" data-rel="popup" data-position-to="window" data-role="button" data-transition="fade">Photo</a>
<div data-role="popup" id="photo" data-overlay-theme="a" data-theme="d" data-corners="false">
    <a href="#" data-rel="back" data-role="button" data-theme="a" data-icon="delete" data-iconpos="nojs" class="ui-btn-right">Close</a><img src="http://lorempixel.com/450/300/food/" />
</div>
The above code gives you an elegant photo centered to the window, including a close window button.
Popup menu
How did they do that? Within the context of a popup, the anchor tag has attributes which behave slightly different than when used in other locations on the page. Specifically, the ui-btn-right class positions the image in the corner of the image rather than the side, while the data-icon and data-iconpos attributes allow you to style the button just as you would a regular button.
You can get pretty fancy with popups including, but not limited to, displaying inline video and even interactive maps. Check the jQuery Mobile documentation for popups and iframes.

Collapsible List Views

Another great new feature is the ability to combine collapsible sets with list views. Call 'em “Collapsible List Views” and you've got a brand new widget in jQuery Mobile 1.2. How do they work? I'm glad you asked. Simply create any list you like, then wrap it in a collapsible set. Collapsible List Views also support multiple lists – so get crazy!
<div data-role="collapsible" data-theme="b" data-content-theme="c">
    <h2>Favorite Spice Girl?</h2>
    <ul data-role="listview">
        <li><a href="index.html">Posh</a></li>
        <li><a href="index.html">Scary</a></li>
        <li><a href="index.html">Sporty</a></li>
        <li><a href="index.html">Baby</a></li>
        <li><a href="index.html">Ginger</a></li>
    </ul>
</div>
The code above would result in this Collapsible List View:
Popup menu

Enhancements

In addition to new widget types, jQuery Mobile 1.2 provides a number of useful enhancements to existing functionality. Let's take a look at some of the more valuable ones.

jQuery Support Changes

One of the biggest enhancements made in version 1.2 is the added support for jQuery 1.8. This brings some significant performance increases in the form of a completely rewritten Sizzle.js (the selector engine for jQuery), along with numerous other improvements.
The tradeoff is that the jQuery Mobile team decided that it was time to sunset support for jQuery 1.6. This may be unfortunate for some folks out there who are still using older versions of jQuery, but it’s a fair trade.

List View Autodividers

If you've ever had to manage a list of constantly changing people, places, or things in jQuery Mobile then, you've probably had to write some tricky code to display dynamic listview headers. Bummer that you spent all that time, because the jQuery Mobile team made it as easy as dropping in a single attribute.
data-autodividers="true"
That turns this list:
Popup menu
Into:
Popup menu
Note that this does not manage sorting, grouping, or filtering. For that sort of functionality, consider trying my jQuery Mobile Tinysort plugin.

Read-Only Lists

jQuery Mobile offers "read-only" list views, but it was difficult to tell that they weren't clickable. Version 1.2 removes the list gradient, making the row a flat color. This should provide a better visual cue to your users.

Better Width adjustments on Form Elements

Version 1.2 fixes a semi-annoying issue with form elements, whereby they wouldn't take up the full width of their parent element in some cases.

Additional Devices Added

You might have noticed that new devices are being added almost on a daily basis. The jQuery Mobile team does their best to test against as many of these devices as possible. Newly added to the A grade platform list are the following devices/operating systems/browsers: iOS 6, Android 4.1 (Jellybean), Tizen, Firefox for Android, and Kindle Fire HD.

Full List of Changes

You can find a complete list of the changes made for version 1.2 on the jQuery Mobile blog.
I hope that some of these improvements will help to improve your applications. Remember that the jQuery Mobile team is on your side! If you see something that you think would be valuable, ask for it: create an issue in their Github repository, and suggest it. Better yet, fork their repo and add some code yourself!
read more

PHP Database Access: Are You Doing It Correctly?

We've covered PHP's PDO API a couple of times here on Nettuts+, but, generally, those articles focused more on the theory, and less on the application. This article will fix that!
To put it plainly, if you're still using PHP's old mysql API to connect to your databases, read on!


What?

It's possible that, at this point, the only thought in your mind is, "What the heck is PDO?" Well, it's one of PHP's three available APIs for connecting to a MySQL database. "Three," you say? Yes; many folks don't know it, but there are three different APIs for connecting:
  • mysql
  • mysqli – MySQL Improved
  • pdo – PHP Data Objects
The traditional mysql API certainly gets the job done, and has become so popular largely due to the fact that it makes the process of retrieving some records from a database as easy as possible. For example:
/*
 * Anti-Pattern
 */

# Connect
mysql_connect('localhost', 'username', 'password') or die('Could not connect: ' . mysql_error());

# Choose a database
mysql_select_db('someDatabase') or die('Could not select database');

# Perform database query
$query = "SELECT * from someTable";
$result = mysql_query($query) or die('Query failed: ' . mysql_error());

# Filter through rows and echo desired information
while ($row = mysql_fetch_object($result)) {
    echo $row->name;
}
Yes, the code above is fairly simple, but it does come with its significant share of downsides.
  • Deprecated: Though it hasn't been officially deprecated – due to widespread use – in terms of best practice and education, it might as well be.
  • Escaping: The process of escaping user input is left to the developer – many of which don't understand or know how to sanitize the data.
  • Flexibility: The API isn't flexible; the code above is tailor-made for working with a MySQL database. What if you switch?
PDO, or PHP Data Objects, provides a more powerful API that doesn't care about the driver you use; it's database agnostic. Further, it offers the ability to use prepared statements, virtually eliminating any worry of SQL injection.

How?

When I was first learning about the PDO API, I must admit that it was slightly intimidating. This wasn't because the API was overly complicated (it's not) – it's just that the old myqsl API was so dang easy to use!
Don't worry, though; follow these simple steps, and you'll be up and running in no time.

Connect

So you already know the legacy way of connecting to a MySQL database:
# Connect
mysql_connect('localhost', 'username', 'password') or die('Could not connect: ' . mysql_error());
With PDO, we create a new instance of the class, and specify the driver, database name, username, and password – like so:
$conn = new PDO('mysql:host=localhost;dbname=myDatabase', $username, $password);
Don't let that long string confuse you; it's really very simple: we specify the name of the driver (mysql, in this case), followed by the required details (connection string) for connecting to it.
What's nice about this approach is that, if we instead wish to use a sqlite database, we simply update the DSN, or "Data Source Name," accordingly; we're not dependent upon MySQL in the way that we are when use functions, like mysql_connect.

Errors

But, what if there's an error, and we can't connect to the database? Well, let's wrap everything within a try/catch block:
try {
    $conn = new PDO('mysql:host=localhost;dbname=myDatabase', $username, $password);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) {
    echo 'ERROR: ' . $e->getMessage();
}
That's better! Please note that, by default, the default error mode for PDO is PDO::ERRMODE_SILENT. With this setting left unchanged, you'll need to manually fetch errors, after performing a query.
echo $conn->errorCode();
echo $conn->errorInfo();
Instead, a better choice, during development, is to update this setting to PDO::ERRMODE_EXCEPTION, which will fire exceptions as they occur. This way, any uncaught exceptions will halt the script.
For reference, the available options are:
  • PDO::ERRMODE_SILENT
  • PDO::ERRMODE_WARNING
  • PDO::ERRMODE_EXCEPTION

Fetch

At this point, we've created a connection to the database; let's fetch some information from it. There's two core ways to accomplish this task: query and execute. We'll review both.

Query

/*
 * The Query Method
 * Anti-Pattern
 */

$name = 'Joe'; # user-supplied data

try {
    $conn = new PDO('mysql:host=localhost;dbname=myDatabase', $username, $password);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    $data = $conn->query('SELECT * FROM myTable WHERE name = ' . $conn->quote($name));

    foreach($data as $row) {
        print_r($row);
    }
} catch(PDOException $e) {
    echo 'ERROR: ' . $e->getMessage();
}
Though this works, notice that we're still manually escaping the user's data with the PDO::quote method. Think of this method as, more or less, the PDO equivalent to use mysql_real_escape_string; it will both escape and quote the string that you pass to it. In situations, when you're binding user-supplied data to a SQL query, it's strongly advised that you instead use prepared statements. That said, if your SQL queries are not dependent upon form data, the query method is a helpful choice, and makes the process of looping through the results as easy as a foreach statement.

Prepared Statements

/*
 * The Prepared Statements Method
 * Best Practice
 */

$id = 5;
try {
    $conn = new PDO('mysql:host=localhost;dbname=myDatabase', $username, $password);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);    

    $stmt = $conn->prepare('SELECT * FROM myTable WHERE id = :id');
    $stmt->execute(array('id' => $id));

    while($row = $stmt->fetch()) {
        print_r($row);
    }
} catch(PDOException $e) {
    echo 'ERROR: ' . $e->getMessage();
}
In this example, we're using the prepare method to, literally, prepare the query, before the user's data has been attached. With this technique, SQL injection is virtually impossible, because the data doesn't ever get inserted into the SQL query, itself. Notice that, instead, we use named parameters (:id) to specify placeholders.
Alternatively, you could use ? parameters, however, it makes for a less-readable experience. Stick with named parameters.
Next, we execute the query, while passing an array, which contains the data that should be bound to those placeholders.
$stmt->execute(array('id' => $id));
An alternate, but perfectly acceptable, approach would be to use the bindParam method, like so:
$stmt->bindParam(':id', $id, PDO::PARAM_INT);
$stmt->execute();

Specifying the Ouput

After calling the execute method, there are a variety of different ways to receive the data: an array (the default), an object, etc. In the example above, the default response is used: PDO::FETCH_ASSOC; this can easily be overridden, though, if necessary:
while($row = $stmt->fetch(PDO::FETCH_OBJ)) {
    print_r($row);
}
Now, we've specified that we want to interact with the result set in a more object-oriented fashion. Available choices include, but not limited to:
  • PDO::FETCH_ASSOC: Returns an array.
  • PDO::FETCH_BOTH: Returns an array, indexed by both column-name, and 0-indexed.
  • PDO::FETCH_BOUND: Returns TRUE and assigns the values of the columns in your result set to the PHP variables to which they were bound.
  • PDO::FETCH_CLASS: Returns a new instance of the specified class.
  • PDO::FETCH_OBJ: Returns an anonymous object, with property names that correspond to the columns.
One problem with the code above is that we aren't providing any feedback, if no results are returned. Let's fix that:
$stmt->execute(array('id' => $id));

# Get array containing all of the result rows
$result = $stmt->fetchAll();

# If one or more rows were returned...
if ( count($result) ) {
    foreach($result as $row) {
        print_r($row);
    }
} else {
    echo "No rows returned.";
}
At this point, our full code should look like so:
$id = 5;
  try {
    $conn = new PDO('mysql:host=localhost;dbname=someDatabase', $username, $password);
    $stmt = $conn->prepare('SELECT * FROM myTable WHERE id = :id');
    $stmt->execute(array('id' => $id));

    $result = $stmt->fetchAll();

    if ( count($result) ) {
      foreach($result as $row) {
        print_r($row);
      }
    } else {
      echo "No rows returned.";
    }
  } catch(PDOException $e) {
      echo 'ERROR: ' . $e->getMessage();
  }

Multiple Executions

The PDO extension becomes particularly powerful when executing the same SQL query multiple times, but with different parameters.
try {
  $conn = new PDO('mysql:host=localhost;dbname=someDatabase', $username, $password);
  $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

  # Prepare the query ONCE
  $stmt = $conn->prepare('INSERT INTO someTable VALUES(:name)');
  $stmt->bindParam(':name', $name);

  # First insertion
  $name = 'Keith';
  $stmt->execute();

  # Second insertion
  $name = 'Steven';
  $stmt->execute();
} catch(PDOException $e) {
  echo $e->getMessage();
}
Once the query has been prepared, it can be executed multiple times, with different parameters. The code above will insert two rows into the database: one with a name of “Kevin,” and the other, “Steven.”

CRUD

Now that you have the basic process in place, let’s quickly review the various CRUD tasks. As you’ll find, the required code for each is virtually identical.

Create (Insert)

try {
  $pdo = new PDO('mysql:host=localhost;dbname=someDatabase', $username, $password);
  $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

  $stmt = $pdo->prepare('INSERT INTO someTable VALUES(:name)');
  $stmt->execute(array(
    ':name' => 'Justin Bieber'
  ));

  # Affected Rows?
  echo $stmt->rowCount(); // 1
} catch(PDOException $e) {
  echo 'Error: ' . $e->getMessage();

Update

$id = 5;
$name = "Joe the Plumber";

try {
  $pdo = new PDO('mysql:host=localhost;dbname=someDatabase', $username, $password);
  $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

  $stmt = $pdo->prepare('UPDATE someTable SET name = :name WHERE id = :id');
  $stmt->execute(array(
    ':id'   => $id,
    ':name' => $name
  ));

  echo $stmt->rowCount(); // 1
} catch(PDOException $e) {
  echo 'Error: ' . $e->getMessage();
}

Delete

$id = 5; // From a form or something similar

try {
  $pdo = new PDO('mysql:host=localhost;dbname=someDatabase', $username, $password);
  $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

  $stmt = $pdo->prepare('DELETE FROM someTable WHERE id = :id');
  $stmt->bindParam(':id', $id); // this time, we'll use the bindParam method
  $stmt->execute();

  echo $stmt->rowCount(); // 1
} catch(PDOException $e) {
  echo 'Error: ' . $e->getMessage();
}

Object Mapping

One of the neatest aspects of PDO (mysqli, as well) is that it gives us the ability to map the query results to a class instance, or object. Here’s an example:
class User {
  public $first_name;
  public $last_name;

  public function full_name()
  {
    return $this->first_name . ' ' . $this->last_name;
  }
}

try {
  $pdo = new PDO('mysql:host=localhost;dbname=someDatabase', $username, $password);
  $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

  $result = $pdo->query('SELECT * FROM someTable');

  # Map results to object
  $result->setFetchMode(PDO::FETCH_CLASS, 'User');

  while($user = $result->fetch()) {
    # Call our custom full_name method
    echo $user->full_name();
  }
} catch(PDOException $e) {
  echo 'Error: ' . $e->getMessage();
}

Closing Thoughts

Bottom line: if you’re still using that old mysql API for connecting to your databases, stop. Though it hasn’t yet been deprecated, in terms of education and documentation, it might as well be. Your code will be significantly more secure and streamlined if you adopt the PDO extension.
read more

How to Process Credit Cards with PayPal Payments Pro Using PHP

PayPal is one of the most popular payment processing platforms available today for many reasons. Its ease of use and its connection to the eBay platform are just the tip of the iceberg. While one of its most popular features is the ability to simply sign in to your PayPal account to submit payments, merchants using PayPal can also accept credit cards directly just like a traditional merchant account solution would provide.

PayPal calls this solution Payments Pro, and I’m going to show you exactly how you can process credit cards directly with PayPal’s API using their Payments Pro web service API’s.

Step 1: Setup the Directory Structure

The first thing I like to do with any project is to create a basic structure organized for the project.  In this case, our structure is very simple as our project will consist of only 2 files:
Directory Structure
 As you might have guessed, we’ll be storing our configuration information in config.php, and we’ll actually handle the processing code in process-credit-card.php.

Step 2: Setup the Config File

Our /includes/config.php file will house our values for the PayPal API information we need including the end-point URL, API version, and our API username, password, and signature that we’ll be using. 
// Set sandbox (test mode) to true/false.
$sandbox = TRUE;

// Set PayPal API version and credentials.
$api_version = '85.0';
$api_endpoint = $sandbox ? 'https://api-3t.sandbox.paypal.com/nvp' : 'https://api-3t.paypal.com/nvp';
$api_username = $sandbox ? 'SANDBOX_USERNAME_GOES_HERE' : 'LIVE_USERNAME_GOES_HERE';
$api_password = $sandbox ? 'SANDBOX_PASSWORD_GOES_HERE' : 'LIVE_PASSWORD_GOES_HERE';
$api_signature = $sandbox ? 'SANDBOX_SIGNATURE_GOES_HERE' : 'LIVE_SIGNATURE_GOES_HERE';
Reviewing the config.php code, you can see that first we set a variable for $sandbox.  For now, we’ll leave this to TRUE because we want to interact with PayPal’s sandbox (test) servers for development purposes.  You’ll need to remember to change this to FALSE when you’re ready to move your project to a live server.
Then, based on the value of $sandbox we’re setting values to other variables for our API information.  You’ll just want to fill in those placeholders with your own details accordingly.  Now we’re ready to build our credit card processing script.

Step 3: Create an API Request

Now we can begin to build our process-credit-card.php page.  The first thing we need to do here is include our config file.
// Include config file
require_once('includes/config.php');
Next, we need to build a name-value-pair string that includes all of the data we need to send PayPal in order to process this payment.  A name-value-pair string looks just like something you might see when passing data via URL parameters.  We just need to make sure our parameter names are in all caps.
PARAM1=value1&amp;PARAM2=value2&amp;PARAM3=value3…etc.
So, you might be thinking to yourself “How do I know what to use for my variable names in my string?”  The good news is PayPal provides very good documentation on this.  We can see all of the possible variables that we can pass PayPal including customer details, order item details, and credit card information.  Some of this information is required in order to process a payment, but many of the variables available are optional.  For demonstration purposes, we’ll keep this pretty simple and just pass the required information.
We’ll store all of our request parameters in an array so that we can loop through this array to easily generate our NVP string.  All requests require the following parameters by default:
  • METHOD – The name of the API call you’re making.
  • USER – The API username
  • PWD – The API password
  • SIGNATURE – The API signature
  • VERSION – The API version
Then you can refer to the PayPal documentation for any API request you’d like to make to see what other parameters should be included.  For the sake of this demonstration, our array will be built as follows.
// Store request params in an array
$request_params = array
     (
     'METHOD' => 'DoDirectPayment',
     'USER' => $api_username,
     'PWD' => $api_password,
     'SIGNATURE' => $api_signature,
     'VERSION' => $api_version,
     'PAYMENTACTION' => 'Sale',
     'IPADDRESS' => $_SERVER['REMOTE_ADDR'],
     'CREDITCARDTYPE' => 'MasterCard',
     'ACCT' => '5522340006063638',
     'EXPDATE' => '022013',
     'CVV2' => '456',
     'FIRSTNAME' => 'Tester',
     'LASTNAME' => 'Testerson',
     'STREET' => '707 W. Bay Drive',
     'CITY' => 'Largo',
     'STATE' => 'FL',
     'COUNTRYCODE' => 'US',
     'ZIP' => '33770',
     'AMT' => '100.00',
     'CURRENCYCODE' => 'USD',
     'DESC' => 'Testing Payments Pro'
     );
You’ll notice we’re using our config variables from config.php, and then I’m simply loading static data for the other values.  In a standard project, though, you’ll most likely be populating these values with form data, session data, or some other form of dynamic data.
Now we can simply loop through this array to generate our NVP string.
// Loop through $request_params array to generate the NVP string.
$nvp_string = '';
foreach($request_params as $var=>$val)
{
 $nvp_string .= '&'.$var.'='.urlencode($val);
}
The value of $nvp_string is now:
METHOD=DoDirectPayment&amp;amp;USER=sandbo*****e.com&amp;amp;PWD=12***74&amp;amp;SIGNATURE=AiKZ******6W18v&amp;amp;VERSION=85.0&amp;amp;PAYMENTACTION=Sale&amp;amp;IPADDRESS=72.135.111.9&amp;amp;CREDITCARDTYPE=MasterCard&amp;amp;ACCT=5522340006063638&amp;amp;EXPDATE=022013&amp;amp;CVV2=456&amp;amp;FIRSTNAME=Tester&amp;amp;LASTNAME=Testerson&amp;amp;STREET=707+W.+Bay+Drive&amp;amp;CITY=Largo&amp;amp;STATE=FL&amp;amp;COUNTRYCODE=US&amp;amp;ZIP=33770&amp;amp;AMT=100.00&amp;amp;CURRENCYCODE=USD&amp;amp;DESC=Testing+Payments+Pro
This string is what we’ll send to PayPal for our request.

Step 4: Send the HTTP Request to PayPal

Now that our NVP string is ready to go we need to send this to the PayPal server to be processed accordingly.  To do this, we’ll use PHP’s CURL methods.
// Send NVP string to PayPal and store response
$curl = curl_init();
  curl_setopt($curl, CURLOPT_VERBOSE, 1);
  curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
  curl_setopt($curl, CURLOPT_TIMEOUT, 30);
  curl_setopt($curl, CURLOPT_URL, $api_endpoint);
  curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($curl, CURLOPT_POSTFIELDS, $nvp_string);

$result = curl_exec($curl);
curl_close($curl);
Here you can see that we’ve setup CURL with a few simple options and we’re using our $api_endpoint and $nvp_string variables accordingly.
This data will be sent over to PayPal and we will receive the API response back in our $result variable so that we can see the result and send the user to a successful or failure page based on whether or not the call succeeded or not.

Step 5: Parse the API Response

The value that we get back in $result from the previous step will be an NVP string just like the one we generated and sent to PayPal.  When we run our current script we get a successful response back that looks like this:
TIMESTAMP=2012%2d04%2d16T07%3a59%3a36Z&amp;CORRELATIONID=9eb40cd84a7d3&amp;ACK=Success&amp;VERSION=85%2e0&amp;BUILD=2764190&amp;AMT=100%2e00&amp;CURRENCYCODE=USD&amp;AVSCODE=X&amp;CVV2MATCH=M&amp;TRANSACTIONID=160896645A8111040
One very simple way to parse this result is to use PHP’s parse_str() function.  This will load all of the response data into PHP variables matching the names and values returned in the response.  For example, if we do the following:
// Parse the API response
  $nvp_response_array = parse_str($result);
  
We would end up with access to the following PHP variables:
  • $TIMESTAMP
  • $CORRELATIONID
  • $ACK
  • $VERSION
  • $BUILD
  • $AMT
  • $CURRENCYCODE
  • $AVSCODE
  • $CVV2MATCH
  • $TRANSACTIONID
We can then proceed to use these variables to present information back to our customer, populate values in email receipts we’d like to generate, update database information, or anything else we need to do once an order is completed.
The $ACK value is what will tell us whether or not the API call was successful or not.  Values for $ACK can be:
  • Success
  • SuccessWithWarning
  • Failure
  • FailureWithWarning
  •   You can simply redirect your user where they need to go and show them information based on this value. A failing API call will result in additional parameters that provide information about why the transaction failed.  If I run this test again with an invalid credit card number, for example, I get the following response back from PayPal:
    TIMESTAMP=2012%2d04%2d16T08%3a08%3a52Z&amp;CORRELATIONID=590d41dbb31e0&amp;ACK=Failure&amp;VERSION=85%2e0&amp;BUILD=2764190&amp;L_ERRORCODE0=10527&amp;L_SHORTMESSAGE0=Invalid%20Data&amp;L_LONGMESSAGE0=This%20transaction%20cannot%20be%20processed%2e%20Please%20enter%20a%20valid%20credit%20card%20number%20and%20type%2e&amp;L_SEVERITYCODE0=Error&amp;AMT=100%2e00&amp;CURRENCYCODE=USD
    Now, when we use parse_str() we end up with the following PHP variables available to us:
    • $TIMESTAMP
    • $CORRELATIONID
    • $ACK
    • $VERSION
    • $BUILD
    • $L_ERRORCODE0
    • $L_SHORTMESSAGE0
    • $L_LONGMESSAGE0
    • $L_SEVERITYCODE0
    • $AMT
    • $CURRENCYCODE
    In this case, $ACK shows a Failure so we know the call did not succeed and we can check the error parameters for more details about what went wrong.

    Additional Data Parsing Option

    While the previous method of parsing the response works just fine, I personally prefer to work with data arrays.  As such, I use the following function to convert the PayPal response into an array.
    // Function to convert NTP string to an array
    function NVPToArray($NVPString)
    {
     $proArray = array();
     while(strlen($NVPString))
     {
      // name
      $keypos= strpos($NVPString,'=');
      $keyval = substr($NVPString,0,$keypos);
      // value
      $valuepos = strpos($NVPString,'&') ? strpos($NVPString,'&'): strlen($NVPString);
      $valval = substr($NVPString,$keypos+1,$valuepos-$keypos-1);
      // decoding the respose
      $proArray[$keyval] = urldecode($valval);
      $NVPString = substr($NVPString,$valuepos+1,strlen($NVPString));
     }
     return $proArray;
    }
    
    This allows me to see all of the response data available by simply looking at the contents of the array: If I run my script again now I get the following result on screen:
    Array
    (
        [TIMESTAMP] => 2012-04-16T08:15:41Z
        [CORRELATIONID] => 9a652cbabfdd9
        [ACK] => Success
        [VERSION] => 85.0
        [BUILD] => 2764190
        [AMT] => 100.00
        [CURRENCYCODE] => USD
        [AVSCODE] => X
        [CVV2MATCH] => M
        [TRANSACTIONID] => 6VR832690S591564M
    )
    
    And If I were to cause an error again I get the following:
    Array
    (
        [TIMESTAMP] => 2012-04-16T08:18:46Z
        [CORRELATIONID] => 2db182b912a9
        [ACK] => Failure
        [VERSION] => 85.0
        [BUILD] => 2764190
        [L_ERRORCODE0] => 10527
        [L_SHORTMESSAGE0] => Invalid Data
        [L_LONGMESSAGE0] => This transaction cannot be processed. Please enter a valid credit card number and type.
        [L_SEVERITYCODE0] => Error
        [AMT] => 100.00
        [CURRENCYCODE] => USD
    )
    
    You can see this is a nice, easy to navigate result array that contains everything we might need to move the user through our application and update data sources as necessary.

    Conclusion

    As you can see, processing credit cards using PayPal Payments Pro is actually a very simple procedure.  It just involves a few standard steps for working with API web services, and a basic knowledge of working with array data can help as well. Good luck, and happy coding!
read more

Responsive Web Design: Layouts and Media Queries

With the growing number of Smartphone produced in the last three years and the diversity of screen sizes it’s practically impossible to ignore users that browse on a mobile device. Whether they use an Android phone, Windows Mobile phone, a BlackBerry device or an iPhone, whether they are on a tablet, on a Smartphone or on a big screen, each user deserves the best experience possible. As designers, it is our goal to provide those users a nice experience browsing the websites we created, whatever the device used to browse is.
Today most of the clients want their website to be mobile compatible, so this is particularly challenging. Creating a version for each device is impossible, due to the number and diversity of those devices, but also simply because we don’t know what will be created tomorrow. That’s where the concept of “Responsive Webdesign” comes to the rescue.
Responsive Web Design: Layouts and Media Queries



A responsive website is a website that will respond and adapt to the user’s behavior and screen size. The idea is to resize and reorder the design, adapt images, typography, columns, etc., based on screen - browser size, resolution and orientation instead of providing each device a specific website.

A Look at 3 Different Types of Layout

Basic Fluid Layout

Fluid layout is based on a system of relative units instead of absolute pixels. This kind of layout has been around for a while now, and most of the designers use fluid grids created in percentage to achieve such layouts.
The idea is pretty simple: instead of giving the layout rigid width in pixels, we will give it relative ones in percentage. The fluid layout based websites usually take the whole browser width, hence the 100% in this example.
You can see a demo of a fluid design here.
Fluid
Data JavaScript credit: Andreas Bovens
The style.css gives us common styles for the page (color, typo), but let’s take a look at our fluid.CSS file :
#header {
    width: 100%;
    margin: 0;
    padding: 0;
}
#content {
    float: left;
    width: 60%;
    margin: 0 0 20px 0;
    padding: 0;
}
#content .inner {
    margin-right: 2%;
}
.sidebar{
    float: left;
    margin: 0 0 20px 1%;
    padding: 0;
}
#bar1{
    width:20%;
}
#bar2{
    width:18%;
}
#footer {
    clear: both;
    width: 100%;
    margin: 0;
    padding: 0;
}
Our header and footer have a 100% width, so they’ll take the whole screen available. The main content has a 60%, and our sidebars 20% and 18% so that we will be able to create a design that will fit the whole space available.
This design adapts perfectly on big screens, but we can see that the sidebar content tend to become hard to read when we resize too small.

Adaptive Layout

The adaptive layout is based on a pretty simple idea: instead of using percentage we will give our layout fixe sizes, but we will adapt those sizes depending of the width of the browser/viewport, thus creating a layout with different “break points”.
For each of those break point, we will use media queries (will come back to explain them in detail in the second part of the article) to adapt the layout of our website so that content is not too hard to read.
You can see and example of adaptive layout here.
Adaptive
The HTML and style.css did not change; all we changed was the structure of the page. Let’s take a closer look at our CSS file.
The "normal" website uses this CSS:
body{
    width:1280px;
    margin:0 auto;
}
#header {
    width: 100%;
    margin: 0;
    padding: 0;
}
#content {
    float: left;
    width: 800px;
    margin: 0 0 20px 0;
    padding: 0;
}
#content .bloc{
    margin-right: 10px;
}
.sidebar{
    float: left;
    margin: 0 0 20px 20px;
    padding: 0;
    width:220px;
}
#footer {
    clear: both;
    width: 100%;
    margin: 0;
    padding: 0;
}
I gave the header and footer a 100% width, but the content has a fixed width. Now the good part, the break points with media queries:
/* Media queries */
@media screen and (max-width: 1200px) {
    body{
    width:1000px;
    margin:0 auto;
    }
    #content {
    width: 700px;
    }
    .sidebar{
    width:280px;
    }
}

@media screen and (max-width: 980px) {
body{
    width:850px;
    margin:0 auto;
    }
    #content {
    width: 550px;
    }
    .sidebar{
    width:280px;
    }
}

@media screen and (max-width: 750px) {
    body{
    width:600px;
    margin:0 auto;
    }
    #content {
    width: 400px;
    }
    .sidebar{
    width:190px;
    margin: 0 0 20px 10px;
    }
}

@media screen and (max-width: 540px) {
    body{
    width:450px;
    margin:0 auto;
    }
    #content {
    width: 450px;
    }
    #content .bloc{
    margin:0px;
    }
    .sidebar{
    width:450px;
    margin: 0 0 10px 0;
    }
}

@media screen and (max-width: 380px) {
    body{
    width:360px;
    margin:0 auto;
    }
    #content {
    width: 360px;
    }
    #content .bloc {
    margin:0px;
    }
    .sidebar{
    width:360px;
    margin: 0 0 10px 0;
    }
}
For each break point given by a media query, I changed the size of the body, the content, and the sidebar. Under 540px, the text in the sidebar was too hard to read, so I gave the sidebar the same size as the content, what has the effect of putting the sidebars under the content.
This nice thing about adaptive layout is the possibility to modify and adapt not only the size of the blocs, but the layout and there place on the page.
The big difficulty is then to choose those break points. A first technique could be to base the break points on most "common" device width. Chris Coyier from CSStricks put a nice list of media queries together. Another way to choose the break points it to actually test the design at different screen sizes and see when it gets ugly or when user can’t really read the text easily, and put break point at those size.
Live example of adaptive layout :
Foodsense

Adaptive Example

Responsive Layout

We could define the responsive layout, as a mix between the fluid and adaptive layouts. It will use the relative units of the fluid layout and the break points of the adaptive one.
Here you can see the demo of our previous example, in responsive layout.
Responsive Example
You can see here how fluid the design is: using percentage enables us to create very smooth transition between the different break points of our design.
Here is our stylesheet for the "normal" version:
#page{
    max-width:1280px;
}
#header {
    width: 100%;
    margin: 0;
    padding: 0;
}
#content {
    float: left;
    width: 60%;
    margin: 0 0 20px 0;
    padding: 0;
}
#content .bloc {
    margin-right: 2%;
}
.sidebar{
    float: left;
    margin: 0 0 20px 1%;
    padding: 0;
}
#bar1{
    width:20%;
}
#bar2{
    width:18%;
}
#footer {
    clear: both;
    width: 100%;
    margin: 0;
    padding: 0;
}
What’s important here is the use of max-width (instead of width for an adaptive layout). It’s this property that enables us to create this smooth transition. Using a max-width, we won’t have to use as many break points as for an adaptive layout, and all the other sizes will be given in a relative unit (percentage for our example).
And the CSS for the media queries:
/* The media queries*/
@media screen and (max-width: 1000px) {
    #bar1,
    #bar2{
    width:39%;
    }
    .sidebar{
    float: left;
    margin: 0 0 20px 1%;
    padding: 0;
    }
}

@media screen and (max-width: 540px) {
    #bar1,
    #bar2{
    clear:both;
    width:100%;
    }
    .sidebar{
    float: left;
    margin: 0 0 20px 1%;
    padding: 0;
    }
    #content {
    clear:both;
    width:100%;
    }
    #content .bloc {
    margin:0;
    }
}
All the other size will be once again given in percentage, relative to the max-width of our body.
Note that for screen size under 540px, we once again gave the sidebars and the content a 100% width, and place the sidebars under the content using some clear: both.
The advantage of the responsive layout is that you won’t have to use too many break points. Since the size are given in percentage, they will adapt automatically, so the major role of the break points will be to be place where design breaks, to re-order our layout (putting sidebars under content in our example) and give the user a more pleasant reading.
Fore Fathers Group

Responsive

Media Queries: Create and Define Break Points

Media queries where introduced in the CSS3 specifications. Put in a simple way, media queries enables the web designer to create conditionals stylesheets based on width, height, but also orientation, color, etc. There’s a huge list of media queries available on the official w3c website but we will only use some of them in our case. Here is a list of the most commonly used media queries and what they do :

Media Query Utilisation

Media Query Use
min-width: … px Used when the viewport’s width is bigger or equal to width
max-width: … px Used when the viewport’s width is smaller or equal to width
min-device-width: … px Used when the device’s width is bigger or equal to width
max-device-width: … px Used when the device’s width is smaller or equal to width
orientation : portrait // orientation: landscape Target orientation
-webkit-min-device-pixel-ratio : 1.5 Used to target high density device on android and ios
As for print style sheets, media queries can be used as external or internal styles sheets. An external style sheet is easier to organize, it is not downloaded by browsers which don’t support it, but it uses extra http request. An internal style sheet on the other hand does not require extra http request, but the whole stylesheet is downloaded for browsers even if they do not support media queries, and it can be harder to organize. Both have then pro and cons, you’ll have.
You already saw the internal syntax in the example above:
body {
    background: gray;
}
@media all and (max-width:500px) {
body {
        background: blue;
    }
}
And here is the external syntaxes:
<link rel="stylesheet" type="text/CSS"  media="screen and (max-device-width: 480px) " href="mobile.CSS" />

Some "tricks" Worth Knowing About Media Queries

Cascade Matters

Yeah that’s right, as for any peace of CSS code, cascade matters.
Consider the following example:
#container{
background:rgba(111, 78, 152, 0.9); /*violet */
color:rgb(1, 1, 1);

@media all and (min-width:500px) {
    #container{
    background: rgba(255, 0, 0, 0.9); /* red */
    color: white;
    }
}
@media all and (min-width:700px) {
   #container{
    background: rgba(0, 0, 255,0.9); /*blue */
    font-family: serif;
    }
}
See the example on jsfiddle.
If the width of our browser is bigger than 500px, the color of the text gets white, and the background red. If we enlarge the screen up to more than 700px, the background gets blue, but the color of the text stays white because it inherited the color of the min-width:500px media query applied before (700 being, well, bigger than 500).

Creating Stacked Media Queries

Consider the following example :
#container{
    background:rgba(111, 78, 152, 0.9); /*violet */
    padding:10px 5px;
    color:rgb(1, 1, 1);
}

@media all and (min-width:500px) and (max-width:699px) {
   #container{
   background: rgba(255, 0, 0, 0.9); /* rouge */
   font-family: serif;
   }
}
@media all and (min-width:700px) {
   #container{
   background: rgba(0, 0, 255,0.9); /*bleu */
   color: white;
   font-family: serif;
   }
}
See the example on jsfiddle.
The first media query will only be applied for screen between 500px and 699px, and the second for screen bigger than 700px. In the case of stacked media queries, since property is only applied for a certain width, they are not herited. In our example, if we also want to apply a serif font the layout bigger than 700px, we will have to repeat this property.
You’ll need a viewport meta tag to make the media queries work. The viewport meta tag enables you to take control of the viewport of the device. Basically, if no viewport is set, mobile device will try to fit the whole page on the screen, resulting in very small websites.
The viewport meta tag looks like this:
<meta name="viewport"  content="initial-scale=1, width=device-width">
We basically tell the device, that we will be using the device width as the width of our page, and that there will be no zooming on the page when we first launch it.

It’s Not Only About the Mobile!

In my examples, I showed some media queries used for mobile optimization, tablets and smaller screens. But I wanted as a conclusion, to emphasize the fact that media queries are not only about mobile optimization. We tend to see more and more mobile device, but also more and bigger screens.
We know have an xbox that can connect to internet, some of the box our internet providers provide us are equipped with a browser, and even some television are able to connect to internet. Maybe tomorrow you will get a web browser on your fridge, who knows. If we use responsive webdesign to optimize for smaller screens, we can also use them to optimize for bigger ones.
Let's remember: responsive webdesign is about adapting layout to the user's browser size, orientation, whatever that size might be!

Some Useful Resources:

Conclusion

As you can see, responsive webdesign is not that hard to use and enables web designers to create nice layouts that will adapt to many devices and screen sizes. Your now it’s your turn: did you ever used responsive design? In what kind of projects? Do you have some advice and special tips? How do you define your break points? Let us know in the comments.
read more

15 Stunning jQuery Lightbox Plug-ins for Your Upcoming Designs

If you see a website built on jQuery using images, they must have played with a lightbox in that context. That’s the power of the lightbox, it can transform any simple image library into an attractive and effective gallery. It’s an important and popular contribution from the jQuery side to the design community.
Thanks to the awesome jQuery community who make these stunning lightbox plug-ins, giving huge scope for designers to showcase images on websites. There is a huge collection of plug-ins each giving a different look and style to images.
We collected 15 stunning jQuery lightbox plug-ins for your reference. Hope you’ll find it worth having a look.
jQuery Lightbox Plug-ins


Stunning jQuery Lightbox Plug-ins

Lightview jQuery Plug-in

Lightview was built to change the way you overlay content on a website.
Lightview jQuery Plug-in
TopUp

TopUp is an easy to use JavaScript library for unobtrusively displaying images and Web pages in a Web 2.0 approach of pop-up. The library is jQuery and jQuery UI driven in order to maintain cross-browser compatibility and compactness.
TopUp
Highslide Lightbox Plug-in

Highslide JS is an image, media and gallery viewer written in JavaScript.
Highslide Lightbox Plug-in
Color Box

A lightweight customizable lightbox plug-in for jQuery 1.3+
Color Box
Lightbox 2

Lightbox 2 is a simple, unobtrusive script used to overlay images on the current page. It's a snap to set up and works on all modern browsers.
Lightbox 2
prettyPhoto

prettyPhoto is a jQuery lightbox clone. Not only does it support images, it also supports videos, flash, YouTube, frames and Ajax. It’s a full blown media lightbox.
prettyPhoto
Slimbox 2

Slimbox 2 is a 4 KB visual clone of the popular Light box 2 script by Lokesh Dhaka, written using the jQuery JavaScript library.
Slimbox 2
Shadowbox

Shadowbox is a web-based media viewer application that supports all of the web's most popular media publishing formats. Shadowbox is written entirely in JavaScript and CSS and is highly customizable.
Shadowbox
Pirobox Extended V.1.0.

One of the most important things with this plug-in is the ability to open any kind of file, from inline content to .swf files, from simple images to .pdf files.
Other things are: automatic image resizing and drag and drop.
Pirobox Extended V.1.0
GreyBox

GreyBox can be used to display websites, images and other content in a beautiful way.
GreyBox
jQuery Super Box

jQuery Super box! Is a script which allows you to display windows with the lightbox effect.
This script is a plug-in for jQuery (1.3.x).
jQuery Super Box
Fancy Box

Fancy Box is a tool for displaying images, HTML content and multimedia in a Mac-style "light box" that floats overtop of web page.
Fancy Box
Pirobox Extended V.1.1.

Pirobox Extended V.1.1 advanced version, Zoom In option with dragable image viewer for large dimension images.
Pirobox Extended V.1.1
jQuery Lightbox Plug-in

JQuery lightbox plug-in is simple, elegant, and unobtrusive, no need for extra markup, and is used to overlay images on the current page through the power and flexibility of jQuery´s selector.
jQuery Lightbox Plug-in
Ceebox

An overlay pop-up script for easily embedding flash video, displaying images, or showing HTML (either external sites via iframe or content on your own site via AJAX).
Ceebox

Conclusion

Do you use any of these lightboxes in your website design work? Do you have a favorite lightbox plug-in? If you do and we haven't listed it here, please share the link with us in the comments below. Your comments and opinions are always very welcome.

Written by: Carol Francis for Onextrapixel - Web Design & Development Online Magazine | One comment
read more

WordPress Multisite Beyond Basics: Essentials and Domain Mapping

"Today we will be discussing a few very important aspects of WordPress Multisite. If you are new to WordPress Multisite please go through the WordPress installation tutorial first to get an overall idea of the Multisite feature in WordPress. In this tutorial I shall be discussing a few key things essential for WordPress Multisite installation, along with some common troubleshooting tips. Finally I shall discuss WordPress Multisite Domain Mapping in detail.

Key Things to Know About WordPress Multisite Installation

Installing WordPress Multisite Using Plugin

You can install WordPress Multisite using two methods. One is using the plugin Enable Multi-Site and another is manual installation. It’s recommended to use manual installation since the installation changes will then be transparent and can be easily altered later.

Changing From Sub-Directory to Sub-Domain

In case you want to change your installation type from sub-directory to sub-domain, please use the following steps:
  • You need to delete all the sites which were created under your main site
  • Enable the sub-domain option from the wp-config.php file by modifying the following code:
    define( 'SUBDOMAIN_INSTALL', true );
  • Finally update your Permalinks
Since you will need to delete all your sites it’s recommended to make the decision carefully while choosing between sub-domain and sub-directory during installation.

Keeping a Default Theme for All Your New Sites

If you want to keep a default theme for all your newly created sites then please go to your wp-config.php file and add the following line of code below the specified line:
// Below this line
 define('WPLANG', '');
 // Add this line
 define('WP_DEFAULT_THEME', 'classic');
Replace ‘classic’ with the folder name of the theme you like.

Add Yourself to All Your Sites

The different sites created will only be visible under My Sites if you have been added as a user for that site. If you are a Network Admin then please add yourself as a regular user for all your created sites so that you can manage them straight from your dashboard.

Let Users Define Custom CSS

Generally the users are not able to edit the themes for their sites but once you (as a Network Administrator) install and activate the Custom User CSS plugin then the site owners can design their themes by defining custom CSS.

Common WordPress Multisite Troubleshooting

Created Sub-Domains Not Working

Your hosting platform should support the Wildcard DNS feature. Please check with your hosting provider prior to installing WordPress Multisite.
To create a Wildcard DNS entry please login to your Control Panel provided by your host and look for the Domain section. Under Domain click on the Subdomain option. This is the place to enable sub-domains for your website.
Once you click on the Subdomain option in the control panel, under Create a Subdomain enter an asterisk ‘*’ in the Subdomain field and then click on Create. The Document Root should point to the root directory of your WordPress installation.

Created Sub-Directories Not Working

The Apache mod_rewrite module should be supported by your hosting provider. This is required in the creation of multiple sites. If you are using WAMP you can enable it by going to Apache > Apache modules > mod_rewrite.

Network Cannot Be Enabled Error

This error occurs if the Site URL is not the same as the WordPress URL, so under WordPress’ Admin Dashboard Settings > General please ensure that they are the same prior to installing Multisite.

Wildcard Sub-Domain Incorrect Error

In order to solve this problem please go to your hosting provider’s control panel where you have defined the wildcard DNS and then update the Document Root of your sub-domain to point to the correct location.

My Uploaded Media Not Working

All the uploaded media including images are located under your created blogs.dir folder. Please check your .htaccess file if the following line of code is in the same format or not. Also ensure that mod_rewrite is enabled for your server.
# uploaded files
 RewriteRule ^([_0-9a-zA-Z-]+/)?files/(.+) wp-includes/ms-files.php?file=$2 [L]
If you find any other issue with WordPress Multisite please feel free to refer to the Official Multisite Support Forum, it contains hundreds of solutions for your common Multisite problems.

Domain Mapping

One of the coolest parts of WordPress Multisite installation is the mapping of domains to turn your network sites into unique domains that carry their own identities. Using Domain Mapping lets you define a custom domain for your blog/site instead of the default address you get when you sign up or create a new site. For example, using a sub-domain install, if you create a new site you will have the URL newsite.parentsite.com. But using Domain Mapping you can turn it into www.newsite.com. Thus Domain Mapping can be used to point external domains to your network sites.
Domain Mapping hides the fact that the site is a part of a Multisite network.
Let’s discuss Domain Mapping in detail.

Step 1 A Little Bit of Your Host cPanel

Before going for the Domain Mapping let’s do a bit of backend work from our cPanel. Please login to your host’s cPanel. Generally the URL of your cPanel will look something like http://www.hostname.com/cpanel or http://www.hostname.com:2082 . Once you log in, you will find two options under the Domains section named Addon Domains and Parked Domains.
Here we will be using the Parked Domains option since we have our WordPress installation in the root directory. The Addon Domain option can be used if you are utilizing WordPress outside the root directory. Under Parked Domains enter the name of the new domain you want to park on your primary domain and then click on Add Domain. The parked domain should automatically point to the root directory of your installation which is generally public_html.
The new domain name should be registered prior to parking.
If you are using the Addon Domains option then you are probably not using your Primary Domain for mapping purposes. After clicking the Addon Domains option fill in all the required details in the window and click on Add Domain.

Step 2 The DNS Settings

After your new domain has been parked, you need to be sure that the DNS Settings are properly configured for your domain. For this you should have your DNS / Name Server information. This can usually be found in the Account Information section of your host’s cPanel. Once you have that info you need to login to your registrar’s website where the domain was registered. Here we have used Go Daddy for registration. Once you login, under My Account > Domain select the required domain name to open the domain editor and then click on Set Nameservers option under the Nameserver section.
Here you can use either of the four options to set up your Name Server.
  • I want to park my domains: This option will park your domain on GoDaddy’s parked servers.
  • I want to forward my domains: This option will forward your domain to another URL.
  • I have a hosting account with these domains: This option is used if the domain is hosted with GoDaddy.
  • I have specific nameservers for my domains: This option is used if your domain is hosted by another company. Here you need to enter the Name Servers provided by your hosting company. We will be using this option for this tutorial.
Once done click on the OK button.
If the nameserver info is changed it may take some time to propagate.
Although I have used GoDaddy and Host Gator in this tutorial, these options are very similar to any other vendor’s interface.

Step 3 WordPress MU Domain Mapping Plugin Installation

Now you need to manually install the WordPress MU Domain Mapping plugin in order to activate your domain mapping. Please download the plugin and extract the files. Copy all files (except the sunrise.php file) to your wp-content > plugins folder. Then copy the sunrise.php file to your wp-content folder.
Open your wp-config.php file and enter the following line of code under the code where you have enabled your multisite feature.
define('WP_DEBUG', false);
define('WP_ALLOW_MULTISITE', true);

define('SUNRISE', 'on'); // Add this line here:

define( 'MULTISITE', true );
define( 'SUBDOMAIN_INSTALL', false );
$base = '/';
define( 'DOMAIN_CURRENT_SITE', 'localhost' );
define( 'PATH_CURRENT_SITE', '/' );
define( 'SITE_ID_CURRENT_SITE', 1 );
define( 'BLOG_ID_CURRENT_SITE', 1 );

/* That's all, stop editing! Happy blogging. */
Save the changes.
After that you will be able to see the Domain Mapping option under the Settings menu of your Network Admin dashboard.

Step 4 Mapping The External Domain to One Of Your Child Sites

Now you are ready to map the external domain to one of your child sites. For this please click on the Settings > Domain Mapping option of your Network Admin Dashboard.
Under Server IP Address put the IP address of your web server. You may contact your host for this address or visit this site to find your site’s IP address.
Finally click on Save.
Next go to the Dashboard of one of your child sites for which you want to map the domain.
Then under Tools > Domain Mapping add the external domain which we have registered. Check the Primary Domain For this Blog checkbox to make it a primary domain for this site. Finally click on Add.
If your domain name includes a hostname like "www", "blog" or some other prefix before the actual domain name you will need to add a CNAME record for that hostname in your DNS pointing at this blog URL. For this please log in to your host cPanel and click on Advanced DNS Zone Editor under the Domains section and set up your CNAME record.
That’s it you have successfully mapped an external domain to your site. To verify please check the URL by visiting your child site.

Step 5 Final Steps

If you don’t want to utilize the original sub-domain URL of your child site any more, you may remove all the traces of the URL from the Network Admin Dashboard. For this go to Sites > All Sites and Edit the Site which you have mapped. In each of the tabs search for the old URL and replace it with the new URL.
In order to redirect users who type the old URL, please log into your cPanel and click on Redirects under the Domain section. Here you need to select the Type, the URL to be redirected and the URL to which it will be redirected. Finally click on Add.
That’s it for now, in my next tutorial I shall be explaining the WordPress Multisite Database in detail using phpMyAdmin and some really cool functions to be used in WordPress Multisite. Thanks a lot for reading."
read more