Uploading and updating your theme

It’s no secret I love doing theme reviews. When I get a chance. The past couple of weeks though I’ve only been able to do one a week if I’m lucky. I do read the theme-reviewers emails when I open my inbox but of course that just isn’t enough.

Background

If you didn’t already know for the last year I’ve been stating that I am working on a theme to submit. Yes and no. The biggest thing I’ve actually been working on is a personal theme. for this site. I’ve looked at many themes and have even thought about purchasing a theme from an established, and reputable theme foundry. As you can tell I chose to use the Displace theme.

For the time being I like and it does the job. I’ve thought about modifying it but want to create my own. Maybe down the road. We’ll see. I’m getting a little sidetracked so we’ll talk about the issue at hand. Updates and the review process.

Uploading a new theme

themes-directory

The process is actually quite simple. One of the first things you want to do is go to the themes directory. As you can see from the image on the right hand side the welcome screen showcases a few featured themes and on the right hand column there are three sub-sections: Popular, Newest and Recently updated.

You will want to login with your WordPress.org username and password. After you have done that you have, and I mean have to, read more about having your theme hosted in the repository. I cannot emphasize that part enough, either. In particular the guidelines to getting your theme accepted.

That little about page is very crucial and does look a little like:

themes-directory-2
Read! And I mean read this page.

Yeah, it does seem a little daunting but it is fairly simple and straight-forward. Some information about theme-tags, guidelines and why you would want your theme in the repository.

So, you’ve signed in and want to upload your theme. You go to the upload section by clicking the Theme Authors link and you are taken to the upload screen. This is the crucial one you will want to store not only in your bookmarks but in your head as well. This page is what will create a new Trac ticket with your theme so that an actual human being will go over your code and give you feedback and hopefully approve your theme.

A ticket?

You read that right. A ticket. You will receive an email confirmation with that ticket number. If that doesn’t happen you may want to contact the theme-reviewers list and I’m pretty sure at least one person will be able to help you out.

themes-trac-intro

The next stage is the review process. When I started doing reviews it was a little tedious, to say the least, only because so much of the process wasn’t as automated as it is now.

What a reviewer would do is login to the Trac for WordPress themes, go to the report, as demonstrated in the next image, and see if there were any other tickets that have been submitted using the same username/theme and close the tickets. The reviewer would then conduct the review with the most current version of the theme.

trac-ticket-queues

What next?

The next thing you will be getting is feedback. Good, bad and maybe even some ugly. We don’t always like to hear the truth but let’s face it. We don’t always do our best the first time around. I know I don’t.

previously-approvedFrom here on out it will be a human being, an actual living, breathing, eating, human being that will read over your lines of code. That person will read every character and see every image you used to create that WordPress theme. And hopefully, just hopefully it will pass inspection. The key here is communication. Get a dialogue between both you and the reviewer. A great example is a review I did a few weeks ago to the Customizer theme as seen on the image to the left.

Now, I know you’re wondering why such a long list of tickets that developer has. At least those that looked at the image. That was because some of those are updates. If you’re not asking how you update your theme then I’m not sure we can keep talking. In real life. I kid of course.

In order to update not only your theme in the repository, but in the theme trac you will want to visit the upload page once more.

upload-signedAfter you have signed in, naturally, you will head back to the upload page, make sure you bump the version of your theme in your stylesheet and you will be on your way to sharing with the WordPress community.

 Final thoughts

As I said the key is communication. Talk to the person conducting the review and the developer. You would be amazed about what both parties can learn and you also may make a new connection in the end.

Filtering the page links

Nothing is ever truly perfect. There are some things that do come close though. In the mean time we can make just a few changes to get our desired result. The photo above is a quick comparison of a photo I took a few years back while at Yosemite National Park.

