Snippets

Red Six wp_posts_ajax

Created by Frank Parent

File main.js Added

  • Ignore whitespace
  • Hide word diff
+// URL for the WP Admin
+let url_admin:string = window.location.origin + '/wp/wp-admin/admin-ajax.php';
+
+/**
+ * getExpert
+ * Retrieve the expert custom post type matching the parameter
+ * The SQL query is done in `lib/posts.php` and perform a simple WP_Query
+ * to get 1 post
+ * Note: only 1 parameter at the time will be used because when the user
+ * selects an option in a list on the site, the other filter lists reset
+ * @param service
+ * @param sector
+ * @param office
+ */
+const getExpert = ( service:number, sector:number, office:number ) => {
+    // Fallback for IE versions that don't support window.location
+    if ( !window.location.origin ) {
+        url_admin = window.location.protocol + '//' + window.location.hostname + (window.location.port ? ':' + window.location.port: '');
+    }
+
+    // Prepare the data for the request
+    let data: Object = {
+        'action'        : 'load_expert',
+        'service'       : service,
+        'sector'        : sector,
+        'office'        : office
+    };
+
+
+    // Send the AJAX request that will be handled by "lib/posts.php"
+    $.ajax({
+        url: url_admin,
+        type: 'POST',
+        data: data
+    })
+    .done( function( html ) {
+        // Add the new expert markup in the overlay
+        $expert_container.append( html );
+
+        // Hide the loader
+        $loader.fadeOut( 150, function() {
+            // Once the loader is hidden, show the new expert
+            $expert_container.find( '.expert-overlay-preview' ).fadeIn( 500 );
+        });
+    })
+    .fail( function( jqXHR, textStatus, errorThrown ) {
+        console.log( 'textStatus = ' + textStatus );
+        console.log( 'errorThrown = ' + errorThrown );
+    });
HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.