I'm really stuck with that. I need to save (update) some $i1, $i2, $i3 metafields of custom post by clicking on the a#save link while editing the post. The custom metafields works fine with default Update button, but I'm not sure how to do that via ajax.
<strong>1 - In first I've registered the custom type post.</strong>
<strong>2 - Then I've created a metafields:</strong>
// METABOXES
function admin_init(){
add_meta_box('prodInfo-meta', 'Product Options', 'meta_options', 'product', 'side', 'low');
}
add_action('admin_init', 'admin_init');
function save_product(){
global $post;
$custom_meta_fields =
array(
'i1',
'i2',
'i3'
);
foreach( $custom_meta_fields as $custom_meta_field ):
if(isset($_POST[$custom_meta_field]) && $_POST[$custom_meta_field] != ""):
update_post_meta($post->ID, $custom_meta_field, $_POST[$custom_meta_field]);
endif;
endforeach;
}
add_action('save_post', 'save_product');
<strong>3 - Get data</strong>
// START META OPTIONS FUNCTION
function meta_options(){
global $post;
$custom = get_post_custom($post->ID);
$i1 = $custom['i1'][0];
$i2 = $custom['i2'][0];
$i3 = $custom['i3'][0];
<strong>4 - Call the action by clicking</strong>
var j = jQuery.noConflict();
j('a#save').click(function() {
j.ajax({
url: "/wp-admin/admin-ajax.php",
type: 'POST',
async: true,
cache: false,
dataType: 'json',
data: {
action: 'IM-NOT-SURE-WHAT-HERE'
},
success: function(){
alert('Saved');
}
});
return false;
});
<strong>5 - and handler</strong>
// AJAX HANDLER
function my_function() {
IM-NOT-SURE-WHAT-HERE
}
add_action('wp_ajax_SOMETHING_HERE', 'my_function');
Probably something else.
I will appreciate your help. Regards.
Christianto answers:
Hi,
Please try this..
The action by click..
var j = jQuery.noConflict();
j('a#save').click(function() {
var formValues = $("#YOUR_FORM_ID").serialize();
var data = { type: 'save', action: 'iajax_save', data: formValues }
j.post( url: ajaxurl, data: data, function(message){
alert('Saved');
});
return false;
});
Change YOUR_FORM_ID to your form id..
And php handler
// AJAX HANDLER
function iajax_save_function() {
if ( !current_user_can('edit_theme_options') )
die('-1');
global $post;
if($_POST['type'] == 'save'){
$data = $_POST['data'];
parse_str($data, $saved);
$custom_meta_fields =
array(
'i1',
'i2',
'i3'
);
foreach( $custom_meta_fields as $custom_meta_field ):
if(isset($saved[$custom_meta_field]) && $saved[$custom_meta_field] != ""):
update_post_meta($post->ID, $custom_meta_field, $saved[$custom_meta_field]);
endif;
endforeach;
echo 'save_success';
die;
}
}
add_action('wp_ajax_iajax_save', 'iajax_save_function');
Thanks
Igor comments:
Hi Christianto,
Many thanks for your reply, but JS script get back an error. Could you take a look an attachment (PHP file) please?
Thank you
Christianto comments:
Sorry I forgot about noConflict();
try this..
var j = jQuery.noConflict();
j('a#save').click(function() {
var formValues = j("#YOUR_FORM_ID").serialize();
var data = { type: 'save', action: 'iajax_save', data: formValues }
j.post( url: ajaxurl, data: data, function(message){
alert('Saved');
});
return false;
});
Igor comments:
Error: Expected ')'
Probaby the some ')' symbol losted. Could you modify attachment file for me please? http://wpquestions.com/uploads/Igor_phpqE9J4m.zip
Thank you
Christianto comments:
sorry the js should be
var j = jQuery.noConflict();
j('a#save').click(function() {
var formValues = j("#YOUR_FORM_ID").serialize();
var data = { type: 'save', action: 'iajax_save', data: formValues }
j.post( ajaxurl, data, function(message){
alert('Saved');
});
return false;
});
Christianto comments:
This is the file..
I hope it works..