0

I've got a custom theme I build using an underscores starter theme. Inside one of my template files (page-talent.php) I've got this jQuery ajax function:

   $.ajax({
          type: 'POST',
          url: '<?php echo admin_url('admin-ajax.php');?>',
          dataType: "html", // add data type
         // data: { action : 'get_ajax_posts' },
          data: { action : 'get_ajax_posts' , filters: filters },
          success: function( response ) {
              console.log( response );
              //alert(response);
              $( '.posts-area' ).html( response ); 
          }
      });      
    })

then I'm running the code in admin-ajax.php inside the admin folder. I'm not sure why I did that (rather than someplace inside my theme directory) other than that's what some of posts in this forum said to do.

The Problem is that when wordpress updates it wipes it out. So the question is how can I put this inside my theme? I tried just calling a file inside my theme folder and it didn't work.

here is my code that I placed inside the existing admin-ajax.php right before the do_action( 'admin_init' ); section. Again this works fine inside this file, but it gets wiped out every time I update the version of wordpress.

// ------------------------------
// ------------------------------
//       talent query code
// ------------------------------
// ------------------------------

function get_ajax_posts() {

    // get filters from 3 drop down menus 
    $tax_query = $_POST['filters'];
    
    $location = $tax_query['location'];
    $specialty = $tax_query['specialty'];

    $levels = $tax_query['level'];
    // create levels array for selected level and above
    switch ($levels) {
            case 23:
                $level = array('23'); // Level 1 through 5
                break;
            case 24:
               $level = array('24'); // Level 2 through 5
                break;
            case 25:
                $level = array('25');// Level 3 through 5
                break;
            case 26:
                $level = array('26');// Level 4 and 5
                break;
            case 27:
                $level = '27';// Level 5
                break;
            default:
                $level = array('23', '24', '25', '26', '27');
        }

    // Display Talent if only Level is selected
    if(isset($tax_query['level']) && empty($tax_query['location']) && empty($tax_query['specialty'])){

        // Query Arguments
        $args = array(
          'orderby' => 'title',
          'order' => 'ASC',
          'post_type' => 'the-talent',
          'posts_per_page'=>-1,
          'tax_query' => array(
              'relation' => 'AND',
              array(
                  'taxonomy' => 'level',
                  'field'    => 'term_id',
                  'terms'    => $level,// 23 (4), 24(4), 25(7), 26(3), 27(3) // array( 25, 26, 27 )
                  
              ),   
           ),
        );
    }
    // Display Talent if only Level and Location is selected
    else if(isset($tax_query['level']) && isset($tax_query['location']) && empty($tax_query['specialty'])){

        // Query Arguments
        $args = array(
          'orderby' => 'title',
          'order' => 'ASC',
          'post_type' => 'the-talent',
          'posts_per_page'=>-1,
          'tax_query' => array(
              'relation' => 'AND',
              array(
                  'taxonomy' => 'level',
                  'field'    => 'term_id',
                  'terms'    => $level,// 23 (4), 24(4), 25(7), 26(3), 27(3) // array( 25, 26, 27 )
                  
              ), 
              array(
                  'taxonomy' => 'location',
                  'field'    => 'term_id',
                  'terms'    => $location,
              ),  
          ),
        );
    }
    // Display Talent if all three are selected
    else if(isset($tax_query['level']) && empty($tax_query['location']) && isset($tax_query['specialty'])){

        // Query Arguments
        $args = array(
          'orderby' => 'title',
          'order' => 'ASC',
          'post_type' => 'the-talent',
          'posts_per_page'=>-1,
          'tax_query' => array(
              'relation' => 'AND',
              array(
                  'taxonomy' => 'level',
                  'field'    => 'term_id',
                  'terms'    => $level,// 23 (4), 24(4), 25(7), 26(3), 27(3) // array( 25, 26, 27 )  
              ), 
              array(
                  'taxonomy' => 'specialty',
                  'field'    => 'term_id',
                  'terms'    => $specialty,
              ),  
          ),
        );
    }
    // Display Talent if Level and specialty is selected
    else if(isset($tax_query['level']) && isset($tax_query['location']) && isset($tax_query['specialty'])){

        // Query Arguments
        $args = array(
          'orderby' => 'title',
          'order' => 'ASC',
          'post_type' => 'the-talent',
          'posts_per_page'=>-1,
          'tax_query' => array(
              'relation' => 'AND',
              array(
                  'taxonomy' => 'level',
                  'field'    => 'term_id',
                  'terms'    => $level,// 23 (4), 24(4), 25(7), 26(3), 27(3) // array( 25, 26, 27 )  
              ), 
              array(
                  'taxonomy' => 'location',
                  'field'    => 'term_id',
                  'terms'    => $location,
              ),
              array(
                  'taxonomy' => 'specialty',
                  'field'    => 'term_id',
                  'terms'    => $specialty,
              ),  
          ),
        );
    }
    // Display Talent if only Location is selected
    else if(empty($tax_query['level']) && isset($tax_query['location']) && empty($tax_query['specialty'])){

        // Query Arguments
        $args = array(
          'orderby' => 'title',
          'order' => 'ASC',
          'post_type' => 'the-talent',
          'posts_per_page'=>-1,
          'tax_query' => array(
              'relation' => 'AND', 
              array(
                  'taxonomy' => 'location',
                  'field'    => 'term_id',
                  'terms'    => $location,
              ),  
          ),
        );
    }
    // Display Talent if Location and specialty is selected
    else if(empty($tax_query['level']) && isset($tax_query['location']) && isset($tax_query['specialty'])){

        // Query Arguments
        $args = array(
          'orderby' => 'title',
          'order' => 'ASC',
          'post_type' => 'the-talent',
          'posts_per_page'=>-1,
          'tax_query' => array(
              'relation' => 'AND',
              array(
                  'taxonomy' => 'location',
                  'field'    => 'term_id',
                  'terms'    => $location,
              ),
              array(
                  'taxonomy' => 'specialty',
                  'field'    => 'term_id',
                  'terms'    => $specialty,
              ),  
          ),
        );
    }
    // Display Talent if only specialty is selected
    else if(empty($tax_query['level']) && empty($tax_query['location']) && isset($tax_query['specialty'])){

        // Query Arguments
        $args = array(
          'orderby' => 'title',
          'order' => 'ASC',
          'post_type' => 'the-talent',
          'posts_per_page'=>-1,
          'tax_query' => array(
              'relation' => 'AND',
              array(
                  'taxonomy' => 'specialty',
                  'field'    => 'term_id',
                  'terms'    => $specialty,
              ),  
          ),
        );
    }
    
    else{
        $args = null;
        //echo "else Args: ". $args;
    }

    wp_reset_query();

    // The Query
    $ajaxposts = new WP_Query( $args );

    $response = '';

    // The Query
    if ( $ajaxposts->have_posts() ) {
        while ( $ajaxposts->have_posts() ) {
            $ajaxposts->the_post();
            //$response .= get_template_part('products');

            $response .= "";

            $name = get_field('name');
            $main_image = get_field('main_image');

         ?>

        <div class="col-sm-6 col-md-3 talent-col">
          <div class="talent">
            <a type="button" href="<?php the_permalink() ?>">
             <img class="img-responsive" src="<?php echo $main_image; ?>">
             <h3 class="dark"><?php echo $name; ?></h3> 
            </a>
          </div><!-- close talent -->
        </div><!-- close col -->

       <?php
     
       }// end while
    } else {
        $response .= get_template_part('none');
    }
    
    exit; // leave ajax call
}// end get_ajax_posts

// Fire AJAX action for both logged in and non-logged in users
add_action('wp_ajax_get_ajax_posts', 'get_ajax_posts');
add_action('wp_ajax_nopriv_get_ajax_posts', 'get_ajax_posts');


//===================
// end talent query code
//===================

If I add this to my functions.php file how do I call it from the Ajax function? Or If I just move the function get_ajax_posts() to a new file (talent-ajax.php) inside my theme folder how do I call that from the ajax function. In other words... what would this part be changed to reach that file?

url: '<?php echo admin_url('admin-ajax.php');?>', 
2
  • You shouldn't modify files in WP core, and you shouldn't make direct requests to PHP files in your theme. Instead use the APIs to register your handlers in a plugin or theme functions file
    – Tom J Nowell
    Feb 15, 2021 at 18:25
  • exactly, that's why I posted this question to learn how to do it inside my custom theme.
    – Aaron4osu
    Feb 16, 2021 at 19:24

2 Answers 2

2

You should never edit any files in the wp-admin directory. All ajax related to your theme, should be inside your theme directory. You can place your PHP code in any file, functions.php, or include some new file there you will retrive only ajax cals, that will be better.

1

Add this all to your theme functions.php file and try again..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.