How to save Custom Post Type in WordPress 3.7

How to save Custom Post Type in WordPress 3.7 09Jul, 2014

Brief Introduction:

When it comes to content management system, WordPress has taken a leap in this area through its diverse technologies. The major development to this was the custom post type mechanism. The evolution of this mechanism allowed the creation of variety of content.

Previous version:

Gained prominence in 2005, Custom Post types have grown popular with it’s latest technologies and flexibility. The popularity of custom post types gained momentum when WordPress 1.5 added support for static pages, creating the post_type database field.

The advanced WordPress functions made coding extremely easy for the developers. The wp_insert_post() function has been around since WordPress 1.0, so when the post_type field was implemented in 1.5, you could simply set the post_type ( Example post, page, link, nav_menu_itemorcustomposttype. Default is post.) value when inserting a post.

2.8 version came out with the register_post_type() function and some other helpful functions in the nightly builds, whereas their usage became possible in the 2.9 version. Some extraordinary built in functions of wordpress made it a full blown CMS. These features were so powerful that they discarded the need of extensive coding and hacking.

Revolution with WordPress version 3.7

Before version 3.7, ‘save_post’ hook was used to handle the posted data from different post types. Its drawback was that the function get called every time a post is saved of any post type. Therefore we had to apply different checks to call the function only when the data is posted from the required post type.

For example, if we want to save the custom post type ‘product’, then using save_post function we will check for post type and return, if the post type is not ‘product’.

add_action('save_post','save_post_callback');

function save_post_callback($post_id)

{

global $post;

if ($post->post_type != ‘product’)

{

// check post type return;

// return if its not the required post type

}

//if you get here then it’s your post type so do your thing….

// Save custom post type details

$old = get_post_meta($post_id, ‘shipment_price’, true)

$new = $_POST[‘shipment_price’];

if ($new && $new != $old)

{

update_post_meta($post_id, ‘shipment_price’, $new);

}

elseif (” == $new && $old)

{

delete_post_meta($post_id, ‘shipment_price’, $old);

}

}

WordPress 3.7 introduced a unique style of saving the custom post type data with the save_post_{$post_type} hook.
Now in the above example, we can save our custom post type only by using something like this:

add_action(‘save_post_product’,‘save_post_callback’);

function save_post_callback( $post_id )

{

// Save custom post type details

$old = get_post_meta($post_id, ‘shipment_price’, true);

$new = $_POST[‘shipment_price’];

if ($new && $new != $old)

{

update_post_meta($post_id, ‘shipment_price’, $new);

}

elseif (” == $new && $old)

{

delete_post_meta($post_id, ‘shipment_price’, $old);

}

}

Posted by: Akhil Latta / In: Wordpress and Tagged
Cam

Leave a Reply

Your email address will not be published. Required fields are marked *