Robert Basic's blog

Archives for March, 2009

Wordpress as CMS tutorial

by Robert Basic on March 14, 2009.
Heads-up! You're reading an old post and the information in it is quite probably outdated.

Wordpress is one of the best blogging platforms out there — if not the best. It’s very powerful, can be easily extended and modified. It’s documentation is very well written and, so far, had answer to all of my crazy questions :)

You know what’s the best part of Wordpress? With some knowledge of PHP and MySql, you can turn it into much more than just a blogging platform. After doing some HTML to WP work for Roger, I thought of one way how could Wordpress be transformed into a CMS. Note the “one way”. This is not the only way for doing this, and, most likely, not the best way.

I didn’t look much, but I think that there are some nice plugins out there that can do this. But, where’s the fun in the download, upload, activate process? Nowhere!

I will show you how to change your Wordpress into a CMS and it really doesn’t take much coding to achieve this! The example presented here is simple and will have a static page for it’s home page, another static page for the “Portfolio” page and the blog. The home and portfolio page will have some of own content and both will include some content from other static pages. You all most likely know the blog part ;)

Static pages

Things you should know: each static page has it’s title, it’s slug or name (the thing that shows up in your browsers address bar: http://example.com/portfolio/ - right there, the portfolio is the slug!), has the parent attribute and the template attribute. The parent attribute is used when it’s needed to make one page a child of another, i.e. to show Page2 as a subpage of Page1. The template attribute is used when we want to apply some different layout and styling to a static page. Read more about static pages and how to create your own page templates.

If you want to, you can download the theme I created for this tutorial from here (it’s not a designers masterpiece, what did you expect?), or you can use any theme you want.

I hope you read the part on creating page templates, I really don’t feel like explaining the next part.

Create 3 new files in your template directory (if you’re using my theme, these files are already there): home.php, portfolio.php and blog.php. Contents of these files are:

// home.php
<?php
/*
Template Name: Home
*/
?>

// portfolio.php
<?php
/*
Template Name: Portfolio
*/
?>

// blog.php
<?php
/*
Template Name: Blog
*/

// Which page of the blog are we on?
$paged = get_query_var('paged');
query_posts('cat=-0&paged='.$paged);

// make posts print only the first part with a link to rest of the post.
global $more;
$more = 0;

//load index to show blog
load_template(TEMPLATEPATH . '/index.php');
?>

To understand the contents of the blog.php file, please take a look at this.

Now, go to the dashboard, the Pages section and write 3 new static pages:

  • Home, with the slug home, for the template choose Home from the drop-down list (it's on the right side) and the parent leave as is (Main Page)
  • Portfolio, with the slug portfolio, for the template choose Portfolio
  • Blog, with the slug blog, for the template choose Blog

You can add some content to the Home and Portfolio pages, but don’t add any to the Blog page.

Organizing links

Now, let’s make that when we are on http://example.com/ it shows us the Home page, instead of the Blog, and when on the http://example.com/blog/ to show us the blog!

Go to Settings->Reading and where it says “First page displays” choose “A static page”, and under the “Front page” drop-down choose “Home”.

Now, go to Settings->Permalinks and change the “Custom structure” to /blog/%postname%/ or whatever is your preferred permalinks structure, but it must start with /blog/! If Wordpress can’t write to your .htaccess file (I hope it can’t!), open it up in your editor and type the following (or similar, depends on your setup):

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /blog/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

The point is in the RewriteBase, with that we’re telling WP where to find the blog. On default setups, when http://example.com/ points to the blog, the RewriteBase is simply / but with the blog located at http://example.com/blog/ we need to change the RewriteBase. If all is well, we’re done with organizing the links.

While you’re still in the dashboard, write some new static pages with content. For the parent of these pages choose Portfolio and leave the template the default (the default page template is page.php).

Time for coding!

Here are two functions I wrote for retrieving content from static pages which will be then included in other static pages:

// functions.php
<?php

/**
* Gets last $number_of_subpages from their $parent_page
* If the <!--more--> tag is ignored ($ignore_more=true) returns the entire content of the subpages
*
* @param mixed $parent_page Contains either the slug of the parent page or it's ID
* @param integer $number_of_subpages Number of subpages to return
* @param boolean $ignore_more Whether to ignore the <!--more--> tag or not
* @return array Contents and titles of subapages
*/
function wpascms_get_subpages($parent_page='portfolio', $number_of_subpages=2, $ignore_more=false)
{
    global $wpdb;

    if(is_string($parent_page))
    {
        $parent_page_ID = wpascms_get_parent_page_ID($parent_page);
    }
    else
    {
        $parent_page_ID = $parent_page;
    }  

    if($number_of_subpages == 0)
    {
        $limit = '';
    }
    else
    {
        $limit = 'LIMIT 0, ' . $number_of_subpages;
    }
    // Get all subpages that are published and are childs of the given parent page
    // and order them by date in descending order (latest first)
    // also, if needed, limit to the latest $number_of_subpages
    $subpages = $wpdb->get_results("SELECT * FROM $wpdb->posts
                                    WHERE `post_parent` = '$parent_page_ID' AND `post_type` = 'page' AND `post_status` = 'publish'
                                    ORDER BY `post_date` DESC $limit");

    if(!$ignore_more)
    {
        foreach($subpages as $key=>$subpage)
            if(strpos($subpage->post_content, '<!--more-->') !== false)
            {
                $short_content = explode('<!--more-->', $subpage->post_content, 2);
                $subpages[$key]->post_content = $short_content[0] . '<a href="' . get_permalink($subpage->ID) . '">Read more...</a>';
            }
        }
    }

    return $subpages;
}

function wpascms_get_parent_page_ID($parent_page)
{
    global $wpdb;

    $id = $wpdb->get_var($wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE `post_name` = %s AND `post_type` = 'page' AND `post_status` = 'publish'", $parent_page));

    return $id;
}

?>

The first function, wpascms_get_subpages() returns the given number of subpages from a specific parent page. By default it will break the content on the tag and append a “Read more…” link. The first parameter can be either a string containing the slug of the parent page, or the ID of the parent page. The second parameter is the number of subpages we want returned. If it’s zero, all subpages will be returned. The second function is merely a helper function, to get the id of the parent page based on it’s slug. To read more on querying the database, read this page.

Here’s how I’m calling this function on my example Home page:

<?php
/*
Template name: Home
*/

get_header();
?>

    <div id="home">
    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>

        <h2><?php the_title(); ?></h2>

        <?php the_content('<p class="serif">Read the rest of this page »</p>'); ?>

    <?php endwhile; endif; ?>
    </div><!-- home -->

    <div id="latest_works">
    <h1>Recent work</h1>
    <?php $subpages = wpascms_get_subpages();
    if(count($subpages) > 0):
        foreach($subpages as $row=>$subpage):
        if($row%2 == 0)
        {
            $class = "left_work";
        }
        else
        {
            $class = "right_work";
        }
    ?>
        <div class="<?php echo $class; ?>">
            <h2><a href=<?php echo get_permalink($subpage->ID); ?>><?php echo $subpage->post_title; ?></a></h2>
                <?php echo $subpage->post_content; ?>
        </div>
    <?php
        endforeach;
    endif;
    ?>
    </div><!-- latest_works -->

<?php
get_footer();
?>

In words: including the header, then showing any content of the home page. After that getting the subpages: by default, wpascms_get_subpages() is getting the newest 2 subpages of the portfolio page. I’m showing the content of the subpages in 2 columns. What we got with this? Add a new subpage to the portfolio and it will automagically show up on the left side column. In the end, including the footer.

Here’s another example from the portfolio page:

<?php
/*
Template name: Portfolio
*/

get_header();
?>

    <div id="portfolio">
    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>

        <h2><?php the_title(); ?></h2>

        <?php the_content('<p class="serif">Read the rest of this page »</p>'); ?>

    <?php endwhile; endif; ?>
    </div><!-- home -->

    <div id="latest_works">

    <?php $subpages = wpascms_get_subpages('portfolio', 0);
    if(count($subpages) > 0):
        foreach($subpages as $row=>$subpage):
    ?>
        <div class="work">
            <h2><a href=<?php echo get_permalink($subpage->ID); ?>><?php echo $subpage->post_title; ?></a></h2>
                <?php echo $subpage->post_content; ?>
        </div>
    <?php
        endforeach;
    endif;
    ?>
    </div><!-- latest_works -->

<?php
get_footer();
?>

Same thing is happening here: including the header, showing the content of the portfolio page. Getting the subpages, but now all of the subpages that are childs of the portfolio page, and showing them one under the other.

All subpages can be viewed each on it’s own page, but that is just a plain ol’ page.php file, so I’ll skip that.

Don’t limit yourself to the existing plugins or waiting for one tutorial/example that will show how you can make everything. Don’t be afraid to get your hands dirty by hacking some code. It really doesn’t take too much to create magic with Wordpress ;)

Cheers!

Tags: blog, cms, example, hack, php, tutorial, wordpress.
Categories: Development, Programming, Software.

Online resources for Zend Framework

by Robert Basic on March 03, 2009.
Heads-up! You're reading an old post and the information in it is quite probably outdated.

Besides the official documentation and the Quickstart, there are many useful resources for Zend Framework, like blogs and Twitter. I did my best to collect them. If you know something that’s not listed here, but should be, please leave a comment and I’ll update the post :)

Update #1 (seconds after publishing): Gotta love Twitter. Already got a message that I missed a blog. List is updated.

Update #2: Added more blogs to the list, thanks Jani for the recommendations!

Update #3: Thanks to Federico and Pablo, even more stuff to add :)

Update #4: Thank you Jon and Cal :)

Update #5: This is growing up into a pretty big list :) new stuff added!

Update #6: Should I keep adding these Update #x lines? :)

Update #7: A bunch of new stuff!

Update #8: A new ZF application via Federico’s blog!

Blogs

Blogs are probably the most important resources out there. Besides the posts, comments can add a great value to the topic, so be sure to read them too. Here are the blogs that have posts on ZF and were updated recently (in the past month or two):

Also, I recommend subscribing to PHPDeveloper’s and Zend Developer Zone’s feeds, just in case I missed some good blogs ;)

