Tuesday, April 20, 2010

Inside wordpress: notes about common variables and functions to add extra features

I've recently used wordpress to make some simple web sites. Wordpress is a very fast solution when creating a blog or a site of which the requirements are just a subset of the wordpress features.

What we can do with wordpress: fixed pages, site news (that are blog posts), simple search, multi-level page and news content management, WYSIWYG editor, changeable themes, lots of free themes already available, url rewriting etc...).

Sometimes we need to extra customize the frontend aspect of the site/blog, and we have to modify the standard behaviour of wordpress. It needs to know a little how its atchitecture, how it works and some of its functions.

Obsolete architecture
Since the first wordpress was released on 2003, its architecture is still using a non OOP style and it has nothing to do with a modern framework, nor a MVC application. Lots of informations are kept in global variables and there are thousand of functions.

Folllowing, some my notes about its architeecture, in order to speed up any type of structural change or extra customization.

Loading chain and main global variables

Wordpress (v.2.9.2, the latest at the time of writing) loads the content in the following order/hierachy:
  • index.php includes wp-blog-header.php that
    • includes wp-load.php
      • defines ABSPATH (root directory)
      • sets error_reporting
      • includes wp-config.php
        • defines db parameters, lang, prefix
        • includes wp-settings.php
          • unregister globals, fix ISS, check PHP versions and mysql extension
          • defines WP_CONTENT_DIR, WPINC ,WP_LANG_DIR
          • requires compat.php (defines function not existing in some PHP versions), functions.php, classes.php
          • includes wp-includes/wp-db.php that istantiates the global variable $wpdb (object of the class wpdb) with methods: get_row, insert, prepare, query,insert, update, etc...
          • includes wp-includes/cache.php that defines the functions wp_cache_add, wp_cache_get, wp_cache_init (initially called), etc...
          • includes all the other files in wp-includes
          • instantiates some global variables (often used)

            $wp_the_query = $wp_query = & new WP_Query();
            $wp_rewrite = new WP_Rewrite();
            $wp = new WP(); #defined in wp-include/classes.php
            $wp_widget_factory = new WP_Widget_Factory();
            $wp_locale = new WP_Locale();

          • init wordpress
            $wp->init();

    • launch wp() that launches $wp->main() (class WP)

      the method main() calls the other (quite self-explanatory) methods: parse_request (parsing of the URL and ), send_headers, query_posts (), handle_404 (), register_globals ()

    • includes wp-includes/template-loader.php, that depending on the content and user agent, includes the templates for homepage, or single page, or category etc...

      Functions used to switch the content and include the template:

      if ( is_home() ) { include( get_home_template() ); }

      if (is_page() ){ include( get_page_template() ); }

      [... the same with 404, search, single, category, date,archive,paged..]

      if ( is_feed() ) {
      do_feed(); }


How to customize the Homepage template:
In case the homepage is loaded (and the default template is enabled), the theme templates are loaded. For the homepage and default template, the file included is

/wp-content/themes/default/index.php
.

These functions call the homonym methods of the global object $wp_query of class WP_Query (wp-includes/query.php). This object contains most of the variables needed for displaying the blog (posts, counting of posts and comments, type of page :single/archive/list/search/home/ query string, request, etc...


Some functions used inside the main template:
have_posts() #returns true if there still are posts to display in the current page
the_post() #get next post (iterator)
the_ID() #prints the ID of the post
the_content() #prints the content of the post


Other common and useful functions:
get_option($name) gets the options (using cache) from the DB table wp_options.
is_paged(), is_search(), is_category() and others: they return the correspondent boolean values in the $wp_query global object if the current page is respectively paged, a search page, a category page etc.. See other function (and methods) in the file wp-includes/query.php

To make queries:
use the object $wpdb (wpdb class), and execute the method query() on it. This variable is visible inside templates (as well as $posts, $post,$wp,$user_ID etc...)

$wpdb->query("delete from ...");


Post moved here: Inside wordpress variables functions extra features

No comments:

Post a Comment

 

PHP and tips|PHP