php - Woocommerce: save_post hook is not being triggered -


i have meta box i'm coding product post type comes woocommerce. i've run problem can't pass in 'save_post' hook not seem working @ products. works posts, since i've changed code work products nothing. save_post function i've hooked absolutely nothing @ moment. i've added sorts of code , doesn't matter, script doesn't seem far. missing obvious?

edit: aside, added

?> <script type="text/javascript">              var post_id = '<?php $post_id ?>';             console.log("id is: " +  post_id );             </script><?php 

but returns absolutely nothing.

<?php /* *   represents plugin's meta box * *   @since          0.0.1 *   @package        bbplugin *   @subpackage     bbplugin *   @author         christopher dando <captaindando@gmail.com> */  /* *   represents plugin's meta box * *   register's meta box wordpress api, sets properties, *   including markup associated view *    *   @package        bbplugin *   @subpackage     bbplugin/admin *   @author         christopher dando <captaindando@gmail.com> */  class bbplugin_meta_box{      /*     * register class wordpress api     *      * @since 0.0.1     */     public function initialize_hooks(){         //add_action( 'add_meta_boxes_product', array( $this, 'add_meta_box' ) );         add_action( 'add_meta_boxes', array( $this, 'bbadd_meta_box' ) );          // checks when wordpress saving or         // updating post.         add_action( 'save_post', array( $this, 'save_post' ) );          $junk = $post_id;         ?> <script type="text/javascript">              var post_id = '<?php global $post; echo $post->id; ?>';             console.log("id is: " +  post_id );             </script><?php     }       // add_meta_boxes wordpress function. add_meta_box our new function      /*     * function responsible creating actual meta box.     *     * @since    0.0.1     */     public function bbadd_meta_box(){         ?> <script>console.log("meta box added");</script><?php         add_meta_box(             'bbplugin',             "brave books",             array( $this, 'display_meta_box' ),             'product',             'normal',             'default'         );     }     // defines properties of meta box.      /*     * renders content of meta box.     *     * @since    0.0.1     */     public function display_meta_box(){         include_once( 'views/bbplugin-navigation.php' );     }       /**      * sanitizes , serializes information associated post.      *      * @since    0.0.1      *      * @param    int    $post_id    id of post that's being edited.      */       // strangely, calls if meta box not render     public function save_post( $post_id ) {         ?><script>alert("post saved");</script><?php         /* if we're not working 'product' post type or                user doesn't have permission save,             exit function.         */         if ( ! $this->user_can_save( $post_id, 'bbplugin_nonce', 'bbplugin_save' ) ) {              return;         }          /*             need 'sanitise' our information before              can save database. means             must strip of html tags             , extract text itself.         */          // if 'resources' inputs exist, iterate through them , sanitize them         if ($this->value_exists( 'bbplugin-resources' ) ) {             // divs id of meta-box-resources             $this->update_post_meta(                 $post_id,                 'bbplugin-resources',                 $this->sanitize_data( 'bbplugin-resources', true )             );         }         else {             // leaving input blank on front end remove specific input.             $this->delete_post_meta( $post_id, 'bbplugin-resources' );         }      }        /**      * determines whether or not value exists in $_post collection      * identified specified key.      *      * @since   0.0.1      *      * @param   string    $key    key of value in $_post collection.      * @return  bool              true if value exists; otherwise, false.      */     private function value_exists( $key ) {         return ! empty( $_post[ $key ] );     }      /**      * deletes specified meta data associated specified post id       * based on incoming key.      *      * @since    0.0.1      * @access   private      * @param    int    $post_id    id of post containing meta data      * @param    string $meta_key   id of meta data value      */     private function delete_post_meta( $post_id, $meta_key ) {         if ( '' !== get_post_meta( $post_id, $meta_key, true ) ) {             delete_post_meta( $post_id, '$meta_key' );         }     }      private function update_post_meta( $post_id, $meta_key, $meta_value ) {            if ( is_array( $_post[ $meta_key ] ) ) {             $meta_value = array_filter( $_post[ $meta_key ] );         }         /*              update_post_meta adds database if there nothing there already.             parameters follows:              1. post id used associate information post.             2. meta key that's used uniquely identify value.             3. actual value associated meta key.         */           update_post_meta( $post_id, $meta_key, $meta_value );     }      /**      * sanitizes data in $_post collection identified specified key      * based on whether or not data text or array.      *      * @since    1.0.0      * @access   private      * @param    string        $key                      key used retrieve data $_post collection.      * @param    bool          $is_array    optional.    true if incoming data array.      * @return   array|string                            sanitized data.      */     private function sanitize_data( $key, $is_array = false ) {         $sanitized_data = null;         if ( $is_array ) {             $resources = $_post[ $key ];             $sanitized_data = array();             foreach ( $resources $resource ) {                 $resource = esc_url( strip_tags( $resource ) );                 if ( ! empty( $resource ) ) {                     $sanitized_data[] = $resource;                 }             }         }          else {             $sanitized_data = '';             $sanitized_data = trim( $_post[ $key ] );             $sanitized_data = esc_textarea( strip_tags( $sanitized_data ) );         }         return $sanitized_data;     }      /**      * verifies post type that's being saved post (versus page or      * custom post type.      *      *      * @since       0.0.1      * @access      private      * @return      bool      return if current post type post; false, otherwise.     */     private function is_valid_post_type() {         return ! empty( $_post['post_type'] ) && 'post' == $_post['post_type'];     }      /**      * determines whether or not current user has ability save meta data associated post.      *      * @since       0.0.1      * @access      private      * @param       int     $post_id      id of post being save      * @param       string  $nonce_action name of action associated nonce.      * @param       string  $nonce_id     id of nonce field.      * @return      bool                  whether or not user has ability save post.      */     private function user_can_save( $post_id, $nonce_action, $nonce_id ) {          $is_autosave = wp_is_post_autosave( $post_id );         $is_revision   = wp_is_post_revision( $post_id );         $is_valid_nonce = ( isset( $_post[ $nonce_action ] ) && wp_verify_nonce( $_post[ $nonce_action ], $nonce_id ) );          // return true if user able save; otherwise, false.         return ! ( $is_autosave || $is_revision ) && $this->is_valid_post_type() && $is_valid_nonce;     }       } ?> 

in wordpress, save_post isn't destination in itself; action carried out 'between' pages: hit update, , wordpress carry out series of actions behind scenes before returning appropriate page (invariably post editing, notification status of save).

as such, never see results of echo, print_r, or js alert or console.log, because save_post isn't user-facing action.

if want see if save_post action being carried out in way, recommend throwing in die(), so:

public function save_post($post_id) {     ?><script>alert("post saved");</script><?php     die(); } 

if save_post action being fired correctly, should see js alert on blank page. if want see if function carrying out actual wordpress-style functionality, i'd recommend simple update_post_meta confirm:

public function save_post($post_id) {     // insert actual logic ensure you're not doing on every post time      update_post_meta($post_id, 'i_am_saved', 'totes saved post #' . $post_id); } 

you can check database (or click view custom fields within post) see if custom metadata has been added.

i recommend attaching save_post action specifically product post type that's used woocommerce:

add_action('save_post_product', array($this, 'save_post'));

that save redundancy checking later.


Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

javascript - Get parameter of GET request -

javascript - Twitter Bootstrap - how to add some more margin between tooltip popup and element -