2

I'm not sure that my title accurately explains what I'm trying to do, but it's the best way I could think of to explain what I'm attempting to do.

I'm writing my first Wordpress plugin, and while it's been a huge learning experience so far, I've found myself stuck.

Basically, the plugin adds a shortcode, [routes_and_schedules_accordion] that generates a jQuery accordion that's a more fleshed-out version of this:

<ul class="accordion">
  <li class="panel">
    <div class="title">
      <h5>Route 1 - Market Street</h5>
    </div>
    <div class="content">
      <a href="http://www.example.com/Route1.pdf">Current Schedule</a>
    </div>
  </li>
</ul>

After a lot of reading, I've created an options page with the Settings API that lets me change the name of the route and the URL to its schedule (we're a small bus agency):

<?php $options = get_option('routes_and_schedules_accordion_settings') ?>

<ul class="accordion">
  <li class="panel">
    <div class="title">
      <h5><?php echo $options['route_name'] ?></h5>
    </div>
    <div class="content">
      <a href="<?php echo $options['route_current_schedule_url'] ?>">Current Schedule</a>
    </div>
  </li>
</ul>

What I'm really struggling with is making it so that new routes can be added. The goal is for the options page to have a way of adding and removing routes, and for the shortcode to spit out something like this:

<?php $options = get_option('routes_and_schedules_accordion_settings') ?>

<ul class="accordion">
  <li class="panel">
    <div class="title">
      <h5><?php echo $options['route1_name'] ?></h5>
    </div>
    <div class="content">
      <a href="<?php echo $options['route1_current_schedule_url'] ?>">Current Schedule</a>
    </div>
  </li>
  <li class="panel">
    <div class="title">
      <h5><?php echo $options['route2_name'] ?></h5>
    </div>
    <div class="content">
      <a href="<?php echo $options['route2_current_schedule_url'] ?>">Current Schedule</a>
    </div>
  </li>
</ul>

I guess I could just make new fields each time the number of routes changes and update the plugin every single time, but that would be a lot of work, and I know that there has to be better way.

If someone could point me in the right direction, I would greatly appreciate it!

1
  • Are you looking for repeatable fields? Examples: WPSE and Gist.
    – brasofilo
    Jan 19, 2013 at 4:06

1 Answer 1

3

If you want to do this with options, save the routes as an array rather than individual options, or save the number of routes in an option so you can dynamically create and fetch them by building the option names based on the number you save.

However, rather than saving this stuff in options, I would make a route custom post type. Route data would be post meta data, schedules would be an attachment, and you could use custom taxonomies to further group/filter routes.

3
  • +1. Options should be discrete. If a content type is intended to consist of an arbitrary number of "units" of that content, then it would be better served as a custom post type. Jan 19, 2013 at 2:55
  • @ChipBennett If I were to make a route custom post type, how would I go about putting the data from all of them into a single page as an accordion? Would I be able to make a shortcode that did it? I'm considering the "save the number of routes in an option" idea, and I think I like it the most. How would I implement that?
    – drewbug
    Jan 19, 2013 at 15:27
  • @DrewBuglione you could use either archive-route.php that WordPress would use to output the archive index page for the route post-type, or else create a custom page template on which you would output your own custom query loop. Jan 19, 2013 at 15:51

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.