0

I'm trying to add some functionality to my "Settings" page so that if a new season is added in a custom post's metabox, a new field is generated in Settings to allow users to add a title and description to that Season. This part is working, but when I try to save my information, the information from one season is saved across all fields, since my fields correspond to the same helper function

Rather than hardcoding separate input fields seasons 1, 2, 3, etc. and hardcoding the proper helper function for each, I'm trying to do this in a loop:

for($k=1; $k<=$numseasons; $k++){
        $funcTitle = get_option('show_season_title_$k');
        $funcDesc = get_option('show_season_desc_$k');


        echo "<label for='show_season_title_$k'>Season: $k</label><br>
        <input type='text' id='show_season_title_$k' name='show_season_title_$k' value='$funcTitle'><br>

        <label for='show_season_desc_$k'> Season Description:</label><br>
        <textarea id='show_season_desc_$k' name='show_season_desc_$k'>$funcDesc</textarea>";
    }

At the top of my file, I have:

add_option( 'show_season_title_1', '');
add_option( 'show_season_desc_1', '');
add_option( 'show_season_title_2', '');
add_option( 'show_season_desc_2', '');

and:

register_setting( 'showsettings', 'show_season_title_1' ); 
register_setting( 'showsettings', 'show_season_desc_1' ); 
register_setting( 'showsettings', 'show_season_title_2' ); 
register_setting( 'showsettings', 'show_season_desc_2' );

However, whenever I save my settings with text in the input fields, the text doesn't save (upon inspection, the "value" field and textarea fields just don't have a value). Also, if I try to echo $funcTitle and $funcDesc within my loop, I don't get anything, so I'm wondering if the issue is with calling the function with the $k variable in it.

If anyone has any suggestions at all, I would be so grateful! Thank you!

0
get_option('show_season_title_' . $k);

I think, I don't have a way to test that right now but it should work.

  • Ah, that makes complete sense....let me give it a try.. – bjorkland Jun 28 '17 at 17:30
  • That fixed it!!! Was just staring at it too long to realize I wasn't concatenating the string properly. Thank you so, so much! Will accept this as the answer when it allows me :) – bjorkland Jun 28 '17 at 17:31

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.