0

I'm working on Settings API recently. Now I can add my theme options item in admin menu.I can register my settings and create my section and fields. And I think I can get the options using get_option(). But the problem is I can't find my way to implement those changes in my template file. For Example I'll create a section and field for the site owner to give her the ability to change the footer text to whatever she want. Settings and textarea field appears correctly, but how can I tell WP to change that text as the site owner edit that text? what changes do I need to my theme's footer code to achieve this goal?

I'll appreciate if you can help me on this.

Edit: My Code in functions.php :

function m3n_admin_menu_init() {
    add_menu_page( 'M3N Theme Options', 'M3N Settings', 'manage_options', 'm3n-theme-options', 'm3n_menu_init' );
}
add_action( 'admin_menu', 'm3n_admin_menu_init' );

function m3n_menu_init() {
    echo '<div class="wrap">';
    echo '<h2>M3N Theme Settings</h2>';
      echo '<form action="options.php" method="POST">';
      settings_fields( 'm3n_settings_group' );
      do_settings_sections( 'm3n-theme-options' );
      submit_button();
      echo '</form>';
    echo '</div>';
}


function m3n_settings_init() {
    register_setting( 'm3n_settings_group', 'm3n_settings_name' );
    add_settings_section( 'section-one', 'General Settings', 'm3n_section_init', 'm3n-theme-options' );
    add_settings_field( 'field-one', 'Footer Settings', 'm3n_footer_field_init', 'm3n-theme-options', 'section-one' );
}
add_action( 'admin_init', 'm3n_settings_init' );

function m3n_section_init() {
    echo '<p>Enter your text for Footer</p>';
}
function m3n_footer_field_init() {
    $options = get_option( 'm3n_settings_name' );
    echo '<textarea name="field-one" id="field-one" rows="3" value="' . $options . '"></textarea>';
}

Code inside footer.php :

<p><?php echo get_option( 'm3n_settings_name' ); ?></p>
  • 1
    Why don’t you use a widget? – fuxia Dec 15 '14 at 12:56
  • because I want to have this ability in my theme out of the box. Also I want to learn this way. Thanks. – novice_wp Dec 15 '14 at 13:44
0

So you have created a custom option using the Settings API.

Lets say you have created an option called footer-text you can display it in your theme using <?php echo get_option( 'footer-text' ); ?>.

Essentially you are echoing the result that is stored in the wp_options table for the option called footer-text.

It is advised to include some of the code you have done already done, this shows you have attempted something and it makes it easier to answer your question.

  • Thanks Stemie. I included my code above. Could you please help me to complete the last function. I have problem with that.I mean m3n_footer_field_init() function which renders my actual settings field. – novice_wp Dec 15 '14 at 16:23
  • Have you tried <?php echo get_option( 'field-one' ); ?> in your footer.php after adding the text in the actual options section? Just as an aside this is a great plugin for options pages wordpress.org/plugins/options-framework I generally use it as it's more convenient than a custom setup. – stemie Dec 15 '14 at 16:45
  • Thanks again, But I have problem at the start, even before I want to go to footer.php. Look at m3n_footer_field_init() function that render the input fields. I want to have a textarea there, which admin can enter their text for show in footer. After defining the $options variable, what should I write for my textarea input(name, id, value)? sorry for this kind of beginner questions but I'm novice in WP and I can't find my way through this. thanks in advance. – novice_wp Dec 15 '14 at 19:13
  • My codes had a major problem. I didn't write the form tag itself. oops... I add that and I think now everything is fine but still it doesn't work. I include my codes in functions.php and footer.php above. Please Let me know if there are any problem in it (obviously, it is). Thanks. – novice_wp Dec 16 '14 at 12:24
  • Sorry, my mistake!!! the $option_name in register_setting function should match the option ID, but those two, didn't match in my codes. so that didn't work for me. I changed $option_name argument in my register_setting & corresponding arguments in get_option, etc, and everything works just fine. Thanks. – novice_wp Dec 16 '14 at 15:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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