Please help support the continued development of AZIndex, particularly if you are using the plugin in your work or as part of a for-profit website. All contributions, no matter what size, will be gratefully received.

Tweaking Themes the Plugin Way

How many WordPress users install a brand new theme for their blog and never feel the need to tweak it in some way?  Not many, I’d wager.  While there are many great themes out there, there’s almost always something that’s not quite right for your blog.  Perhaps the blog title is too big, or something in the sidebar doesn’t look quite the way you want it to.

And so it’s off into the theme files you go—fumbling around with functions.php, header.php, and style.css, until you get it the exactly the way you want your blog to look.  I should know, I’ve never met a theme I didn’t want to tweak in, oh so many ways. (That doesn’t make me a person, does it?)

Anyway, there’s usually no real harm in tweaking a few theme files now and again, until you find out that there is a new version of the theme, with even more bells and whistles that you are just dying to try out.

Fine.  Just a quick download and… uh oh… you just overwrote all that painstaking work you did to get the previous version looking just right.  Even if you did remember to back everything up the first time, there’s still a good chance that you’ve forgotten where you put all the little modifications and tweaks you made.  I know of what I speak!

So, is there a better way?  Of course there is, otherwise what would be the point of this article?  For many of the theme tweaks you want to make, you can use a plugin instead of attacking the theme files.

“A plugin?” I hear you gasp.  “But aren’t they all hundreds of lines of code written by those geeky programmers?”  Well, yes they are, for the most part, but there’s no reason why a plugin can’t contain just a few lines of code to do something really useful.  In fact, I’m about to show you a plugin you can use with any theme that’s only six lines long (excluding all those comments that clutter up the place).

Consider how many times you want to add some more CSS styles to your theme’s main stylesheet (style.css) or maybe tweak the values that are already in there.  Unless you are extremely conscientious about comments, you will soon forget which values you added or changed.  It would be much better if you could consolidate all your changes in another CSS file (e.g. my-style.css) that you know will never be overwritten.  You can certainly do this by modifying the header template of your theme and including a <styles> tag, but even that modification should not be necessary.  You can use a plugin instead.

And here’s the code:

add_action('wp_head', 'my_add_headers');

function my_add_headers() {
   if (file_exists(get_stylesheet_directory().'/my-style.css')) {
      echo '<link rel="stylesheet"href="'.get_stylesheet_directory_uri()
           .'/my-style.css" type="text/css" />';
   }
}

That’s all there is to it.  The function my_add_headers() checks to see if there is a my-style.css file in the current theme’s folder, and if there is one, adds a link to it into the HTML header section of your blog page.  So the code works fine whether or not you decide to create your own stylesheet file.

The magic to make this happen is in the first line.  The add_action() function call “hooks into” WordPress just after it has generated all the HTML headers your blog needs for displaying a page.  (WordPress has dozens of  these “action” and “filter” hooks that can be used to inject your own code and HTML into a blog page.)

The one we are using here is the wp_head action which, as the name suggests, is invoked just before the </head> end tag is added to the HTML going to the browser.  This is an excellent place to hook in your own stylesheet file without having to change any existing theme files at all.

The great thing is that this will work for any theme you use.  Anytime you switch to a different theme, all you have to do is create another my-style.css file in that theme’s directory (or copy an old one over) and you’re done. No more messing about with theme files.

Now all that’s left to do now is to bundle this code up into a plugin, so that WordPress will recognize it properly.  That’s pretty easy too.  Just add the following comment lines to the top of the file:

/*
    Plugin Name: My Tweaks
    Plugin URI: none
    Description: Customize the theme without modifying any template files
    Version: 1.0
    Author: Your name here
    Author URI: Your home page URL here
*/

All you really need here is the Plugin Name: line, but the rest of the information will show up on the Manage Plugins page, so it’s useful to put something in there so you remember what it is when you look at it in several months’ time.

Finally, because this is PHP code, you need to add the <?php and ?> tags around the whole thing, so the complete file looks like this:

<?php
/*
    Plugin Name: My Theme Tweaks
    Plugin URI: none
    Description: Customize the theme without modifying any template files
    Version: 1.0
    Author: Your name here
    Author URI: Your home page URL here
*/

add_action('wp_head', 'my_add_headers');

function my_add_headers() {
   if (file_exists(get_stylesheet_directory().'/my-style.css')) {
      echo '<link rel="stylesheet"href="'.get_stylesheet_directory_uri()
           .'/my-style.css" type="text/css" />';
   }
}
?>

Create an new file called something like my-tweaks.php and place it in a directory with a name like my-tweaks (imaginative, or what?).  Then add the file and directory into a zip file, and you’re ready to install it.  Go to Plugins » Add New in your blog’s administration pages and upload the zip file to your blog by clicking the Install Now button.

Your plugin will install in a few seconds, and at the next screen just click the Activate Plugin link to finish the process.  You will be taken to the main plugin page where you will see your new plugin taking pride of place amongst all the other plugins you installed.

If you’re worried about the process of making your own zip file, here is one I prepared earlier:

zipfile

