javascript - Wordpress plugin ajax returns 0 -
i'm trying send ajax data wordpress table can php 0
. on admin side. can me?
also of code inside plugin-admin.php
file inside plugin folder.
<?php if ( ! defined( 'abspath' ) || ! current_user_can( 'manage_options' ) ) exit; global $wpdb; global $wp_version; $results = $wpdb -> get_results( " select id, post_title, post_excerpt $wpdb->posts post_type = 'post' , post_status not 'auto-draft' , post_title not 'auto draft' , post_status = 'publish' order post_title " ); add_action( 'wp_ajax_featured_submit_action', 'featured_submit_callback' ); function featured_submit_callback(){ echo "hi"; wp_die(); } ?> <div class="wrap"> <h2>select posts</h2> <select id="selected-posts" multiple="multiple"> <?php foreach ( $results $result ){ ?><option value="<?php echo $result->id; ?>"> <?php echo $result->post_title; ?> </option> <?php } ?> </select> <br> <input type="submit" id="sposts-submit"></input> </div> <script> jquery(document).ready(function($) { var spostsarray = new array(); //click button $("#sposts-submit").click(function(){ var spostsarray = new array(); $("#selected-posts").each(function(item){ spostsarray.push( $(this).val() ); }); console.log(spostsarray); var data = { "action": "featured_submit_action", "posts": spostsarray } $.ajax({ url: "<?php echo admin_url('admin-ajax.php'); ?>", type: "post", action: "featured_submit_action", data: {"posts": spostsarray}, success: function(data){ console.log(data); } }); }); }); </script>
i've condensed bit general idea can grab recent posts , user can select ones want feature, send php method , edit table it.
the problem ajax callback ever return 0
, not data sent javascript.
solved: after rohil_phpbeginner figured out. reason didn't work executing code menu page @ at point late add hook. here page used solve it:
below code worked fine me:
<?php global $wpdb; global $wp_version; $results = $wpdb -> get_results( " select id, post_title, post_excerpt $wpdb->posts post_type = 'post' , post_status not 'auto-draft' , post_title not 'auto draft' , post_status = 'publish' order post_title " ); ?> <div class="wrap"> <h2>select posts</h2> <select id="selected-posts" multiple="multiple"> <?php foreach ( $results $result ){ ?><option value="<?php echo $result->id; ?>"> <?php echo $result->post_title; ?> </option> <?php } ?> </select> <br> <input type="submit" id="sposts-submit"></input> </div> <?php add_action( 'wp_ajax_featured_submit_action', 'featured_submit_callback' ); add_action( 'wp_ajax_nopriv_featured_submit_action', 'featured_submit_callback' ); function featured_submit_callback(){ echo "hi"; wp_die(); } ?> <script> jquery(document).ready(function($) { //click button $("#sposts-submit").click(function(){ var spostsarray = new array(); $("#selected-posts").each(function(item){ spostsarray.push( $(this).val() ); }); console.log(spostsarray); var data = { "action": "featured_submit_action", "posts": spostsarray } $.ajax({ url: ajaxurl, type: "post", data: data, success: function(data){ console.log(data); } }); }); }); </script>
you don't need pass ajax url in way because when used code, showing me php. wordpress provides default url ajax can use that( ajaxurl
used in below code).
other have not added code no-privilege
user (if going use privileged user okay otherwise need add code that).
Comments
Post a Comment