Twitter

On Twitter there are many friendly developers willing to help out with any problems related to Zend Framework &#151 just write your question with a ZF hashtag and someone will most likely show up with the answer :)

Books

These two books are a must read. That is all :)

Surviving The Deep End — a free online book that is written chapter by chapter. Author is Pádraic Brady:

The book was written to guide readers through the metaphorical "Deep End". It's the place you find yourself in when you complete a few tutorials and scan through the Reference Guide, where you are buried in knowledge up to your neck but without a clue about how to bind it all together effectively into an application. This take on the Zend Framework offers a survival guide, boosting your understanding of the framework and how it all fits together by following the development of a single application from start to finish. I'll even throw in a few bad jokes for free.

Zend Framework in Action — OK, this book is not an online resource, but it is great and surely must be mentioned :) Authors are Rob Allen, Nick Lo and Steven Brown:

Zend Framework in Action is a book that covers all you need to know to get started with the Zend Framework.
The first part of the book works through the creation of web site using the MVC components (Zend_Controller, Zend_View and Zend_Db). The book then follows on by looking at user authentication and access control, forms, searching and email to round out the application. After considering deployment issues, we then look at other components that add value to a web site; including web services, PDF creation, internationalisation and caching.

Guide to Programming with Zend Framework — another great book, a must have. Written by Cal Evans.

