开发者 | sunjanle |
---|---|
更新时间 | 2011年8月20日 16:20 |
PHP版本: | 3.1 及以上 |
WordPress版本: | 3.2.1 |
The javascript code: $j.ajax( { url : home_url, type : 'POST', dataType : 'html', data : { 'ajax_extend_mark' : 1, /* this is indispensable if you want ajax-extend to call the function you need. */ 'ajax_extend_action' : 'my_function', /* the function name (a WP core function, or a function in one plugin. any functions loaded by wp()) */ 'name' : 'sunjianle', }, success : function(data, textStatus, errorThron) { alert(data); } }); The background PHP code: function my_function() { $name = $_POST["name"]; echo "Hello, " . $name; global $wpdb; $query_sql = "select user_login from wp_users limit 0,10"; $users = $wpdb->get_results($wpdb->prepare($query_sql)); foreach($users as $user){ echo $users->user_login; } }
add_action( "wp", "ajax_operation" ); function ajax_operation() { /* * ajax_extend_mark : * ajax_extend_action : */ if(isset($_REQUEST["ajax_extend_mark"])) { $function_name = $_REQUEST["ajax_extend_action"]; if(!function_exists($function_name)) return; call_user_func($function_name); die(); } } That is the core code and the all code of ajax-extend. What you need to understand the code above.
Yes.