# wp-ultimate-post-grid/2.4.0/helpers/post_save.php

WP Ultimate Post Grid, version 2.4.0. 47 lines.

- Page: https://pluginprobe.com/plugins/wp-ultimate-post-grid/2.4.0/code/helpers/post_save.php
- Raw: https://pluginprobe.com/plugins/wp-ultimate-post-grid/2.4.0/raw/helpers/post_save.php
- Modified: 2017-04-20T07:39:56+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/wp-ultimate-post-grid/2.4.0/code/helpers/post_save.php#L10-L20`.

```php
<?php

class WPUPG_Post_Save {

    public function __construct()
    {
        add_action( 'save_post', array( $this, 'save' ), 10, 2 );
        add_action( 'edit_attachment', array( $this, 'save_attachment' ) );
    }

    public function save_attachment( $post_id )
    {
        $post = get_post( $post_id );
        $this->save( $post_id, $post );
    }

    public function save( $post_id, $post )
    {
        if( $post->post_type != WPUPG_POST_TYPE )
        {
            if ( !isset( $_POST['wpupg_nonce'] ) || !wp_verify_nonce( $_POST['wpupg_nonce'], 'grid' ) ) {
                return;
            }

            // Basic metadata
            $fields = array(
                'wpupg_custom_link',
                'wpupg_custom_link_behaviour',
                'wpupg_custom_image',
                'wpupg_custom_image_id',
            );

            foreach ( $fields as $field )
            {
                $old = get_post_meta( $post_id, $field, true );
                $new = isset( $_POST[$field] ) ? $_POST[$field] : null;

                // Update or delete meta data if changed
                if( isset( $new ) && $new != $old ) {
                    update_post_meta( $post_id, $field, $new );
                } elseif ( $new == '' && $old ) {
                    delete_post_meta( $post_id, $field, $old );
                }
            }
        }
    }
}
```