Simply download this to your computer and install it using the instructions above. If you want, you can edit the information in the plugin comment by clicking the Edit link next to your plugin in the list of installed plugins once it is running on your blog. (Be careful not to change anything outside the comment lines unless you know what you’re doing.)

Now you can start adding all those new CSS styles into your my-style.css file and they will immediately be picked up by your blog.  Remember, because your stylesheet file is added after the main style.css file in the page header, you can also override the default theme CSS values with your own.  (You may have to use the !important tag for to override them successfully in some cases, but you should only use that when you really have to.)

Stay tuned for some more interesting things you can do with your new tweaks plugin.

287 comments to Tweaking Themes the Plugin Way

  • I’d should verify with you here. Which is not one thing I often do! I enjoy studying a submit that will make individuals think. Also, thanks for allowing me to remark!

  • Excellent read, I just passed this onto a colleague who was doing some research on that. And he just bought me lunch since I found it for him smile Therefore let me rephrase that: Thank you for lunch!

  • It’s actually a nice and helpful piece of information. I am glad that you just shared this helpful information with us. Please stay us informed like this. Thank you for sharing.

  • You can definitely see your expertise in the paintings you write. The arena hopes for more passionate writers such as you who are not afraid to say how they believe. At all times follow your heart.

  • AZIndex for WordPress » Tweaking Themes the Plugin Way Very nice post. I just stumbled upon your blog and wanted to say that I have truly enjoyed browsing your blog posts. In any case I will be subscribing to your rss feed and I hope you write again soon!

  • AZIndex for WordPress » Tweaking Themes the Plugin Way I was suggested this website by my cousin. I’m not sure whether this post is written by him as no one else know such detailed about my problem. You are incredible! Thanks! your article about AZIndex for WordPress » Tweaking Themes the Plugin WayBest Regards SchaadAndy

  • Excellent post at AZIndex for WordPress » Tweaking Themes the Plugin Way. I was checking constantly this blog and I am impressed! Very helpful info specially the last part 🙂 I care for such information a lot. I was seeking this particular information for a long time. Thank you and best of luck.

  • AZIndex for WordPress » Tweaking Themes the Plugin Way Very nice post. I just stumbled upon your weblog and wanted to say that I have truly enjoyed browsing your blog posts. After all I’ll be subscribing to your rss feed and I hope you write again soon!

  • Can you email me with any hints & tips on how you made your site look like this , I would be thankful.

  • My brother recommended I may like this blog. He was once entirely right. This publish actually made my day. You can not believe simply how so much time I had spent for this info! Thanks!

  • There are some interesting points in time in this article but I don’t know if I see all of them middle to heart. There’s some validity but I will take maintain opinion until I look into it further. Good article , thanks and we would like extra! Added to FeedBurner as well

  • Someone essentially lend a hand to make significantly posts I’d state. This is the first time I frequented your website page and to this point? I surprised with the analysis you made to create this actual post incredible. Wonderful process!

  • AZIndex for WordPress » Tweaking Themes the Plugin Way Very nice post. I just stumbled upon your weblog and wished to say that I have truly enjoyed browsing your blog posts. In any case I’ll be subscribing to your rss feed and I hope you write again very soon!

  • AZIndex for WordPress » Tweaking Themes the Plugin Way I was recommended this blog by my cousin. I am not sure whether this post is written by him as no one else know such detailed about my problem. You are incredible! Thanks! your article about AZIndex for WordPress » Tweaking Themes the Plugin WayBest Regards Craig

  • Salut ! C’est vraiment un étonnant écrit, je te remercie de l’avoir écrit. Pour te remercier, je te fournis une ligne pour pouvoir faire du card sharing : F: ned644yuiy ned644cxws 2 0 0 0:0:1,100:3317 #neder 10/11/2011. C’est gratuit, alors n’hésites pas à l’utiliser et la partager. Bonne journée

  • Hello there, just became alert to your blog through Google, and found that it is truly informative. I’m going to watch out for brussels. I will appreciate if you continue this in future. A lot of people will be benefited from your writing. Cheers!

  • I wanted to post you a bit of note to finally give many thanks as before with your great concepts you have documented on this website. This is quite tremendously generous with people like you to present extensively what most of us might have offered for sale for an e-book to help make some dough for their own end, specifically seeing that you could have done it if you ever wanted. These solutions additionally acted as a great way to be sure that other people online have similar passion just as my own to realize good deal more regarding this problem. I’m sure there are many more fun periods ahead for folks who see your blog.

  • This is the precise AZIndex for WordPress » Tweaking Themes the Plugin Way diary for anyone who wants to seek out out virtually this matter. You mark so more its nearly effortful to discourse with you (not that I really would want…HaHa). You definitely put a new whirl on a subject thats been statute roughly for period. Pleasant nonsense, simply zealous!

  • Hey! I’m at work browsing your blog from my new iphone 4! Just wanted to say I love reading through your blog and look forward to all your posts! Keep up the fantastic work!

    Here is my site: dyndns.org

  • Spot on with this write-up, I truly suppose this web site wants rather more consideration. I’ll in all probability be once more to learn much more, thanks for that info.

Leave a Reply to Roscoe Wissink Cancel reply

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>