Ask your WordPress questions! Pay money and get answers fast! (more info)

Front end post Update button WordPress

  • SOLVED

I want to update a post's meta fields from the front end.
This can already be done via [[LINK href="http://www.wpquestions.com/question/show/id/2553"]]this [[/LINK]]answer, and it is working.

However, after i saved the new meta value using the above code, it is just writing the new meta to the post. What i need is that the post also needs to be <strong>Updated</strong>. Updated as in the same way when you click Update in the post back-end.

The reason for this is some meta in the back-end <strong>ONLY </strong> gets written to SQL when the <strong>publish_post</strong> hook is fired - when a post is Updated.

So i would like the above code to automatically trigger a post update. Or a new seperate code that only gives me a post Update button.

Answers (1)

2013-01-16

Arnav Joy answers:

which answer are you using , can you share code here


oomskaap comments:

The one in this link http://www.wpquestions.com/question/show/id/7885

Or i paste here. This part goes on the top of my single.php file:

<?php



if($_POST['update_me']=='yes'){



if(!wp_verify_nonce($_POST['drw_inventory'],'update_drw_postmeta')){

die('failed security check');

}



global $post;



$postid = $post->ID;



$data = $_POST['_dappcf_i_priceone'];



update_post_meta($postid,'price',$data);



}



?>




And this part is in the single.php file where i want it to appear:

<form method="post" action="">

<?php wp_nonce_field('update_drw_postmeta','drw_inventory'); ?>

<label>This is label</label>



<?php

global $post;

$post_id = $post->ID;

$priceone = get_post_meta($post_id,'price',true);

?>



<input type='text' name='_dappcf_i_priceone' value='<?php echo $priceone ?>' />

<input type="hidden" name='update_me' value="yes"/>

<input type='submit' value='save' />

</form>


Arnav Joy comments:

ok try this

<?php







if($_POST['update_me']=='yes'){







if(!wp_verify_nonce($_POST['drw_inventory'],'update_drw_postmeta')){



die('failed security check');



}







global $post,$wpdb;







$postid = $post->ID;







$data = $_POST['_dappcf_i_priceone'];







update_post_meta($postid,'price',$data);










$tablename="wp_cart66_products";

if($post->post_type == "post" && strlen( get_post_meta($post_ID, 'price', true))>0 )
{
$id = $wpdb->get_var("SELECT id FROM ".$tablename." WHERE id=".$post_ID);
$cny = get_post_meta($post->ID, 'price', true);



/*Shipping rate */
if( $cny < 50 )
$shipping = 14.97;

else if( $cny >= 50 && $cny < 200 )

$shipping = 22.59;

else if( $cny >= 200 && $cny < 250 )

$shipping = 24.59;

else if( $cny >= 250 && $cny < 300 )

$shipping = 26.60;


else if( $cny >= 300 )
$shipping = 29.27;

/*Exchange rate CNY to EURO */

$cny_to_euro = 0.124;
$euro = $cny * $cny_to_euro ;
$price = $euro + $shipping;
$price = number_format($price,2);
$data=array(
'id'=>$post_ID,
'item_number'=>get_post_meta($post->ID, 'scode', true),
'name'=>$post->post_title,
'price'=>$price,
'options_1'=>get_post_meta($post->ID, 'variations', true),
'shipped'=>'1',
);
$where = array("id" => $post_ID);
// Possible format values: %s as string; %d as decimal number; and %f as float.
$format=array( '%d', '%s', '%s', '%s', '%s', '%d');
$where_format = array( '%d' );
if($id>0){
// update
$wpdb->update( $tablename,$data, $where, $format, $where_format);
}else{

// insert
$wpdb->insert( $tablename,$data,$format);
}

//AddMetaPrice ( $post_ID ) ;
}

}


?>