WordPress: Add a theme option to display or hide the post author

Since I’m (at the time) the only author on my blog, I just wanted to get rid of the “By hb” next to all my posts. But since I thought I might need to have it back some day, it’d be nice to be able to configure it in the theme options.

So first I needed to add it to the theme options. That’s pretty easy, open options.php in the theme editor. You’ll see something like this:

global $theme_options;
$theme_options = array (

)

This is the array containing all the theme options and their headers. This is basically an array of arrays where each of the contained array has either these two entries:

‘name’ => < name of the heading >
‘type’ => ‘heading’

or these four entries:

‘id’ => < ID of this theme option >
‘name’ => < name of this theme option >
‘desc’ => < additional text displayed for this option >
‘type’ => ‘checkbox’, ‘textarea’, ‘text’, ‘numeric’, ‘widetext’…

or these four entries:

‘id’ => < ID of this theme option >
‘name’ => < name of this theme option >
‘type’ => ‘select’
‘options’ => Options in the combobox

So you have to add the following at the appropriate place in options.php:

array(
‘id’ => ‘theme_show_post_author’,
‘name’ => __(‘Show post author’, THEME_NS),
‘desc’ => __(‘Yes’, THEME_NS),
‘type’ => ‘checkbox’
),

Now you can see the option. In order to define a default value for this option, you’ll need to update defaults.php and add the following line (to the $theme_default_options array):

‘theme_show_post_author’ => 1,

Now everything is done on the configuration side. We just need to use this new theme option.

First we need to find out where the author is added. So let’s go in a shell to the directory where this theme is located and search for the_author:

# grep -R the_author *

functions.php: get_author_posts_url( get_the_author_meta( ‘ID’ ) ),
functions.php: sprintf( esc_attr(__( ‘View all posts by %s’, THEME_NS )), get_the_author() ),
functions.php: get_the_author()

In my case these three lines were located in the function theme_get_metadata_icons(). So all I needed to do was find out where this function is called and update all these files to not get the name of the person who published the post:

# grep -R theme_get_metadata_icons * | awk -F : ‘{ print $1; }’ | grep -v functions.php | sort -u
content-aside.php
content-attachment.php
content-gallery.php
content-page.php
content.php
content-single.php

So I basically added the following if statement to all these files:

if (theme_get_option(‘theme_show_post_author’)) {

}
else {

}

(getting the author name in the first block and not in the second one).

That’s it. The author name is now gone and I just need to check a box to get it back !

Leave a Reply

Your email address will not be published.