Turn WordPress into a CMS with Pods Framework

PHP Master | Turn WordPress into a CMS with Pods Framework

If someone mentions the term Content Management System, probably WordPress, Joomla, Drupal, and others come to mind. But the only content types supported by WordPress out of the box are pages and articles, which is fine for most websites, but what if you want more? Luckily you can use the Pods Framework; it’s a WordPress plugin which enables you to add custom content types and their management to your WordPress installation.

I’ll show you some of Pods Framework’s functionality by giving you a quick tutorial on how you we can use it to make a library site. The site will contain an overview page with all of the books we currently have, and when the user clicks on a book link he’ll see its details.

Adding Content Types

First you have to install the framework. Just use the plugin installer in your WordPress backend like you always do; the full name of the plugin is Pods CMS Framework.

After installation, click on the Pods menu item in the side menu. Like the description says, a pod is a named group of input fields, which means that it’s a custom content type with properties which you define. Click the “Add new pod” button and create a pod named “books”.

After you make the book pod, the screen changes to show its properties:

By default, our new pod already has the columns name and slug. In this example, we won’t need slug, so just click the red X to the left of it to delete it. The book does however need some more information, such as its publisher, description, genre, author, publication date, and the book cover. Let’s create them now.

At the right you’ll see the ‘Add Column’ section. Enter appropriate column names in the Machine Name field and use ‘Single Line Text’ for the Column Type, except for the publication date and description. The type for the publication date should be Date, and the description should be ‘Paragraph Text’. As for the cover, we can just store an URL string to the image.

Pages and Templates

Once the pod is configured, go back to the side Pods menu and click Add book.

Add a few of your favorite books as an example, providing the necessary information and saving your work.

To display our pages correctly, we’ll need to make a small theme modification. Go to the theme’s directory (I’m using Twentyeleven for this example) and create a new file named pods.php. The content should be:

<?php
/*
Template Name: Custom Pods template
*/

get_header();
?>
<div id="primary">
 <div style="width: 93%;" id="content" role="main">
  <article id="post-0" class="post page hentry">
   <?php pods_content(); ?>
  </article>
 </div>
</div>
<?php
get_sidebar();
get_footer();
?>

Be sure to include the Template Name comment at the top of the file. This way, WordPress will detect it as template file and name it the same way you named it.

Now for our visitors to see the book collection we need to create the listing page to show all the books which are currently in the database. Go to Pods’ Setup menu, then the Pages tab, and then click ‘Add new page’. Name it ‘books’.

You should now see a text field where you can enter the code you want for that page. You can include HTML, Javascript, CSS and PHP. Use the following:

<h1><b>Book List</b></h1>
<?php
$pods = new Pod('book');
$pods->findRecords('id DESC', 10);
while ($pods->fetchRecord()) {
    echo '<li><a href="/book/' . $pods->get_field('id') . '">' . $pods->get_field('name') . '</a></li>'; //Create a link to each different book
}
?>

Then, set the Page Template select to ‘Custom Pods Template’ and save your work.

In order to view the pages correctly, there’s one additional configuration change we need to make. Go to Settings > Permalinks, select ‘Custom Structure’, and in the text field type ‘/%postname%/’. Now you should be able to view the page you just created by going to yoursite.com/books.

We still need to create a page which displays the book information, so click Pods’ ‘Add new page’ button again and use the name ‘book/*’. The * is important because it is a wildcard in the URL, in our case the id of the book.

We can retrieve the first wildcard value (and in our case the only one) by with the pods_url_variable() function.

This is the code to use for our books/* page:

<?php
// store the book’s id into $bookId
$bookId = pods_url_variable(1);

// retrieves all book entries which fullfill the parameter requirement
$params = array('where'=>'t.id = '. $bookId);
$book = new Pod('book', $params);

// the pods is mostly a data class which contains multiple pod objects
while ($book->fetchRecord()) {
    // use get_field() to retrieve the value of a specific field
    echo '<h1>' . $book->get_field('name') . '</h1><br />';
    echo '<img src="' . $book->get_field('cover') . '" /><br />';
    echo 'Autor: ' . $book->get_field('autor') . '<br />';
    echo 'Publisher: ' . $book->get_field('publisher') . '<br />';
    echo 'Release date: ' . $book->get_field('release') . '<br />';
}
?>

Now we can easily manage our book collection and show it to the world. You can see that with the Pods Framework you can easily create a powerful website using WordPress. Actually, the possibilities are almost unlimited since with wildcards you can emulate a routing system that other frameworks might use.

For more information about the different Pod methods, you can visit the Pods Codex and ask for support in the Pods Forums.