I’m trying to build an interface so that two custom fields for a custom post type can be edited on the front end with checkboxes. I’d like to display empty checkboxes if neither of those the fields have values, and allow a user to check one or both boxes, and have the corresponding fields be set to true (or just have some value, I suppose – not sure if a postmeta can have a boolean value) (preferably via AJAX).
My first thought is to use Scribu’s Front End Editor plugin, although it obviously doesn’t solve this use case without some tweaking. However, I figured I’d ask to see if anyone else has done something like this before, or has suggestions on how specifically to implement the FEE plugin to do this (or if I should look elsewhere).
Edit to expand: this is going on a page listing multiple posts, so I’m trying to figure out a way to make this work for more than just one post on a page. Here’s what the page looks like right now:
And here’s the markup for that table:
<table>
<thead>
<tr>
<th>Date/Time</th>
<th>Job ID</th>
<th>Customer Name</th>
<th>File Size</th>
<th>Details</th>
<th>Download</th>
<th>Scheduled</th>
<th>Completed</th>
</tr>
</thead>
<tr class="job">
<td>8/8/12 1:12pm</td>
<td>28</td>
<td>Selina Kyle</td>
<td>filesize</td>
<td><a href="http://example.com/jobs/selina-kyle/" target="_blank">Details</a></td>
<td><a href="http://example.com/wp-content/uploads/gravity_forms/1-884e859c1f31e83338dbdaefd3bb2853/2012/08/CC-Content-Checklist.docx">Download File</a></td>
<td><input type="checkbox" name="28-scheduled" /></td>
<td><input type="checkbox" name="28-completed" /></td>
<td><input type="hidden" name="post_id" value="28" /></td>
<td><span class="response"></span></td>
</tr>
<tr class="job">
<td>8/8/12 1:06pm</td>
<td>26</td>
<td>Jim Gordon</td>
<td>filesize</td>
<td><a href="http://example.com/jobs/jim-gordon/" target="_blank">Details</a></td>
<td><a href="http://example.com/wp-content/uploads/gravity_forms/1-884e859c1f31e83338dbdaefd3bb2853/2012/08/Copy-Corner-Proposal-7-17-121.pdf">Download File</a></td>
<td><input type="checkbox" name="26-scheduled" /></td>
<td><input type="checkbox" name="26-completed" /></td>
<td><input type="hidden" name="post_id" value="26" /></td>
<td><span class="response"></span></td>
</tr>
<tr class="job">
<td>8/8/12 1:05pm</td>
<td>25</td>
<td>Lucius Fox</td>
<td>filesize</td>
<td><a href="http://example.com/jobs/lucius-fox-2/" target="_blank">Details</a></td>
<td><a href="http://example.com/wp-content/uploads/gravity_forms/1-884e859c1f31e83338dbdaefd3bb2853/2012/08/Process-and-deadlines-Copy-Corner2.pdf">Download File</a></td>
<td><input type="checkbox" name="25-scheduled" /></td>
<td><input type="checkbox" name="25-completed" /></td>
<td><input type="hidden" name="post_id" value="25" /></td>
<td><span class="response"></span></td>
</tr>
<tr class="job">
<td>8/8/12 12:51pm</td>
<td>22</td>
<td>Bruce Wayne</td>
<td>filesize</td>
<td><a href="http://example.com/jobs/bruce-wayne-2/" target="_blank">Details</a></td>
<td><a href="http://example.com/wp-content/uploads/gravity_forms/1-884e859c1f31e83338dbdaefd3bb2853/2012/08/CC-Wireframes1.pdf">Download File</a></td>
<td><input type="checkbox" name="22-scheduled" /></td>
<td><input type="checkbox" name="22-completed" /></td>
<td><input type="hidden" name="post_id" value="22" /></td>
<td><span class="response"></span></td>
</tr>
</table>
My gut instinct would be to roll my own AJAX callback. You obviously know the post ID (since you have it on the front end) and the names of the fields, just post that back to WP using AJAX and programmatically set the values there.
An untested example (props to Kailey Lampert):
Update Based on Markup
Now that we know a little more what you’re doing, we can be a bit more specific on the JS code you need to use.
Basically, what I recommend you do is set up a custom class and some data elements inside your checkboxes. So where you have things like:
Become things like:
Now, you can set up a jQuery event listener on the entire table that looks at each input individually:
This event listener will wait for the
change
event of all<input>
elements that have theajax-checkbox
class. It will then extract the values you’ve hard-coded (PHP-generated, I hope) from thedata-
attributes so you can send them in your AJAX request. You’ll have to merge this in with the examples above or the custom code you’re already written to dispatch the messages.