Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Enqueue JS
- ----- */
- function post_via_ajax_scripts() {
- wp_register_script( 'post-via-ajax-js', plugin_dir_url( __FILE__ ) . 'post_via_ajax.js', array( 'jquery' ), '', true );
- wp_localize_script( 'post-via-ajax-js', 'pva_params', array( 'pva_ajax_url' => admin_url( 'admin-ajax.php' ) ) );
- wp_enqueue_script( 'post-via-ajax-js' );
- }
- add_action('wp_enqueue_scripts', 'post_via_ajax_scripts');
- //Add Ajax Actions
- add_action('wp_post_via_ajax', 'post_via_ajax');
- add_action('wp_post_via_ajax', 'post_via_ajax');
- /* WP Insert Post Function
- ----- */
- function post_via_ajax()
- {
- $query_post_via_ajax = $_POST;
- $post_title = ($query_post_via_ajax['post_title']) ? $query_post_via_ajax['post_title'] : false;
- // Create post object
- $new_pva_post = array(
- 'post_type' => 'page',
- 'post_title' => wp_strip_all_tags($post_title ),
- 'post_status' => 'publish',
- 'post_author' => 1,
- );
- // Insert the post into the database
- wp_insert_post( $new_pva_post );
- exit();
- };
- /* Form Shortcode
- ----- */
- function post_via_ajax_shortcode( $atts, $content = null ) {
- return '<input id="post_title" name="post_title" type="text"><button id="create_post">Create Post</button>';
- }
- add_shortcode( 'post_via_ajax', 'post_via_ajax_shortcode' );
- jQuery( document ).ready( function ( $ ) {
- $('#create_post').attr('disabled',true);
- $('#post_title').keyup(function(){
- if($(this).val().length > 3) {
- $('#create_post').attr('disabled', false);
- $('#create_post').click(function(event){
- post_via_ajax();
- });
- } else {
- $('#create_post').attr('disabled',true);
- }
- });
- // Return Post Title Field Value
- function returnNewPostTitle(){
- var newPostTitleValue = $('#post_title').val();
- return newPostTitleValue;
- }
- // AJAX > Get City Posts
- function post_via_ajax()
- {
- console.log('woo');
- var pva_ajax_url = pva_params.pva_ajax_url;
- //alert('woo');*/
- $.ajax({
- type: 'POST',
- url: pva_ajax_url,
- data: {
- action: 'post_via_ajax',
- post_title: returnNewPostTitle(),
- },
- beforeSend: function ()
- {
- console.log('sending');
- },
- success: function(data)
- {
- console.log('yay');
- },
- error: function()
- {
- console.log('nay');
- }
- })
- }
- });
- /**
- Plugin Name: Post via Ajax
- */
- /* Enqueue JS
- ----- */
- function pva_scripts() {
- wp_register_script( 'pva-js', plugin_dir_url( __FILE__ ) . 'pva.js', array( 'jquery' ), '', true );
- wp_localize_script( 'pva-js', 'pva_params', array( 'pva_ajax_url' => admin_url( 'admin-ajax.php' ) ) );
- wp_enqueue_script( 'pva-js' );
- };
- add_action('wp_enqueue_scripts', 'pva_scripts');
- // creating Ajax call for WordPress
- add_action( 'wp_ajax_nopriv_pva_create', 'pva_create' );
- add_action( 'wp_ajax_pva_create', 'pva_create' );
- /* WP Insert Post Function
- ----- */
- function pva_create()
- {
- $post_title = $_POST['post_title'];
- // Create post object
- $new_pva_post = array(
- 'post_type' => 'page',
- 'post_title' => $post_title,
- 'post_status' => 'publish',
- 'post_author' => 1,
- );
- // Insert the post into the database
- wp_insert_post( $new_pva_post );
- exit();
- };
- /* Form Shortcode
- ----- */
- function pva_shortcode( $atts, $content = null ) {
- ob_start();
- include(plugin_dir_path( __FILE__ ) . 'post_via_ajax_field.php');
- $ret = ob_get_contents();
- ob_end_clean();
- return $ret;
- //pva();
- };
- add_shortcode( 'pva', 'pva_shortcode' );
- jQuery( document ).ready( function ( $ ) {
- $('#create_post').attr('disabled',true);
- $('#post_title').keyup(function(){
- if($(this).val().length > 3) {
- $('#create_post').attr('disabled', false);
- } else {
- $('#create_post').attr('disabled',true);
- }
- });
- $('#create_post').click(function(event){
- post_via_ajax();
- });
- // Return Post Title Field Value
- function returnNewPostTitle(){
- var newPostTitleValue = $('#post_title').val();
- return newPostTitleValue;
- }
- // AJAX > Get City Posts
- function post_via_ajax()
- {
- var pva_ajax_url = pva_params.pva_ajax_url;
- $.ajax({
- type: 'POST',
- url: pva_ajax_url,
- data: {
- action: 'pva_create',
- post_title: returnNewPostTitle()
- },
- beforeSend: function ()
- {
- console.log('sending');
- },
- success: function(data)
- {
- console.log('yay');
- },
- error: function()
- {
- console.log('nay');
- }
- })
- }
- });
Add Comment
Please, Sign In to add comment