As you can see there is a huge difference between the two. One is more vibrant and full of color while the other is just a little flat with little to no contrast. Yeah, I critique my own photos constantly. The left is the raw untouched file taken with my camera and the right is the edited version. As you can see it made it better by correcting some color and adding a little more light to the overall image.

Now, in WordPress one way of altering is by using filters. Recently I’ve been messing with one in particular that I feel can be a good practice for any theme developer. Filtering the arguments of wp_link_pages() to style post pagination.

Pagination?

Yes, pagination in posts. Often times when a tutorial or a written work is far too long that it needs to be divided into segments of easy to read, easy to consume blocks of text. In WordPress the

<!--nextpage-->

tag is used in the text editor to create another page within that post. WordPress handles this in one of two ways. The first is by listing the pages and the other is by using simple next/previous links. All themes tend to style that in different ways. The default Twenty Thirteen handles it like:

twenty-thirteen-link-pages.PNG

The way it is styled uses the same code throughout all the content related files. The reason I say it that way is because 2013 has support for all the post formats available within WordPress, which is amazing! Now the downside to that is that it can mean you have to copy/paste, type out a lot more code than you’d want to. I know I can be that way. I just want to be able to set it once and be done. I mean who doesn’t, right?

So, the way 2013 is coded is by calling wp_link_pages and then passing to it an array with some settings.

 wp_link_pages(
        array(
                'before' => '<div class="page-links"><span class="page-links-title">' . __( 'Pages:', 'twentythirteen' ) . '</span>',
                'after' => '</div>',
                'link_before' => '<span>',
                'link_after' => '</span>'
        ) );

As you can see you would copy and paste that code on all the files depending on how you have all your content files setup. While it does create that nice little HTML structure for us to style it may be tedious to have to copy and paste over and over. I know I got tired of it.

Solution

What I did was looked at the source code for the function. I took notice at one particular line of code that made me happy. That line was:

$r = apply_filters( 'wp_link_pages_args', $r );

The reason it made me happy was because I realized that I could filter the arguments and not have to type out all that code over and over again or even have to copy and paste it all. So all I now had to do was create a callback function and hook it through a filter.

add_filter( 'wp_link_pages_args', 'theme_link_pages_args' );
function theme_link_pages_args( $args ){
    $args = array(
        'before'           => '<div class="link-pages">Page ',
        'after'            => '</div>',
        'link_before'      => '',
        'link_after'       => '',
        'next_or_number'   => 'number',
        'separator'        => '<span>',
        'nextpagelink'     => 'Next Page',
        'previouspagelink' => 'Previous Page',
        'pagelink'         => '%',
        'echo'             => 1
    );
    return $args;
}

*If you are going to filter the $args make sure you use all the settings or you may end up getting some errors.

There we have it! Keep in mind that if you want the theme to be translation ready don’t forget to add the proper functions where needed and prefix the function with your theme’s name.

Be friendly and filter your foot

HTML5 footer

A brief glimpse of my WordPress.com hosted blog’s footer. I’ve had that thing for nearly six years. Yep, six years. A little hard to believe that I only have about eighty posts but it’s true. Part of that reason being me branching off and trying on a self hosted site. I wish I could tell you all the things I’ve tried over the course of this branching but honestly don’t recall every little thing I’ve done.

I can honestly tell you I have learned a few things here and there from not only reading the WordPress codex  pages but from doing some theme reviews as well.

Be friendly

Yes, be friendly. What I mean by this is that you should think about who will be using your theme or plugin. True that not many people are capable of knowing how to code a site with PHP, HTML but that may not be your target, right? Odds are that you want to share your theme with the community.

That means that another developer will be looking at your code. Shock! Awe! You name the emotion, odds are that person will experience it at one point while looking at your code. The one you don’t want to bestow upon them is anger. Once that one hits their ability to trust you will dwindle.

How do you prevent it? Well, documenting is a great way. Another is by making things a little easier by adding hooks and filters to your theme.

Hooks and filters

