简体   繁体   中英

Pre-populate and disable select option based on choice in another select box in Admin centre

I am developing an events management plugin for a project I'm working on, and I've come across a problem I don't really know where to start with!

Basically, I have two option select dropdown meta-fields, one which looks up a CPT of Events and populates the list with posts of that type, using the following code;

<select name="photo_event" id="photo_event"/>
        <option value="none">--- Select Event ---</option>
        <?php
            global $post;
            $args = array( 'post_type' => 'event' );
            $posts = get_posts( $args );
            foreach($posts as $post) : setup_postdata($post); ?>
            <option value="<?php echo $post->ID; ?>"> <?php the_title(); ?> </option>
            <?php endforeach; ?>

        </select>

And the other looks up a Venues category and populates the list with sub-categories (the actual venues) using the following;

<select name="photo_venue" id="photo_venue"/>
        <option value="none">--- Select Venue ---</option>
        <?php 
            $params = array(
            'type'           => 'post', 
            'child_of'   => 3,
            'hide_empty'     => 0
            );
            $categories=  get_categories($params); 
            foreach ($categories as $category) {
            $option = '<option value="'.$category->category_nicename.'">';
            $option .= $category->cat_name;
            $option .= '</option>';
            echo $option; }
        ?>  
        </select>

The CPT these meta-fields are attached to is to post entity's related to either an event or a venue (such as photos, promo material, recorded mixes, etc).

Now, the problem: The user may wish to attach the entity to a venue, in which case, they wouldn't select an event, and would just leave it at the default " --- Select Event -- " which returns a value of none in the field photo_event . This, is fine.

However, as each event is held in only ONE venue, I would like to make it so if the user selects an event from the list of event posts, the Venue option select box would default to the venue that event was held in (by looking up the post information for that event, and selecting the appropriate venue from the value held in event_venue for that specific post (the meta-fields for event_venue also look-up the same category and sub-categories as photo_venue so there is complete continuity across the board [so that filtering by venue throws no errors]) and either removes all other options (so the user can't select a different venue than the one the event was held in), or disables the box completely and just fills photo_venue with the correct venue before saving.

How to solve this?

This is called chained selections , and you will need Ajax and jQuery to make it work.

With jQuery, you will "listen" to changes in Photo Event dropdown. Each change dispatches an Ajax call that executes the PHP get_categories command for the Photo Venue and the response is processed by jQuery rendering a new dropdown for the Venue.

I'd suggest making a new plugin just to make this part work, and merge it with the main plugin once it's ok. Use this Answer as starting point.

You'll find other resources in this search query at WordPress Answers .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM