Wordpress Dropdown Category Ajax Search

WordPress Drop down Category Ajax Search


Today Discussed wordpress dropdown category list ajax search without plugin and create shortcodes follows briefly discussed. normally any search field or any one task mostly used plugin activate list of more then plugin this the wordpress large file and buy the plugin. So just avoid plugin hardcode this wordpress is category list ajax search follows code.

Wordpress Dropdown Category Ajax Search
Wordpress Dropdown Category Ajax Search


shortcodes.php

function ajax_filter_search($atts, $content = null) {
    extract(shortcode_atts(array(
  'post_type'    => AFSAdmin::afs_retrieve('_general_post_type'),
  'post_tax'     => AFSAdmin::afs_retrieve('_general_post_taxonomy'),
  'posts_per_page'  => AFSAdmin::afs_retrieve('_general_posts_per_page'),
  'filter_type'   => '',
  'filter_by'     => '',
  'filter_months'    => '',
  'filter_years'    => '',
  'filter_withPDF'   => '',
  'offset'     => 0,
    ), $atts));

ajax filter search what ever search stored array format year month and pdf taxnonmy etc.,  and included functions and follows code.

if($post_type == '') { } else { $afs_args .=' post_type="'.$post_type.'" '; }
 if($posts_per_page == '') { } else { $afs_args .=' posts_per_page="'.$posts_per_page.'" '; }
 if($filter_by == '') { } else { $afs_args .=' filter_by="'.$filter_by.'" '; }
 if($filter_months == '') { } else { $afs_args .=' filter_months="'.$filter_months.'" '; }
 if($filter_years == '') { } else { $afs_args .=' filter_years="'.$filter_years.'" '; }
 if($filter_withPDF == '') { } else { $afs_args .=' filter_withPDF="'.$filter_withPDF.'" '; }
 if($offset == '') { } else { $afs_args .=' offset="'.$offset.'" '; }
  
 if($filter_type == '') { 
  $all='all'; 
  $filter_type_count = 999;
 } else { 
  $afs_args .=' filter_type="'.$filter_type.'" '; 
  $all=$filter_type;
  $filter_type_array = explode(',',$filter_type);
  $filter_type_count = count($filter_type_array);
 }
  
 $text = '';
 $i = 1;
ajax filter conditions check else and true or false conditions check and then show this html code included same php format follows.

$text .= '<div id="afs-wrapper">';
 $text .= ' <div class="press-releases">';
 $text .= '  <form id="newsForm">';
 $text .= '   <div class="row">';
 
 
 $text .= '    <div class="col-xs-1TableRowItem2">';
 
   $text .= '       <label for="category"><strong>Display:&nbsp;</strong></label>';
   $text .= '       <select name="category">';
   $text .= '        <option value="all">All</option>';
             $terms = get_terms($taxonomy, $args = array('orderby'=>'id') );
             if($terms) {
             
              foreach($terms as $term) {
               if(isset($filter_type_array)) {
                if(in_array($term->slug,$filter_type_array)) {
                 if($term->name == 'Uncategorized') { continue; }
                 $text .= '<option value="'.$term->slug.'">'.$term->name.'</option>';
                }
               } else {
                if($term->name == 'Uncategorized') { continue; }
                 $text .= '<option value="'.$term->slug.'">'.$term->name.'</option>';
               }
              }
             }
   $text .= '       </select>';
   $text .= '      </div>';
   $text .= '     </div>';
  }
  } 
 
function.php

if (!function_exists('myplugin_ajaxurl')) {
 add_action('wp_head', 'myplugin_ajaxurl');
 
 function myplugin_ajaxurl() {
 
    echo '<script type="text/javascript">
      var ajaxurl = "' . admin_url('admin-ajax.php') . '";
      var scriptpath = "' . site_url() . '";
    </script>';
 }
}

if (!function_exists('my_action_callback')) {
 // Ajax call to sort PRs
 function my_action_callback() {  
 
  // Get the values:
  $post_type  = AFSAdmin::afs_retrieve('_general_post_type');
  $posts_per_page = AFSAdmin::afs_retrieve('_general_posts_per_page');
  $filter_type  = $_POST['filingType'];
  $filter_by   = $_POST['filterBy'];
  $filter_months  = $_POST['filterMonths'];
  $filter_years  = $_POST['filterYears'];
  $filter_withPDF = $_POST['withPDF'];
 
  $offset    = $_POST['page'];
  
  echo do_shortcode('[afs_feed post_type="'.$post_type.'" posts_per_page="'.$posts_per_page.'" offset="'.$offset.'" filter_type="'.$filter_type.'" filter_by="'.$filter_by.'" filter_months="'.$filter_months.'" filter_years="'.$filter_years.'" filter_withPDF="'.$filter_withPDF.'"]');
 
  exit;
 
 }
 
 add_action('wp_ajax_my_action', 'my_action_callback');
 add_action('wp_ajax_nopriv_my_action', 'my_action_callback');
}

/*******************************************************************
* AJAX IN ADMIN
********************************************************************/
if (!function_exists('get_selected_taxonomy')) {
 // Ajax call to get taxnomy values
 function get_selected_taxonomy() {  
 
  // Get the values:
  $cur_post_tax = AFSAdmin::afs_retrieve('_general_post_taxonomy');
  $post_type   = $_POST['option'];
  
  $taxonomy_objects = get_object_taxonomies($post_type, 'objects');
  
  if($taxonomy_objects) {
   foreach($taxonomy_objects as $tax) {
    $sel = '';
    if($tax->name == 'post_format') {
     continue;
    } else {
     if($tax->name == $cur_post_tax) { $sel = 'selected="selected"'; }
     echo '<option value="'.$tax->name.'" '.$sel.'>'.$tax->label.'</option>';
    }
   } 
  } else {
   echo '<option value="none" '.$sel.'>None</option>';
  }
 
  exit;
 
 }
 
 add_action('wp_ajax_get_selected', 'get_selected_taxonomy');
 add_action('wp_ajax_nopriv_get_selected', 'get_selected_taxonomy');
}

/*add_action( 'admin_footer', 'my_action_javascript' ); // Write our JS below here

function my_action_javascript() { ?>
 <script type="text/javascript" >
 jQuery(document).ready(function($) {
  
  $('#afs-form input#submit').on({
   click: function(){
    
    //return false;
    
   }
  });
  
  var data = {
   'action': 'my_action',
   'whatever': 1234
  };

  // since 2.8 ajaxurl is always defined in the admin header and points to admin-ajax.php
  jQuery.post(ajaxurl, data, function(response) {
   console.log('Got this from the server: ' + response);
  });
 });
 </script> <?php
}*/
?>

Wordpress Dropdown Category Ajax Search Dev2Tricks 5 of 5
WordPress  Drop down Category Ajax Search Today Discussed wordpress dropdown category list ajax search without plugin and create short...

Share this

Related Posts

Previous
Next Post »