Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Building a WordPress theme from scratch

Building a WordPress theme from scratch

This is a presentation I used for a kids workshop on WordCamp Bangkok. You might find it useful when starting a career in WordPress theme development. (Keep in mind that this presentation is aimed for kids of 15+ age, and it's not focused on any of the advanced techniques used when building WordPress themes)

Emanuel Blagonic

February 20, 2017
Tweet

More Decks by Emanuel Blagonic

Other Decks in Programming

Transcript

  1. 10:00 Introduction 10:15 Chapter 1: Building a basic WordPress theme

    10:45 Chapter 2: Understanding WordPress theme building process 11:15 Break 11:30 Chapter 3: Let’s build a real WordPress theme 13:00 Q&A TODAY’S PLAN
  2. — Let’s write a couple of posts and pages —

    Add images inside if you want — Add featured image to at least one post — Use read more on at least one post — Add excerpt to at least one post BEFORE WE START
  3. — Templates files (like page.php, header.php, footer.php etc.) exist within

    a theme and express how your site is displayed. — Page Templates are those that apply only to pages to change their look and feel. TERMINOLOGY
  4. — Template Tags are built-in WordPress functions you can use

    inside a template file to retrieve and display data (such as the_title() and the_content()). — Template Hierarchy is the logic WordPress uses to decide which theme template file(s) to use, depending on the content being requested. TERMINOLOGY
  5. — for every WordPress theme to work (so you can

    activate it), you’ll need only two files, really – just two files :) — index.php is where the code for theme goes — style.css is to give your theme some basic information like who is the author, what textdoman does it use etc. OUR BASIC THEME
  6. /* Theme Name: My WordPress Theme Theme URI: https://www.yourwebsite.com/my-wordpress-theme Author:

    Your Name Author URI: https://www.yourwebsite.com Version: 1.0 Text Domain: mywptheme */ STYLE.CSS
  7. — To add screenshot to our theme, just save screenshot.png

    in your theme folder. Screenshot has to be 1200x900px PNG file ADDING A THEME SCREENSHOT
  8. — get_header() and get_footer() will use the default code until

    we add our own header.php and footer.php files REMEMBER!
  9. <!doctype html> <html <?php language_attributes(); ?>> <head> <meta charset="<?php bloginfo(

    'charset' ); ?>" /> <?php wp_head(); ?> </head> <body <?php body_class(); ?>> HEADER.PHP
  10. <?php if ( have_posts() ) : ?> <?php while (

    have_posts() ) : the_post(); ?> <!-- our content goes here --> <?php endwhile; ?> <?php endif; ?> THE LOOP
  11. — If there are any articles that needs to show

    on the page requested – show them — While there are articles to show, show them and then – continue with the next one THE LOOP EXPLAINED
  12. <?php get_header(); ?> <?php if ( have_posts() ) : ?>

    <?php while ( have_posts() ) : the_post(); ?> <!-- our content goes here --> <?php endwhile; ?> <?php endif; ?> <?php get_footer(); ?> INDEX.PHP
  13. — header.php is for showing page header code — footer.php

    is for showing page footer code REMEMBER!
  14. — single.php is for showing single post — page.php is

    for showing single page — functions.php is used for our custom functions — sidebar.php is used to show sidebar widgets REMEMBER!
  15. <article> <h1><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h1> <?php if(has_excerpt()) {

    ?> <div class="excerpt"><?php the_excerpt(); ?></div> <?php } else { ?> <div class=“content"> <?php the_content( __('Read more','mywptheme') ); ?> </div> <?php } ?> </article> SHOWING A POST
  16. the_content( __('Read more','mywptheme') ); // or echo esc_html__( 'Text', 'mywptheme'

    ); // or _e( 'Jump to <strong>navigation</strong>', 'mywptheme' ); IN YOUR FILES
  17. — is_front_page() – when the front of the site is

    displayed, whether it is posts or a Page. — is_single() – when a single post of any post type (except attachment and page post types) is being displayed. — is_single( ’17’ ) – when a single post with a specific ID is being displayed. CONDITIONAL TAGS
  18. — is_page() – when any Page is being displayed. —

    is_page( ’17’ ) – when a Page with a specific ID is being displayed. — is_page_template( 'about.php' ) – when a Page Template 'about' is being used. CONDITIONAL TAGS
  19. — is_author() – when any Author page is being displayed.

    — is_archive() – when any type of Archive page is being displayed. Category, Tag, other Taxonomy Term, custom post type archive, Author and Date- based pages are all types of Archives. — is_404() – when there’s an 404 error page. CONDITIONAL TAGS
  20. — get_header() – will include header.php. — get_footer() – will

    incude footer.php. — get_sidebar() – will incude sidebar.php. — get_search_form() – will incude search.php. INCLUDING HEADER, FOOTER, AND SIDEBAR
  21. — get_template_part( ‘navigation’ ) – will include navigation.php template part.

    — get_template_part( ‘articles’, ‘archive’ ) – will include articles-archive.php template part. — get_template_part( ‘partials/navigation’ ) – will include navigation.php from the “partials” directory. INCLUDING TEMPLATE PARTS
  22. — wp_head() – placeholder for all additional actions plugins or

    WordPress may use (like scripts, styles etc.). Use it! — wp_footer() – placeholder for all additional actions plugins or WordPress may use. Use it! — body_class() – display the classes for the body element. TAGS YOU SHOULD ALWAYS USE! LIKE, ALWAYS!! :)
  23. — get_bloginfo() – retrieves information about the current site. —

    get_bloginfo( ‘name’ ) – site title. — get_bloginfo( ‘description’ ) – site tagline. — get_bloginfo( ‘url’ ) – the Site address (URL). TAGS USED TO SHOW BLOG INFORMATION
  24. — get_home_url() – retrieves the URL where the front end

    is accessible. — get_site_url() – retrieves the URL where WordPress application files (e.g. the wp-admin/ folder) are accessible. — get_template_directory_uri() – retrieves theme directory URL. TAGS TO GET URLS
  25. — wp_nav_menu( $args ) – displays a navigation menu for

    given arguments (keep in mind that you need to define a navigation menu in your functions.php for this to work). — wp_list_pages() – retrieve or display list of pages. — wp_list_categories() – display or retrieve the HTML list of categories. TAGS TO SHOW NAVIGATION
  26. — the_title() – displays or returns the title of the

    current post. — the_excerpt() – displays the post excerpt. — the_content() – displays the post content. TAGS USED WITHIN THE LOOP
  27. — the_permalink() – displays the URL for the permalink to

    the post. — the_category() – displays a link to the category or categories a post belongs to. — the_tags() – displays a link to the tag or tags a post belongs to. TAGS USED WITHIN THE LOOP
  28. — the_date() – Displays or returns the date of a

    post, or a set of posts if published on the same day. — get_the_date() – retrieves the date the current post was written. Will always return the date. TAGS USED WITHIN THE LOOP
  29. if ( function_exists( 'add_theme_support' ) ) { // Add support

    for featured images first add_theme_support( 'post-thumbnails' ); // Add a post thumbnail size for ‘article photo’, hard crop add_image_size( ‘article-photo', 1200, 600, true ); } FUNCTIONS.PHP
  30. if ( has_post_thumbnail() ) { // We want to show

    our custom defined size the_post_thumbnail( ‘article-photo' ); } WITHIN THE LOOP
  31. <?php $query = array( ‘posts_per_page' => '3', ); $query =

    new WP_Query($query); ?> <?php if($query->have_posts()) : ?> <?php while ($query->have_posts()) : $query->the_post(); ?> <!-- Code within the loop goes here --> <?php endwhile; ?> <?php endif; ?> <?php wp_reset_query(); ?> CUSTOM WORDPRESS QUERY
  32. Q&A