WordPress.org

Making WordPress.org


Ignore:
Timestamp:
02/12/2018 10:23:05 PM (4 years ago)
Author:
iandunn
Message:

WordCamp Helpers: Add helper to generate a Select2 dropdown of WordCamps.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordcamp.org/public_html/wp-content/mu-plugins/helpers-wcpt.php

    r6599 r6609  
    77 */
    88
     9
     10/**
     11 * Retrieve `wordcamp` posts and their metadata.
     12 *
     13 * @param array $args Optional. Extra arguments to pass to `get_posts()`.
     14 *
     15 * @return array
     16 */
     17function get_wordcamps( $args = array() ) {
     18    $args = wp_parse_args( $args, array(
     19        'post_type'   => WCPT_POST_TYPE_ID,
     20        'post_status' => 'any',
     21        'orderby'     => 'ID',
     22        'numberposts' => -1,
     23        'perm'        => 'readable',
     24    ) );
     25
     26    $wordcamps = get_posts( $args );
     27
     28    foreach ( $wordcamps as &$wordcamp ) {
     29        $wordcamp->meta = get_post_custom( $wordcamp->ID );
     30    }
     31
     32    return $wordcamps;
     33}
    934
    1035/**
     
    175200    return absint( $duration_days );
    176201}
     202
     203/**
     204 * Get a <select> dropdown of `wordcamp` posts with a select2 UI.
     205 *
     206 * The calling plugin is responsible for validating and processing the form, this just outputs a single field.
     207 *
     208 * @param string $name          The `name` attribute for the `select` element
     209 * @param array  $query_options Optional. Extra arguments to pass to `get_posts()`. Defaults to the values in `get_wordcamps()`.
     210 *
     211 * @return string The HTML for the <select> list.
     212 */
     213function get_wordcamp_dropdown( $name = 'wordcamp_id', $query_options = array() ) {
     214    $wordcamps = get_wordcamps( $query_options );
     215
     216    wp_enqueue_script( 'select2' );
     217    wp_enqueue_style(  'select2' );
     218
     219    ob_start();
     220
     221    ?>
     222
     223    <select name="<?php echo esc_attr( $name ); ?>" class="select2">
     224        <option value=""><?php _e( 'Select a WordCamp', 'wordcamporg' ); ?></option>
     225        <option value=""></option>
     226
     227        <?php foreach ( $wordcamps as $wordcamp ) : ?>
     228            <option value="<?php echo esc_attr( $wordcamp->ID ); ?>">
     229                <?php
     230
     231                echo esc_html( $wordcamp->post_title );
     232                if ( ! empty( $wordcamp->meta['Start Date (YYYY-mm-dd)'][0] ) ) {
     233                    echo ' ' . esc_html( date( 'Y', $wordcamp->meta['Start Date (YYYY-mm-dd)'][0] ) );
     234                }
     235
     236                ?>
     237            </option>
     238        <?php endforeach; ?>
     239    </select>
     240
     241    <script>
     242        jQuery( document ).ready( function() {
     243            jQuery( '.select2' ).select2();
     244        } );
     245    </script>
     246
     247    <?php
     248
     249    return ob_get_clean();
     250}
Note: See TracChangeset for help on using the changeset viewer.