SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member Emphacy's Avatar
    Join Date
    Aug 2012
    Location
    Newcastle upon Tyne UK
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Using Checkboxes in the WordPress admin panel.

    Fellow PHP Guru's, I need your mad problem solving skillz...

    I'll cut straight to the point (this may be a long post). I'm attempting to control a category navigation bar for WordPress. This is controlled by a series of checkboxes (one for each category) in the admin settings area.
    Basically, the user selects the categories (via checkboxes) they want to show in the Navigation Bar.

    For each category in the $website_categories array I want it to create a checkbox - That part is done and works perfectly! YEY.

    Now for each checkbox selected I want it to add to the $options['category_navigation'] array key the ID of the category the checkbox corresponds to. If I where to print_r($options); it would look like this...

    Array([category_navigation] => 1 7 5 3 8 14) where each number is the category_id of each category.

    Here's the important part the code I've written:

    Code PHP:
    add_settings_field('category_navigation', 'Category Navigation:', 'category_navigation_setting', basename(__FILE__), 'main_settings');
     
    function category_navigation_setting()
    {
    	global $website_categories;
     
    	$options = get_option('website_options');
    	foreach ($website_categories as $category_id => $category_name )
    	{
    		echo "<div><input name='website_options[category_navigation]' type='checkbox' id='$category_id' value='$category_id' /> $category_name</div>";		
    	}      print_r($options);
    }


    Unfortunately with the code I have, it chooses the last checked checkbox in the foreach and puts the value for that in the $options['category_navigation'] key instead of all values of the boxes checked - This is obvious why, even to me. The problem I'm having is finding a way to simply append the value to the end of the string of values already stored in $options['category_navigation'].

    There are also other things to consider...
    a) It will only add it to the $options variable once I click 'Update Options' and post the form. So it means I have to click a checkbox and update one at a time.
    b) I have no idea how to get the state of each checkbox (true, false) from the $options['category_navigation'] key so that it stays that way when the user comes back to the admin area - At the moment all the checkboxes return to null even though the database was updated.

    If any of you guru's can help me out I'd hugely appreciate it! I'd like to finally get this theme ready so I can publish it! If you can't help me, then god behold I'll be stuck for days pondering the situation until I give up and scrap my newly-found career as a web developer.

    Please, if you need any more information just ask.
    Thanks! - Tom.

  2. #2
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,129
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    Well, first off, you are overwriting each value in the form submission, so you can solve that by using this HTML output instead of what you currently have
    PHP Code:
    echo "<div><input name='website_options[category_navigation][]' type='checkbox' id='$category_id' value='$category_id' /> $category_name</div>"
    This will return an array for website_options[category_navigation]. What I currently do not know is if you definitely want this as a string, or if you can keep the values as an array. If you need it as a string, just pass the array into
    PHP Code:
    $options['category_navigation'] = implode(' '$array); // $array being the posted value of website_options[category_navigation] 
    And if you want it get the string back into an array, you can use explode like so
    PHP Code:
    $array explode(' '$options['category_navigation']); 
    Hopefully this will get you to where you want to go.

  3. #3
    SitePoint Member Emphacy's Avatar
    Join Date
    Aug 2012
    Location
    Newcastle upon Tyne UK
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Amazing!

    Oddly enough I'd already tried that method and it didn't seem to work so I scrapped the idea of using an array... Maybe I had a semantic error somewhere. Anyway I must thank you because it worked perfectly!
    I did indeed need it as a string and each value separated by a comma as it's how WordPress lists the categories but I managed to get that working just fine!

    Now just to figure out a way to store the state of each checkbox... I do love coding, honest.
    Thanks again!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •