Skip to content

Commit e97e299

Browse files
author
ThemeBoy
committed
Add no content option to display dropdown only
1 parent 664bc2b commit e97e299

File tree

2 files changed

+49
-25
lines changed

2 files changed

+49
-25
lines changed

ajax-dropdowns.php

+43-25
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ public static function posts_meta_box( $post, $args ) {
197197
?>
198198
<optgroup label="<?php echo $object->labels->name; ?>">
199199
<?php
200-
printf( '<option value="%s" data-post-type="%s">%s</option>', 0, $object->labels->singular_name, sprintf( __( '&mdash; Add all %1$s (%2$s) &mdash;', 'ajaxd' ), $object->labels->name, sizeof( $posts ) ) );
200+
printf( '<option value="%s" data-post-type="%s">%s</option>', 0, $object->labels->singular_name, sprintf( __( '&mdash; Add All %1$s (%2$s) &mdash;', 'ajaxd' ), $object->labels->name, sizeof( $posts ) ) );
201201
foreach ( $posts as $post ):
202202
printf( '<option value="%s" data-post-type="%s">%s</option>', $post->ID, $object->labels->singular_name, $post->post_title );
203203
endforeach;
@@ -264,6 +264,7 @@ public static function shortcode_meta_box( $post ) {
264264
public static function method_meta_box( $post, $args ) {
265265
wp_nonce_field( 'ajaxd_save_data', 'ajaxd_meta_nonce' );
266266
$method = get_post_meta( $post->ID, 'ajaxd_method', true );
267+
$no_content = get_post_meta( $post->ID, 'ajaxd_no_content', true ) == 'yes' ? true : false;
267268
$method_options = array( 'ajax' => __( 'Ajax', 'ajaxd' ), 'inline' => __( 'Inline', 'ajaxd' ), 'parameter' => __( 'URL Parameter', 'ajaxd' ), 'redirect' => __( 'Redirect', 'ajaxd' ) );
268269
?>
269270
<p class="howto">
@@ -278,6 +279,13 @@ public static function method_meta_box( $post, $args ) {
278279
?>
279280
</select>
280281
</p>
282+
<p class="ajaxd-no-content-option">
283+
<label>
284+
<input type="hidden" name="ajaxd_no_content" value="no">
285+
<input type="checkbox" name="ajaxd_no_content" class="ajaxd-no-content" value="yes" <?php checked( $no_content ); ?>>
286+
<?php _e( 'Dropdown only (no content)', 'ajaxd' ); ?>
287+
</label>
288+
</p>
281289
<?php
282290
}
283291

@@ -306,6 +314,10 @@ public static function save_meta_boxes( $post_id, $post ) {
306314
update_post_meta( $post_id, 'ajaxd_layout', 'dropdown' );
307315
if ( isset( $_POST['ajaxd_method'] ) )
308316
update_post_meta( $post_id, 'ajaxd_method', $_POST['ajaxd_method'] );
317+
if ( isset( $_POST['ajaxd_method'] ) && 'redirect' == $_POST['ajaxd_method'] && isset( $_POST['ajaxd_no_content'] ) )
318+
update_post_meta( $post_id, 'ajaxd_no_content', $_POST['ajaxd_no_content'] );
319+
else
320+
update_post_meta( $post_id, 'ajaxd_no_content', 'no' );
309321
}
310322

311323
/**
@@ -343,8 +355,9 @@ public static function shortcode( $atts ) {
343355
else
344356
$current = reset( $include );
345357

346-
// Get method
358+
// Get method and no content option
347359
$method = get_post_meta( $id, 'ajaxd_method', true );
360+
$no_content = get_post_meta( $id, 'ajaxd_no_content', true );
348361

349362
/**
350363
* Select options
@@ -368,32 +381,37 @@ public static function shortcode( $atts ) {
368381
$script = '$("#ajaxd-select-' . $id . '").change(function(){$.post("' . admin_url('admin-ajax.php') . '",{"action":"ajax_dropdown","post_id":$(this).val()},function(response){if(response!=0){$("#ajaxd-posts-' . $id . '").html(response)};});});';
369382
endif;
370383

371-
// Get global $wp_query and hold onto original queried object
372-
global $wp_query;
373-
$queried_object = $wp_query->queried_object;
384+
// Skip if no content
385+
if ( 'yes' == $no_content ):
386+
$content = '';
387+
else:
388+
// Get global $wp_query and hold onto original queried object
389+
global $wp_query;
390+
$queried_object = $wp_query->queried_object;
391+
392+
// Limit posts to current if not inline
393+
if ( 'inline' != $method ):
394+
$include = array( $current );
395+
endif;
396+
397+
// Loop through posts
398+
$content = '<div class="ajaxd-posts" id="ajaxd-posts-' . $id . '">';
399+
foreach ( $include as $post_id ):
400+
$query = new WP_Query( array( 'p' => $post_id, 'post_type' => 'any' ) );
401+
if ( ! $query->have_posts() ) continue;
402+
while ( $query->have_posts() ): $query->the_post();
403+
global $post;
404+
$wp_query->queried_object = $post;
405+
$content .= '<div class="ajaxd-post" id="ajaxd-post-' . $post_id . '"' . ( $current == $post_id ? '' : ' style="display:none;"' ) . '>' . apply_filters( 'the_content', get_the_content() ) . '</div>';
406+
endwhile;
407+
endforeach;
408+
wp_reset_postdata();
409+
$content .= '</div><!-- .ajaxd-posts -->';
374410

375-
// Limit posts to current if not inline
376-
if ( 'inline' != $method ):
377-
$include = array( $current );
411+
// Restore original queried object
412+
$wp_query->queried_object = $queried_object;
378413
endif;
379414

380-
// Loop through posts
381-
$content = '<div class="ajaxd-posts" id="ajaxd-posts-' . $id . '">';
382-
foreach ( $include as $post_id ):
383-
$query = new WP_Query( array( 'p' => $post_id, 'post_type' => 'any' ) );
384-
if ( ! $query->have_posts() ) continue;
385-
while ( $query->have_posts() ): $query->the_post();
386-
global $post;
387-
$wp_query->queried_object = $post;
388-
$content .= '<div class="ajaxd-post" id="ajaxd-post-' . $post_id . '"' . ( $current == $post_id ? '' : ' style="display:none;"' ) . '>' . apply_filters( 'the_content', get_the_content() ) . '</div>';
389-
endwhile;
390-
endforeach;
391-
wp_reset_postdata();
392-
$content .= '</div><!-- .ajaxd-posts -->';
393-
394-
// Restore original queried object
395-
$wp_query->queried_object = $queried_object;
396-
397415
// Return output
398416
return $select . $content . '<script type="text/javascript">(function($) {' . $script . '})(jQuery);</script>';
399417
}

assets/js/ajaxd-admin.js

+6
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,10 @@ jQuery(document).ready(function($){
4646
$table = $(".ajaxd-posts-table");
4747
if ( $table.find(" > tbody tr:visible").length == 0 ) $table.find(" > tbody tr.ajaxd-placeholder").show();
4848
});
49+
50+
// Hide or show no content option when method is changed
51+
$(".ajaxd-method").change(function() {
52+
if ( $(this).val() == "redirect" ) $(".ajaxd-no-content-option").show();
53+
else $(".ajaxd-no-content-option").hide();
54+
}).trigger("change");
4955
});

0 commit comments

Comments
 (0)