Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

when i save a data with my curl function using hook to save a data twice.

public function __construct() {
            add_action('init', array(&$this, 'init'));
            add_action('admin_init', array(&$this, 'admin_init'));
            add_action('admin_menu', array(&$this, 'add_page'));
            add_action('admin_notices', array(&$this,'socipilot_admin_notice') );
            add_action('admin_bar_menu', array(&$this, 'socipilot_adminbar_links' ), 1001 );
            add_action('add_meta_boxes', array(&$this, 'add_meta_box' ) );
            add_action('admin_enqueue_scripts', array(&$this,'socipilot_enqueue_scripts')); 

            add_action('save_post', array(&$this, 'save'));
            //add_action('publish_post', array(&$this, 'save'));
            //add_action('edit_post', array(&$this, 'save'));
            //add_action('pre_post_update',array(&$this, 'save'));

            add_filter('plugin_action_links_'.SOCI_PILOT_PLUGIN_BASENAME, array(&$this,'ts_add_plugin_action_links'));
            // Listen for the activate event
            register_activation_hook(SOCI_PILOT_FILE, array(&$this, 'activate'));
            // Deactivation plugin
            register_deactivation_hook(SOCI_PILOT_FILE, array(&$this, 'deactivate'));
        }




    public function save($post_id) {
               global $post;


                    // Autosave, do nothing
                    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) 
                            return;
                    // AJAX? Not used here
                    if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) 
                            return;
                    // Check user permissions
                    if ('post' == $_POST['post_type']){
                        if ( ! current_user_can( 'edit_post', $post_id ) )
                        return;
                    } 
                    // Return if it's a post revision
                    if ( false !== wp_is_post_revision( $post_id ) )
                            return;


                    // Check if our nonce is set.
                if ( ! isset( $_POST['socipilot_inner_custom_box_nonce'] ) )
                return;

            $nonce = $_POST['socipilot_inner_custom_box_nonce'];

                    // Verify that the nonce is valid.
            if ( ! wp_verify_nonce( $nonce, 'socipilot_inner_custom_box' ) )
                return;

                   if(isset($_POST['soci-pilot'])):
                        $all_sociuser_posts = $_POST['soci-pilot'];
                        foreach ($all_sociuser_posts as $key=>$val){
                            $mydata[] = sanitize_text_field($key);
                        }

                        if(!empty($mydata)):


                            $mydata1 = get_post_meta( $post->ID, '_socipilot_meta_value_key', true );

                            if(!empty($mydata1)):
                                $update_soci = array_unique(array_merge($mydata1,$mydata));
                                update_post_meta( $post_id, '_socipilot_meta_value_key', $update_soci);
                            else:
                                update_post_meta( $post_id, '_socipilot_meta_value_key', $mydata);
                            endif;

                            update_post_meta( $post_id, 'soci-pilot_account_type', $_POST['soci-pilot_account_type']);
                            update_post_meta( $post_id, 'soci-pilot_schedule_date', $_POST['soci-pilot_schedule_date']);

                            $options = get_option($this->option_name);

                             $data[].= implode(',',$mydata);
                             $data[].= get_permalink($post_id);
                             $data[].= $_POST['post_title'];
                             $data[].= wp_get_attachment_url(get_post_thumbnail_id($_POST['post_ID']));
                             if($_POST['post_excerpt']):
                                $len = strpos(substr(strip_tags($_POST['post_excerpt']),25,100),'.')+1;
                                $data[].= substr(strip_tags($_POST['post_excerpt']),0,25+$len);
                             else:
                                $len = strpos(substr(strip_tags($_POST['post_content']),25,100),'.')+1;
                                $data[].= substr(strip_tags($_POST['post_content']),0,25+$len);
                            endif;

                            $data[].= $_POST['soci-pilot_account_type'];

                            if($_POST['soci-pilot_post_is_auto']=='Y'){
                                $data[].= "";
                            }else{
                                $data[].= $_POST['soci-pilot_schedule_date'];
                            }

                            if(($options['public_key']!="")&&($options['private_key']!="")):
                                $res = actionTest($options['public_key'],$options['private_key']); 
                                $error_id = $res['error'];
                            else:
                                $error_id = 1;
                            endif;

                            if($error_id==0 && isset($res)):
                                $res = actionPosttest($options['public_key'],$options['private_key'],$data);
                                if($res->error==1){
                                     global $my_error;
                                     echo $res->msg;
                                }
                            endif;
                        else:
                            //add_post_meta($post_id, '_socipilot_meta_value_key', $mydata, true);
                            update_post_meta( $post_id, '_socipilot_meta_value_key', $mydata);
                        endif;
                  else:      
                        $update_soci = get_post_meta( $post->ID, '_socipilot_meta_value_key', true );
                        update_post_meta( $post_id, '_socipilot_meta_value_key', $update_soci);
                  endif;

                  return;
            }
share|improve this question
    
Please, provide a description of the problem. –  cybmeta May 6 at 8:26
    
i save a post data send on my curl function so add twice a post on my curl site db. –  ravi patel May 6 at 9:14
    
I'm sorry but I still don't understand your question. Can you clarify? Are you trying to save a post using cURL? Can you post the cURL code and the saving code? Do you want to save the post twice or saving twice is the problem? –  cybmeta May 6 at 12:43

2 Answers 2

That is normal behaviour. See this question on Wordpress Stack Exchange: Plugin Development: Wordpress processes twice on post update. How to skip process on the first? answered by @Hameedullah Khan.

Wordpress call functions hooked to save_post twice because:

  • first time it saves the post revision.
  • second time it saves the actual post.

As Hameedulah suggests, from your save_function() hooked at save_post action, you can return if DOING_AUTOSAVE:

if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE )
    return;
share|improve this answer
    
thanks for reply but i have all condition use on hook see my question i have also added this one. my post is not save 2 times but my curl function to 2 times call to save post data 2 times on curl data server. –  ravi patel May 9 at 11:20
up vote -1 down vote accepted

More info this code

   <?php
               global $my_save_post_flag;
                       // .. some stuff here ..
               if ($my_save_post_flag == 0) {
                      // .. do the stuff only once i have add my curl code hear ..
               }  
                     // .. continue your stuff ..
               $my_save_post_flag = 1;
        ?>

my fixing code:

    <?php 
        global $socipost_save_post_flag;

        if($error_id==0 && isset($res)):
            if ($socipost_save_post_flag == 0){
                $res = actionPosttest($options['public_key'],$options['private_key'],$data);

                //this is my curl function so i need only one execution 
            } 
        $socipost_save_post_flag =1;

        endif;
    ?>                        
share|improve this answer
    
Don't add non code in backticks or in codeblock please –  Pieter Goosen May 6 at 11:08
    
i have fix twice issue using this code add my plugin code. –  ravi patel May 6 at 12:46
    
First, explain better your question. Second, if this code fix your problem, explain why and how to help other users with the same problem understand the solution. –  cybmeta May 8 at 6:12

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.