This book covers much of the primary functionality offered by the Zend Framework, and works well both as a thorough introduction to its use and as a reference for higher-level tasks

Beginning Zend Framework — written by Armando Padilla

Beginning Zend Framework is a beginner’s guide to learning and using the Zend Framework. It covers everything from the installation to the various features of the framework to get the reader up and running quickly.

Easy PHP Websites with Zend Framework by Jason Gilmore

Easy PHP Websites with the Zend Framework is the ultimate guide to building powerful PHP websites. Combining over 330 pages of instruction with almost 5 hours of online video and all of the example code, you’ll have everything you need to learn PHP faster and more effectively than you ever imagined.

Applications powered by ZF

Wanna see what’s ZF capable of?

Other resources

Of course, there’s the good ol’ IRC, channels are #zftalk and #zftalk.dev. For more information, visit ZFTalk.

Jani Hartikainen’s Packageizer is a great tool to get only those ZF components you need.

Scienta ZF Debug Bar an awesome plugin for Zend Framework which “injects into every request a snippet of HTML with commonly used debug information.”

There’s also the Zend Framework Forum. For those of you who understand it, here’s a German forum www.zfforum.de.

The Zend Framework Wiki and the Zend Framework Issue Tracker are also very helpful, so, be sure to check them out.

The unofficial PEAR channel for the Zend Framework can be found at http://zend.googlecode.com/.

That’s all from me. This are the resources I found useful and hopefully are and will be useful for you too :)

Do you know anything I missed? If so, please, leave a comment and I’ll update the post :)

Cheers!

Tags: blog, book, framework, resource, site, twitter, zend.
Categories: Development, Places on the web, Programming.