Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save og-shawn-crigger/5010503 to your computer and use it in GitHub Desktop.

Select an option

Save og-shawn-crigger/5010503 to your computer and use it in GitHub Desktop.

Revisions

  1. @helen helen revised this gist Jan 11, 2012. 1 changed file with 4 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions repeatable-fields-metabox.php
    Original file line number Diff line number Diff line change
    @@ -139,15 +139,15 @@ function hhs_repeatable_meta_box_save($post_id) {
    $new = array();
    $options = hhs_get_sample_options();

    $names = stripslashes( strip_tags( $_POST['name'] ) );
    $names = $_POST['name'];
    $selects = $_POST['select'];
    $urls = stripslashes( $_POST['url'] ); // and however you want to sanitize
    $urls = $_POST['url'];

    $count = count( $names );

    for ( $i = 0; $i < $count; $i++ ) {
    if ( $names[$i] != '' ) :
    $new[$i]['name'] = $names[$i];
    $new[$i]['name'] = stripslashes( strip_tags( $names[$i] ) );

    if ( in_array( $selects[$i], $options ) )
    $new[$i]['select'] = $selects[$i];
    @@ -157,7 +157,7 @@ function hhs_repeatable_meta_box_save($post_id) {
    if ( $urls[$i] == 'http://' )
    $new[$i]['url'] = '';
    else
    $new[$i]['url'] = $urls[$i];
    $new[$i]['url'] = stripslashes( $urls[$i] ); // and however you want to sanitize
    endif;
    }

  2. @helen helen revised this gist Jan 11, 2012. 1 changed file with 4 additions and 5 deletions.
    9 changes: 4 additions & 5 deletions repeatable-fields-metabox.php
    Original file line number Diff line number Diff line change
    @@ -123,7 +123,6 @@ function hhs_repeatable_meta_box_display() {
    <?php
    }


    add_action('save_post', 'hhs_repeatable_meta_box_save');
    function hhs_repeatable_meta_box_save($post_id) {
    if ( ! isset( $_POST['hhs_repeatable_meta_box_nonce'] ) ||
    @@ -140,15 +139,15 @@ function hhs_repeatable_meta_box_save($post_id) {
    $new = array();
    $options = hhs_get_sample_options();

    $names = $_POST['name'];
    $names = stripslashes( strip_tags( $_POST['name'] ) );
    $selects = $_POST['select'];
    $urls = $_POST['url'];
    $urls = stripslashes( $_POST['url'] ); // and however you want to sanitize

    $count = count( $names );

    for ( $i = 0; $i < $count; $i++ ) {
    if ( $names[$i] != '' ) :
    $new[$i]['name'] = stripslashes( strip_tags( $names[$i] ) );
    $new[$i]['name'] = $names[$i];

    if ( in_array( $selects[$i], $options ) )
    $new[$i]['select'] = $selects[$i];
    @@ -158,7 +157,7 @@ function hhs_repeatable_meta_box_save($post_id) {
    if ( $urls[$i] == 'http://' )
    $new[$i]['url'] = '';
    else
    $new[$i]['url'] = stripslashes( $urls[$i] ); // and however you want to sanitize
    $new[$i]['url'] = $urls[$i];
    endif;
    }

  3. @helen helen revised this gist Jan 11, 2012. 1 changed file with 25 additions and 15 deletions.
    40 changes: 25 additions & 15 deletions repeatable-fields-metabox.php
    Original file line number Diff line number Diff line change
    @@ -4,8 +4,19 @@
    * Author: Helen Hou-Sandi
    *
    * From a bespoke system, so currently not modular - will fix soon
    * Note that this particular metadata is saved as one multilevel array (serialized)
    * Note that this particular metadata is saved as one multidimensional array (serialized)
    */

    function hhs_get_sample_options() {
    $options = array (
    'Option 1' => 'option1',
    'Option 2' => 'option2',
    'Option 3' => 'option3',
    'Option 4' => 'option4',
    );

    return $options;
    }

    add_action('admin_init', 'hhs_add_meta_boxes', 1);
    function hhs_add_meta_boxes() {
    @@ -14,9 +25,10 @@ function hhs_add_meta_boxes() {

    function hhs_repeatable_meta_box_display() {
    global $post;

    $repeatable_fields = get_post_meta($post->ID, 'repeatable_fields', true);

    $options = hhs_get_sample_options();

    wp_nonce_field( 'hhs_repeatable_meta_box_nonce', 'hhs_repeatable_meta_box_nonce' );
    ?>
    <script type="text/javascript">
    @@ -47,19 +59,12 @@ function hhs_repeatable_meta_box_display() {
    <tbody>
    <?php

    $options = array (
    'Option 1' => 'option1',
    'Option 2' => 'option2',
    'Option 3' => 'option3',
    'Option 4' => 'option4',
    );

    if ( $repeatable_fields ) :

    foreach ( $repeatable_fields as $field ) {
    ?>
    <tr>
    <td><input type="text" class="widefat" name="name[]" value="<?php if($field['name'] != '') echo $field['name']; ?>" /></td>
    <td><input type="text" class="widefat" name="name[]" value="<?php if($field['name'] != '') echo esc_attr( $field['name'] ); ?>" /></td>

    <td>
    <select name="select[]">
    @@ -69,7 +74,7 @@ function hhs_repeatable_meta_box_display() {
    </select>
    </td>

    <td><input type="text" class="widefat" name="url[]" value="<?php if ($field['url'] != '') echo $field['url']; else echo 'http://'; ?>" /></td>
    <td><input type="text" class="widefat" name="url[]" value="<?php if ($field['url'] != '') echo esc_attr( $field['url'] ); else echo 'http://'; ?>" /></td>

    <td><a class="button remove-row" href="#">Remove</a></td>
    </tr>
    @@ -133,6 +138,7 @@ function hhs_repeatable_meta_box_save($post_id) {

    $old = get_post_meta($post_id, 'repeatable_fields', true);
    $new = array();
    $options = hhs_get_sample_options();

    $names = $_POST['name'];
    $selects = $_POST['select'];
    @@ -142,13 +148,17 @@ function hhs_repeatable_meta_box_save($post_id) {

    for ( $i = 0; $i < $count; $i++ ) {
    if ( $names[$i] != '' ) :
    $new[$i]['name'] = $names[$i];
    $new[$i]['select'] = $selects[$i];
    $new[$i]['name'] = stripslashes( strip_tags( $names[$i] ) );

    if ( in_array( $selects[$i], $options ) )
    $new[$i]['select'] = $selects[$i];
    else
    $new[$i]['select'] = '';

    if ( $urls[$i] == 'http://' )
    $new[$i]['url'] = '';
    else
    $new[$i]['url'] = $urls[$i];
    $new[$i]['url'] = stripslashes( $urls[$i] ); // and however you want to sanitize
    endif;
    }

  4. @helen helen created this gist Jan 11, 2012.
    160 changes: 160 additions & 0 deletions repeatable-fields-metabox.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,160 @@
    <?
    /**
    * Repeatable Custom Fields in a Metabox
    * Author: Helen Hou-Sandi
    *
    * From a bespoke system, so currently not modular - will fix soon
    * Note that this particular metadata is saved as one multilevel array (serialized)
    */

    add_action('admin_init', 'hhs_add_meta_boxes', 1);
    function hhs_add_meta_boxes() {
    add_meta_box( 'repeatable-fields', 'Repeatable Fields', 'hhs_repeatable_meta_box_display', 'post', 'normal', 'default');
    }

    function hhs_repeatable_meta_box_display() {
    global $post;

    $repeatable_fields = get_post_meta($post->ID, 'repeatable_fields', true);

    wp_nonce_field( 'hhs_repeatable_meta_box_nonce', 'hhs_repeatable_meta_box_nonce' );
    ?>
    <script type="text/javascript">
    jQuery(document).ready(function( $ ){
    $( '#add-row' ).on('click', function() {
    var row = $( '.empty-row.screen-reader-text' ).clone(true);
    row.removeClass( 'empty-row screen-reader-text' );
    row.insertBefore( '#repeatable-fieldset-one tbody>tr:last' );
    return false;
    });

    $( '.remove-row' ).on('click', function() {
    $(this).parents('tr').remove();
    return false;
    });
    });
    </script>

    <table id="repeatable-fieldset-one" width="100%">
    <thead>
    <tr>
    <th width="40%">Name</th>
    <th width="12%">Select</th>
    <th width="40%">URL</th>
    <th width="8%"></th>
    </tr>
    </thead>
    <tbody>
    <?php

    $options = array (
    'Option 1' => 'option1',
    'Option 2' => 'option2',
    'Option 3' => 'option3',
    'Option 4' => 'option4',
    );

    if ( $repeatable_fields ) :

    foreach ( $repeatable_fields as $field ) {
    ?>
    <tr>
    <td><input type="text" class="widefat" name="name[]" value="<?php if($field['name'] != '') echo $field['name']; ?>" /></td>

    <td>
    <select name="select[]">
    <?php foreach ( $options as $label => $value ) : ?>
    <option value="<?php echo $value; ?>"<?php selected( $field['select'], $value ); ?>><?php echo $label; ?></option>
    <?php endforeach; ?>
    </select>
    </td>

    <td><input type="text" class="widefat" name="url[]" value="<?php if ($field['url'] != '') echo $field['url']; else echo 'http://'; ?>" /></td>

    <td><a class="button remove-row" href="#">Remove</a></td>
    </tr>
    <?php
    }
    else :
    // show a blank one
    ?>
    <tr>
    <td><input type="text" class="widefat" name="name[]" /></td>

    <td>
    <select name="select[]">
    <?php foreach ( $options as $label => $value ) : ?>
    <option value="<?php echo $value; ?>"><?php echo $label; ?></option>
    <?php endforeach; ?>
    </select>
    </td>

    <td><input type="text" class="widefat" name="url[]" value="http://" /></td>

    <td><a class="button remove-row" href="#">Remove</a></td>
    </tr>
    <?php endif; ?>

    <!-- empty hidden one for jQuery -->
    <tr class="empty-row screen-reader-text">
    <td><input type="text" class="widefat" name="name[]" /></td>

    <td>
    <select name="select[]">
    <?php foreach ( $options as $label => $value ) : ?>
    <option value="<?php echo $value; ?>"><?php echo $label; ?></option>
    <?php endforeach; ?>
    </select>
    </td>

    <td><input type="text" class="widefat" name="url[]" value="http://" /></td>

    <td><a class="button remove-row" href="#">Remove</a></td>
    </tr>
    </tbody>
    </table>

    <p><a id="add-row" class="button" href="#">Add another</a></p>
    <?php
    }


    add_action('save_post', 'hhs_repeatable_meta_box_save');
    function hhs_repeatable_meta_box_save($post_id) {
    if ( ! isset( $_POST['hhs_repeatable_meta_box_nonce'] ) ||
    ! wp_verify_nonce( $_POST['hhs_repeatable_meta_box_nonce'], 'hhs_repeatable_meta_box_nonce' ) )
    return;

    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
    return;

    if (!current_user_can('edit_post', $post_id))
    return;

    $old = get_post_meta($post_id, 'repeatable_fields', true);
    $new = array();

    $names = $_POST['name'];
    $selects = $_POST['select'];
    $urls = $_POST['url'];

    $count = count( $names );

    for ( $i = 0; $i < $count; $i++ ) {
    if ( $names[$i] != '' ) :
    $new[$i]['name'] = $names[$i];
    $new[$i]['select'] = $selects[$i];

    if ( $urls[$i] == 'http://' )
    $new[$i]['url'] = '';
    else
    $new[$i]['url'] = $urls[$i];
    endif;
    }

    if ( !empty( $new ) && $new != $old )
    update_post_meta( $post_id, 'repeatable_fields', $new );
    elseif ( empty($new) && $old )
    delete_post_meta( $post_id, 'repeatable_fields', $old );
    }
    ?>