使用插件钩子函数WordPress进行两次后卷曲函数保存

时间:2014-05-06 作者:Ravi Patel

当我用curl函数保存数据时,使用hook保存数据两次。

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;
            }

2 个回复
最合适的回答,由SO网友:Ravi Patel 整理而成

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;
        ?>
我的修复代码:

    <?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;
    ?>                        

SO网友:Petr Cibulka

这是正常的行为。在Wordpress堆栈交换上查看此问题:Plugin Development: Wordpress processes twice on post update. How to skip process on the first? 由@Hameedullah Khan回答。

Wordpress调用函数两次挂接到save\\u post,因为:

第一次保存后修订正如Hameedulah所建议的那样save_function() 钩住save_post 操作,如果执行自动保存,则可以返回:

if ( defined(\'DOING_AUTOSAVE\') && DOING_AUTOSAVE )
    return;

结束

相关推荐

Custom Post Types in plugins?

我一直在想这个。在我现在看到的大多数插件中,它们看起来像CPT,但没有“已发布”链接。他们也不担任职务。它们只是停留在仪表板中的插件,页面和列列表顶部有“添加新”链接,当您单击“编辑”时,可以像在任何其他CPT中一样编辑项目。所以我想知道他们是否是CPT?如果是的话,有没有关于这方面的教程?我正在考虑为我的网站创建一个插件,我想要的是构建一个插件,我可以“添加新项目”,并保存项目(并且可以编辑)。我可以通过短代码调用项目。我知道如何在函数中创建CPT。php和我一直在阅读关于基本插件创建的内容。我似乎找不