3

I'm trying to add a new footer widget area to my theme. I was able to add the new footer widget area and it works correctly. But I would like to limit what can be done with the widget area by the end user, right now the user can add any widget that they wish to the new footer widget area.

I would like to limit the widget area to a line of text only such as a copyright statement. Is this possible with widget areas? How should I go about limiting it to just a line of text?

2 Answers 2

8

Widget areas are the wrong tools for what you need. They are built to offer a choice. Breaking that would be very difficult … and hard to understand for the user.

Alternative: Use a custom setting, for example in wp-admin/options-general.php where the tagline and the site title is. There is even a hook for such additions:

do_settings_sections('general');

First, wait for the action admin_init, and then register a new settings field:

add_filter( 'admin_init', 'wpse_76943_register_setting' );

function wpse_76943_register_setting()
{
    register_setting( 'general', 'footer_text', 'trim' );

    add_settings_field(
        'footer_text',
        'Footer text',
        'wpse_76943_print_input',
        'general',
        'default',
        array ( 'label_for' => 'footer_text' )
    );
}

Easy. Now we need a function to print that field. Use a classic text input field with a class large-text to make it wide enough:

function wpse_76943_print_input()
{
    $value = esc_attr( get_option( 'footer_text' ) );
    print "<input type='text' value='$value' name='footer_text' id='footer_text'
    class='large-text' />
    <span class='description'>Text for the footer.</span>";
}

enter image description here

That’s all!

To use the footer text in your theme just check if there is a value, then print it:

if ( $footer_text = get_option( 'footer_text' ) )
    print "<p>$footer_text</p>";

I have written a plugin doing almost the same thing, it is just more flexible: Public Contact Data (Download as ZIP archive).

4
  • Great answer. One other thing, you could also use the wp_footer action hook to add it to the theme.
    – shea
    Dec 22, 2012 at 3:41
  • @bungeshea I tried that in Twenty Eleven: the result was not very convincing because the stylesheet is not prepared for that. You have to to change the theme files.
    – fuxia
    Dec 22, 2012 at 3:43
  • It works in some themes, other themes … not so much.
    – shea
    Dec 22, 2012 at 3:44
  • Thanks for taking the time to explain. This is exactly what I was looking for. Dec 22, 2012 at 5:21
-2

I think as usually you do

Hope it helps

1
  • 2
    This answer has nothing to do with the question! Read the question properly, and then read it again
    – shea
    Dec 22, 2012 at 3:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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