Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm creating my first theme (actually a child theme of "twenty eleven") and am not sure how to best approach theme customization. Say that I want to have a RSS link in the sidebar shown or hidden based on some configuration, what's the best way to approach it?

The most basic approach would be to go directly into the theme and comment / uncomment the related piece of HTML every time I want to change it but I think it would be best if the administrator of the site could just tick a checkbox somewhere and the theme would respond accordingly.

How to code it?

share|improve this question

1 Answer 1

up vote 2 down vote accepted

Your specific example can be addressed by making sure your theme "widgetized" (or supports sidebar widgets). If the example you described is what you're really trying to accomplish, I'd recommend taking that route. (I'll use the term sidebar, but really, a "sidebar" can be placed anywhere in your site... the header, footer, or even in the body).

1) First create a file named sidebar-rss.php

2) Place the code you'd like to include for the customization into that file:

<!-- begin custom sidebar -->
    <?php if (!function_exists('dynamic_sidebar') || !dynamic_sidebar('RSS Widget')) :
    ?>
        (place any code or content here that should show up if no widget is active)
    <?php endif; ?>
<!-- end custom sidebar -->

3) Tell WordPress about your widget area. Put this code in your functions.php file:

if (function_exists('register_sidebar')) {
    register_sidebar(array(
        'name'=>'RSS Widget',
        'before_widget' => '<div id="%1$s" class="widget %2$s">',
        'after_widget' => '</div>',
        'before_title' => '<h4 class="widgettitle">',
        'after_title' => '</h4>',
    ));
}

4) Now you should be ready to go. Include sidebar-rss.php anywhere in your theme by using this code:

<?php get_sidebar("rss"); ?>

5) Now the user can drag and drop the RSS widget from Appearance > Widgets. (Or any widget for that matter).

Now... let's talk about a situation where you don't have a widget available, but rather, you want to hard code some functionality into your theme and allow them to maybe click a checkbox in your own custom admin interface to turn it on and off. It'll take more work, but it's actually still pretty easy.

In general, you're going to create a new admin menu in the WordPress admin interface. You'll build all of that code in your functions.php page. Then you'll create the HTML for that new menu page (with your own text and controls like textboxes, checkboxes, dropdown menus, or whatever you want). You'll save the user's "preferences" or "settings" for your theme as a custom option (WordPress gives you access to the wp_options table in order to save your own custom data). Then in your theme files, you'll get those options and render your content / functionality based on what is set in there.

All that is just a high level walkthrough. You can find the actual code and proper steps on the Codex under Administration Menus.

Have fun!

share|improve this answer
    
Wow, excellent description, thank you. Why can't I upvote twice? :) –  Borek Feb 19 '12 at 0:21

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.