Guest User

Untitled

a guest
Dec 17th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.93 KB | None | 0 0
  1. /* Enqueue JS
  2. ----- */
  3.  
  4. function post_via_ajax_scripts() {
  5. wp_register_script( 'post-via-ajax-js', plugin_dir_url( __FILE__ ) . 'post_via_ajax.js', array( 'jquery' ), '', true );
  6. wp_localize_script( 'post-via-ajax-js', 'pva_params', array( 'pva_ajax_url' => admin_url( 'admin-ajax.php' ) ) );
  7. wp_enqueue_script( 'post-via-ajax-js' );
  8. }
  9. add_action('wp_enqueue_scripts', 'post_via_ajax_scripts');
  10.  
  11. //Add Ajax Actions
  12. add_action('wp_post_via_ajax', 'post_via_ajax');
  13. add_action('wp_post_via_ajax', 'post_via_ajax');
  14.  
  15. /* WP Insert Post Function
  16. ----- */
  17.  
  18. function post_via_ajax()
  19. {
  20.  
  21. $query_post_via_ajax = $_POST;
  22. $post_title = ($query_post_via_ajax['post_title']) ? $query_post_via_ajax['post_title'] : false;
  23.  
  24. // Create post object
  25. $new_pva_post = array(
  26. 'post_type' => 'page',
  27. 'post_title' => wp_strip_all_tags($post_title ),
  28. 'post_status' => 'publish',
  29. 'post_author' => 1,
  30. );
  31.  
  32. // Insert the post into the database
  33. wp_insert_post( $new_pva_post );
  34. exit();
  35. };
  36. /* Form Shortcode
  37. ----- */
  38.  
  39. function post_via_ajax_shortcode( $atts, $content = null ) {
  40. return '<input id="post_title" name="post_title" type="text"><button id="create_post">Create Post</button>';
  41. }
  42.  
  43. add_shortcode( 'post_via_ajax', 'post_via_ajax_shortcode' );
  44.  
  45. jQuery( document ).ready( function ( $ ) {
  46.  
  47. $('#create_post').attr('disabled',true);
  48. $('#post_title').keyup(function(){
  49. if($(this).val().length > 3) {
  50. $('#create_post').attr('disabled', false);
  51. $('#create_post').click(function(event){
  52. post_via_ajax();
  53. });
  54. } else {
  55. $('#create_post').attr('disabled',true);
  56. }
  57. });
  58.  
  59. // Return Post Title Field Value
  60. function returnNewPostTitle(){
  61. var newPostTitleValue = $('#post_title').val();
  62. return newPostTitleValue;
  63. }
  64.  
  65. // AJAX > Get City Posts
  66. function post_via_ajax()
  67. {
  68. console.log('woo');
  69. var pva_ajax_url = pva_params.pva_ajax_url;
  70.  
  71. //alert('woo');*/
  72. $.ajax({
  73. type: 'POST',
  74. url: pva_ajax_url,
  75. data: {
  76. action: 'post_via_ajax',
  77. post_title: returnNewPostTitle(),
  78. },
  79. beforeSend: function ()
  80. {
  81. console.log('sending');
  82. },
  83. success: function(data)
  84. {
  85. console.log('yay');
  86. },
  87. error: function()
  88. {
  89. console.log('nay');
  90.  
  91. }
  92. })
  93. }
  94. });
  95.  
  96. /**
  97. Plugin Name: Post via Ajax
  98. */
  99.  
  100. /* Enqueue JS
  101. ----- */
  102.  
  103. function pva_scripts() {
  104. wp_register_script( 'pva-js', plugin_dir_url( __FILE__ ) . 'pva.js', array( 'jquery' ), '', true );
  105. wp_localize_script( 'pva-js', 'pva_params', array( 'pva_ajax_url' => admin_url( 'admin-ajax.php' ) ) );
  106. wp_enqueue_script( 'pva-js' );
  107. };
  108.  
  109. add_action('wp_enqueue_scripts', 'pva_scripts');
  110.  
  111. // creating Ajax call for WordPress
  112. add_action( 'wp_ajax_nopriv_pva_create', 'pva_create' );
  113. add_action( 'wp_ajax_pva_create', 'pva_create' );
  114.  
  115. /* WP Insert Post Function
  116. ----- */
  117.  
  118. function pva_create()
  119. {
  120.  
  121. $post_title = $_POST['post_title'];
  122.  
  123. // Create post object
  124. $new_pva_post = array(
  125. 'post_type' => 'page',
  126. 'post_title' => $post_title,
  127. 'post_status' => 'publish',
  128. 'post_author' => 1,
  129. );
  130.  
  131. // Insert the post into the database
  132. wp_insert_post( $new_pva_post );
  133. exit();
  134. };
  135.  
  136.  
  137.  
  138. /* Form Shortcode
  139. ----- */
  140.  
  141. function pva_shortcode( $atts, $content = null ) {
  142. ob_start();
  143. include(plugin_dir_path( __FILE__ ) . 'post_via_ajax_field.php');
  144. $ret = ob_get_contents();
  145. ob_end_clean();
  146. return $ret;
  147. //pva();
  148. };
  149.  
  150. add_shortcode( 'pva', 'pva_shortcode' );
  151.  
  152. jQuery( document ).ready( function ( $ ) {
  153.  
  154. $('#create_post').attr('disabled',true);
  155. $('#post_title').keyup(function(){
  156. if($(this).val().length > 3) {
  157. $('#create_post').attr('disabled', false);
  158. } else {
  159. $('#create_post').attr('disabled',true);
  160. }
  161. });
  162.  
  163. $('#create_post').click(function(event){
  164. post_via_ajax();
  165. });
  166.  
  167. // Return Post Title Field Value
  168. function returnNewPostTitle(){
  169. var newPostTitleValue = $('#post_title').val();
  170. return newPostTitleValue;
  171. }
  172.  
  173. // AJAX > Get City Posts
  174. function post_via_ajax()
  175. {
  176. var pva_ajax_url = pva_params.pva_ajax_url;
  177.  
  178. $.ajax({
  179. type: 'POST',
  180. url: pva_ajax_url,
  181. data: {
  182. action: 'pva_create',
  183. post_title: returnNewPostTitle()
  184. },
  185. beforeSend: function ()
  186. {
  187. console.log('sending');
  188. },
  189. success: function(data)
  190. {
  191. console.log('yay');
  192. },
  193. error: function()
  194. {
  195. console.log('nay');
  196.  
  197. }
  198. })
  199. }
  200. });
Add Comment
Please, Sign In to add comment