Yes, hooks and filters. One simple solution to help everyone out. At least a good portion of people. You can’t really please everyone, unfortunately. One way of adding friendliness to your theme, or plugin, is by adding a simple action so that a child theme can easily hook to it. A quick sample would be just before the page of the theme.

< ?php do_action( 'page_prior' ); ?>
    

See? Simple, right? Then all a child theme, or even a plugin, has to do is hook to it. Now, that is a quick and painless way of making your theme a little easier to work with and a future developer will like it since they won’t have to change any files or create a separate file. But there is more! What’s even crazier to think about is that you can not only create minor changes that way but you can also make huge changes that way.

Suppose for a moment that you create a post structure that you feel would want to be changed down the road or by somebody else. Simple! You guessed it! Action hook. Now it is a little more work but you can do it with a few extra lines of code.

//somewhere in the index file
while ( have_posts() ) : the_post(); 
 do_action( 'theme_content' ); 
endwhile;

// in the functions file
add_action( 'theme_content', 'theme_content_cb' );
function theme_content_cb(){
    // inside the WordPress loop
}

Yeah it does look a little odd. But in the child theme’s function file all you would have to do is remove the action and add it again with your own callback function. So the child theme’s function would look a little like:

remove_action( 'theme_content', 'theme_content_cb' );
add_action( 'theme_content', 'child_theme_content' );
function child_theme_content(){
    // My child theme's content code
}

What is nice about that is you can easily alter an entire block of code without ever having to touch any parent theme files or having to create any duplicate files and modifying to your choosing. It is very convenient when you have to remove credits sometimes. One of the issues I see in the WordPress.org Themes and Templates forums.

Filtering

Filtering the output is key in this. Why? Because it makes things just that much easier for the developer that will be using the theme. Remember to keep your audience in mind when creating a theme, or plugin. A good portion of the time it will be a developer. Adding a filter is as easy as using

apply_filters

and you’re done.

function theme_footer_cb( $theme_footer ){
    $theme_footer =
    ';
    echo apply_filters( 'theme_footer_cb', $theme_footer );
}

With that any themer can filter the output of the theme’s footer by adding a filter in the child theme’s function file like so:

