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!
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.