-
-
Save Tusko/a02e816b57cc20c24a93db273f2a7179 to your computer and use it in GitHub Desktop.
Wordpress Ajax
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//JS | |
var ajaxcontent = $('.products'); | |
$.ajax({ | |
type: 'POST', | |
url: window.wp_data.ajax_url, | |
data: { | |
action: "loadPosts", | |
}, | |
success: function (html) { | |
ajaxcontent.html(html); | |
} | |
}); | |
return false; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/*IS NOT WOOCOMMERCE*/ | |
if ( ! function_exists( 'is_ajax' ) ) { | |
/** | |
* is_ajax - Returns true when the page is loaded via ajax. | |
* @return bool | |
*/ | |
function is_ajax() { | |
return defined( 'DOING_AJAX' ); | |
} | |
} | |
//FUNCTIONS AJAX | |
add_action( 'wp_ajax_loadPosts', 'loadPosts'); | |
add_action( 'wp_ajax_nopriv_loadPosts', 'loadPosts'); | |
function loadPosts($paged = 1){ | |
extract($_POST); | |
//show post | |
if (is_ajax()) { | |
exit(); | |
} | |
} | |
//FUNCTIONS VARIABLES JS | |
function js_variables(){ | |
$variables = array ( | |
'ajax_url' => admin_url('admin-ajax.php'), | |
); | |
echo( | |
'<script type="text/javascript">window.wp_data = '. | |
json_encode($variables). | |
';</script>' | |
); | |
} | |
add_action('wp_head','js_variables'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment