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?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
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>";
}
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).
Method 2
I think as usually you do
- Javascript on frontend using some easy character counter limiter (Google first result)
- PHP string length checking on backend (PHP manual)
Hope it helps
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0