add_filter( 'theme_footer_cb', 'child_theme_footer' );
function child_theme_footer( $footer ){
    $footer = '
Created with LUV
'; return $footer; }

Not so hard, right? Now go out and make a developer happy by creating these simple little hooks and filters.

All about my Child – Theme that is

Frequenting the WordPress support forum always makes wonder a lot of things. The biggest one, I hate to admit, is do people read. I know it does come off a little mean, rude but the truth is a lot of the threads that are in the forums could easily be avoided. The number one being how to modify a specific theme.

The forums are filled with a few of these but the biggest mistakes is that people don’t really read. I want to say part of that reason is the welcome page to the forums. Don’t get me wrong I can find my way quite easily since first starting over a year ago but for a first timer you will want to look elsewhere.

The thing that people want to be able to do is modify a theme or alter it to make it their own. The best way to do this is by creating a child theme. The codex has a good quick and simple tutorial and then gets a little more detailed about how child themes function.

I’ll break things down into steps. Why? Because that’s how most of us read and follow directions, right?

  1. Create empty folder
  2. Create style.css
  3. Active

Create an empty folder

One of the first things to do is create a folder. It really doesn’t matter what you name the folder. 

Create a new folder
Create a new folder

I chose twentysumchild for demonstration purposes but it really could be anything you want. In that folder we will then create a new file. It will be the main file for our child theme. Can you guess the name? Yep,

style.css

will be the newly created file.

Create style.css

Pick any text editor and you will add the needed format so WordPress knows what to do and how to interact with our newly created theme. In our randomly named folder we will now have a single file called: style.css

In this file we will add the CSS header information so that WordPress will show it on our Themes selection page.

Sample information for style.css
Sample information for style.css

As you can see I chose to name my child theme ‘My Randmoizer’ but really it can be anything you want it to be. The key to making the child theme is actually the keyword “Template.” It sounds a little odd but it is true. If you for some reason get a message that there is a template file missing it may be due to that missing line of code.

Now all that is left is to activate the theme.

Activate

Child theme activated
Child theme activated

I didn’t use a screenshot only because I didn’t have much time and I got a little lazy towards the end. A good size for one is 600 pixels by 450 pixels.

As you can see I chose to base my child theme on Twenty-Thirteen but you can create a child theme from just about any theme. Just don’t forget that the key word is: Template

So go, explore, tweak and hack away to your heart’s content. Make as many changes as you want knowing that you will not lose any changes when that theme gets updated.

Understanding arrays

Backstory

For the last month I have read about JavaScript ( ECMAScript ) and how it works. To say the least I am one step closer to not being as bald as I thought I would be. JavaScript is a great language to learn and has so much potential behind it and for it.

What caught me a little off guard was the term object-oriented. For the longest time I hadn’t the slightest idea what that meant. I finally reached that pivotal moment when it hit me. That little moment of: that makes total sense.

What are arrays?

Arrays, simply put, are lists. Yes, it does sound sort of strange but it is true. Arrays are lists. In just about any programming language arrays are used to store not only information but information about that information. Yes, more information within information.

A great way of thinking what arrays are is to think of them as lists; which is kind of what they are. A good example is the little JavaSvript snippet:

var groceries = new Array( 'tomato', 'cucumber', 'squash', 'zucchini' );

Simple right? Now, what if we want to deal with and store more information? For that we introduce associative arrays.

Associative Arrays

Not the suit wearing type but the mathematical type. At least for me it makes sense. Not sure if it will help anybody else but here goes. An associative array is an array within an array.

Inception much?

A really great way to look at this is by not only looking at the code but thinking about lists. Yes, lists. Again. A great example of this is a restaurant’s menu.

$menu = array(
    'pizza' => array( 'cheese', 'pepperoni', 'sausage' ),
    'potato' => array( 'fried', 'baked', 'chopped', 'chip' ),
    'drink' => array(
        'soda' => array( 'diet', 'regular' ),
        'alcohol' => array( 'beer', 'wine' )
        ),
    'salad' => array( 'house', 'caesar' )
    );

Wow! That is quite a bit of code to show my point but I will run through it pretty quickly.

We start with the basic menu items: pizza, potato, drink and salad. Each one of those items then has its own bit of information attached to it. Pizza has the toppings, potato shows how it is cooked, the drink one is a little tricky in that it has two different arrays for soda and alcohol.

Get it? Got it?

And there we have a small glimpse of how I am capable of understanding arrays and associative arrays. Very dumbed down but it is a very clear way of looking at it.

Creating a custom comment list

What?

Comments. That is the topic. As some may know I’m in the middle of creating a WordPress theme to submit to the repository. I want this theme to not only pass but to pass with flying colors. I followed about as close to how the default themes are but have also integrated some from themes I have reviewed and added a bit of my own style, of course.

What started this?

What really initiated this was the default theme for WordPress 3.6: Twenty Thirteen. Previous default versions all passed a certain set of arguments and made a callback function in order to display the comments rather than have WordPress handle it. Wait? You mean to tell me that I can let WordPress do the coding for me? Yes. If you don’t like having control of how to structure your code. Twenty thirteen lets WordPress handle the coding of the comment list.

Callback?

Yes, a callback function. Simple and you have more control of how the code is structured and displayed. The function that handles this is:

wp_list_comments()

All that is needed now is some arguments:

$defaults = array(
    'walker'            => null,
    'max_depth'         => '',
    'style'             => 'ul',
    'callback'          => null,
    'end-callback'      => null,
    'type'              => 'all',
    'page'              => '',
    'per_page'          => '',
    'avatar_size'       => 32,
    'reverse_top_level' => null,
    'reverse_children'  => '',
    'format'            => 'xhtml', /* or html5 added in 3.6  */
    'short_ping'        => false, /* Added in 3.6 */
);
wp_list_comments ( $defaults );

Now, the thing that creates the comments is the callback. It tells WordPress to run the default code if no argument is passed. If there is one, then WordPress will look for that function and use that instead. In Twenty Twelve it was:

wp_list_comments( array(
    'callback' => 'twentytwelve_comment',
    'style' => 'ol'
     ) );

In the Twenty Thirteen theme it actually uses the default HTML5 markup by not setting a callback function and using the new argument:

'format' => 'html5'

This tells WordPress to use the HTML5 markup for the comment which is:

<article class="comment-body">
 <footer class="comment-meta">
 <div class="comment-author vcard"></div><!-- .comment-author -->
 
 <div class="comment-metadata">
 <a href="">
 <time></time>
 </a>
 <span class="edit-link">Edit</span>
 </div><!-- .comment-metadata -->

 <p class="comment-awaiting-moderation"></p>

 </footer><!-- .comment-meta -->

 <div class="comment-content"></div><!-- .comment-content -->

 <div class="reply"></div><!-- .reply -->

</article><!-- .comment-body -->

Keep in mind that is the very simplified version without any WordPress comment template tags. Just straight HTML5 markup. Some people don’t mind using that while others like to have more control and would like to style in a different way. In order to do that we have to pass an argument to the callback function argument.

Enter the callback

This is why you want to use a callback function to create and style the comment. In order to do this we pass the argument

'callback' => 'my_theme_comment'

in our array of arguments much like Twenty Twelve does. Next we have to create the actual function that will house our newly created and customized comment with custom markup. Where that function resides is entirely up to you.

Once we have created the function we can start with all the markup and comment template tags we so desire. I personally like to keep things fairly simple and just have the comment author, date and text. Often my code looks like:

function custom_comments( $comment, $args, $depth ) {
    $GLOBALS['comment'] = $comment;
    switch( $comment->comment_type ) :
        case 'pingback' :
        case 'trackback' : ?>
            <li <?php comment_class(); ?> id="comment<?php comment_ID(); ?>">
            <div class="back-link">< ?php comment_author_link(); ?></div>
        <?php break;
        default : ?>
            <li <?php comment_class(); ?> id="comment-<?php comment_ID(); ?>">
            <article <?php comment_class(); ?> class="comment">

            <div class="comment-body">
            <div class="author vcard">
            <?php echo get_avatar( $comment, 100 ); ?>
            <span class="author-name"><?php comment_author(); ?></span>
            <?php comment_text(); ?>
            </div><!-- .vcard -->
            </div><!-- comment-body -->

            <footer class="comment-footer">
            <time <?php comment_time( 'c' ); ?> class="comment-time">
            <span class="date">
            <?php comment_date(); ?>
            </span>
            <span class="time">
            <?php comment_time(); ?>
            </span>
            </time>
            <div class="reply"><?php 
            comment_reply_link( array_merge( $args, array( 
            'reply_text' => 'Reply',
            'after' => ' <span>&amp;amp;darr;</span>', 
            'depth' => $depth,
            'max_depth' => $args['max_depth'] 
            ) ) ); ?>
            </div><!-- .reply -->
            </footer><!-- .comment-footer -->

            </article><!-- #comment-<?php comment_ID(); ?> -->
        <?php // End the default styling of comment
        break;
    endswitch;
}

And there we have a custom comment that will be used instead of the default WordPress one.

Creating a sticky post slider

Brief background

Creating a slider is simple. For some. In WordPress there are a lot of ways to creating a post slider, image slider, category slider and just about any type of slider you could possibly imagine. The one I wanted to share is creating one with sticky posts. You know the ones that you have to pick from the slightly hidden options.

Make post sticky

Yeah. That little guy. The way WordPress handles sticky post is by putting them on the front page and keeping them on top regardless of when it was posted. A good example is the quick image I created to help illustrate.

The requirements

WordPress has plenty functions and plenty of ways of getting the required posts. One way of getting posts is using get_posts() but that isn’t the method I’ll be using. I’ll be using the WP_Query and alter it just a little bit. No, not with query_posts because that will just cause the world to implode. Okay, not really but there are several other reasons not to use that function.

The steps

In order to have a good functional featured post slider we first need to layout the steps needed. What I did initially was make a quick list of the steps.

  1. Get the posts
  2. Check if there are any ‘sticky’ posts
  3. If there are then run our code
  4. Ignore sticky posts in regular feed
  5. Drink up

The code

Now that we have our logic we can get started.

The first thing we have to do is get all the posts that are set to be sticky. We create and array to hold these.

$stickies = get_option( 'sticky_posts' );

We need to count how many posts there are and we’ll store that as a variable so we can use it later on.

$count = count( $stickies );

Now that we have an array and the total we can start a new query for only those posts. The way we do this is by creating a new WP_Query and passing some arguments. So we’ll create these arguments and name our newly created object of posts $featured. It can be whatever you want it to be. If you are submitting to the WordPress repo then prefix it with your theme or equivalent.

$args = array(
	'post__in'       => $stickies,
	'posts_per_page' => 3,
	'post_type'      => 'post',
	'nopaging'       => true
	);
$featured = new WP_Query( $args );

Now that we have our new object filled with posts we can run our code with the newly created query. But wait! When WordPress is first installed there are no sticky posts so then how do we check for that? Simple. Remember that we stored the value into a variable called $count? We’ll use that. We’ll use an if statement to run our code. If there are no posts then we’ll begin the newly created WordPress loop.

if ( $count > 0 ) {
  // run WordPress loop using $featured
}

Sounds simple now that we have it all together, right? Now we can create the loop and fill it with all the goodies and information we want using template tags. Don’t forget to use wp_reset_postdata() to reset our post data. There is one last thing we must do to get it all working right and that is hooking to pre_get_posts.

What?!

One more thing?

Yes, one last thing. I know a bit of a curveball but this is what will prevent the doubling of sticky posts. What we have to do is add just a few lines of code to our functions file of our theme.

add_action( 'pre_get_posts', 'my_awesome_theme_pre_posts' );
function my_awesome_theme_pre_posts( $query ){
    if ( is_home() ){
        $query->set( 'post__not_in', get_option( 'sticky_posts' ) );
    }

    return $query;
}

Now that we have that done we have our final code snippet:

// Create array of all the sticky_posts
$stickies = get_option( 'sticky_posts' );

// Count how many there are, if any
$count = count( $stickies );

// Create a set of arguments to pass
$args = array(
	'post__in'       => $stickies,
	'posts_per_page' => 3,
	'post_type'      => 'post',
	'nopaging'       => true
	);

$featured = new WP_Query( $args );

// If there is one or more sticky post we create our new slider
if ( $count > 0 ) : ?>
<section class="featured" id="featured-slider">
    <?php while( $featured->have_posts() ) : $featured->the_post(); ?>
    <article <?php post_class( 'featured' ); ?> id="post-< ?php the_ID(); ?>">
        <h1 class="featured-title"><?php the_title(); ?></h1>
        <div class="content"><?php the_content(); ?></div>
        <div class="content"><?php wp_link_pages(); ?></div>
        <footer class="meta">Posted: <?php the_time( get_option( 'date_format' ) ); ?></footer>
    </article>
    <?php endwhile; wp_reset_postdata(); ?>
</section><!-- end of our featured article slider -->
<?php endif; // end the featured posts ?>

Enjoy

And done! For the most part since we need to add interaction but I’ll let you pick out what to use since there are plenty of jQuery slider plugins to help with that.