Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

wp_update_post_meta example WordPress

  • SOLVED

I'm looking for a working update_post_meta example.

What I want to do is display post meta in an input field and when a user clicks submit trigger wp_update_post();.

Answers (4)

2011-06-29

Denzel Chia answers:

Hi,

What you are looking for is actually the whole example of a post meta box,
with reference to this WordPress Codex, http://codex.wordpress.org/Function_Reference/add_meta_box

I will use that as an example for you, by adding update_post_meta() to the end of the script.
Please put the below codes in your functions.php. You may need to change the HTML label.


<?php
/* Define the custom box */

// WP 3.0+
// add_action( 'add_meta_boxes', 'myplugin_add_custom_box' );

// backwards compatible
add_action( 'admin_init', 'myplugin_add_custom_box', 1 );

/* Do something with the data entered */
add_action( 'save_post', 'myplugin_save_postdata' );

/* Adds a box to the main column on the Post and Page edit screens */
function myplugin_add_custom_box() {
add_meta_box(
'myplugin_sectionid',
__( 'My Post Section Title', 'myplugin_textdomain' ),
'myplugin_inner_custom_box',
'post'
);
add_meta_box(
'myplugin_sectionid',
__( 'My Post Section Title', 'myplugin_textdomain' ),
'myplugin_inner_custom_box',
'page'
);
}

/* Prints the box content */
function myplugin_inner_custom_box() {

// Use nonce for verification
wp_nonce_field( plugin_basename( __FILE__ ), 'myplugin_noncename' );

// The actual fields for data entry
echo '<label for="myplugin_new_field">';
_e("Description for this field", 'myplugin_textdomain' );
echo '</label> ';

global $post;
$postid=$post->ID;
$value = get_post_meta($postid,'meta_key',true);

echo "<input type='text' id='myplugin_new_field' name='myplugin_new_field' value='$value' size='25' />";
}

/* When the post is saved, saves our custom data */
function myplugin_save_postdata( $post_id ) {
// verify if this is an auto save routine.
// If it is our form has not been submitted, so we dont want to do anything
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
return;

// verify this came from the our screen and with proper authorization,
// because save_post can be triggered at other times

if ( !wp_verify_nonce( $_POST['myplugin_noncename'], plugin_basename( __FILE__ ) ) )
return;


// Check permissions
if ( 'page' == $_POST['post_type'] )
{
if ( !current_user_can( 'edit_page', $post_id ) )
return;
}
else
{
if ( !current_user_can( 'edit_post', $post_id ) )
return;
}

// OK, we're authenticated: we need to find and save the data

$mydata = $_POST['myplugin_new_field'];

global $post;
$post_id = $post->ID;

update_post_meta($post_id,'meta_key',$mydata);
}
?>


Hope there is no syntax error!

Thanks.
Denzel


Matt Taylor comments:

Hi Denzel,

This will only work on the backend right?


Denzel Chia comments:

Hi,

Yes, this will only work in the back end.
It helps, if initially you stated that you want it to work on the front end.
Is there any other requirements that we need to know?

Thanks.
Denzel


Matt Taylor comments:

I appologize. I thought that was in there but now I see it is not explicit. There are mo other requirements. I'm just looking to update pm on the frontend like the attached media shows.


Denzel Chia comments:

Hi,

No apologize needed.
I think Alessendro's answer is what you need.

Thanks.
Denzel

2011-06-29

Alessendro answers:

Assuming that your form is in your single.php
The below is the form code that will post back to single.php
by leaving action blank, it will post back to itself.


<form method="post" action="">
<?php wp_nonce_field('name_of_my_action','name_of_nonce_field'); ?>
<label>This is label</label>
<input type='text' name='input1' value=' ' />
<input type='submit' value='save' />
</form>


Put this processing code at the beginning of single.php or header.php


<?php
if ( empty($_POST) || !wp_verify_nonce($_POST['name_of_nonce_field'],'name_of_my_action') )
{ //if fail nonce check, exit script
exit;
}
else
{
global $post;
$postid = $post->ID;
$data = $_POST['input1'];
update_post_meta($postid,'metakey',$data);

}
?>



To use the post meta data else where on the same template.


<?php
global $post;
$id = $post->ID;
$re_data = get_post_meta($id,'metakey',true);
echo $re_data;
?>



Alessendro comments:

To Matt,

Have you tried my codes? Is it working for you?

To Denzel,

Thank you.


Matt Taylor comments:

Alessendro, I will be able to try it out in about 90 minutes. The logic is exactly what I'm looking for though. Thanks!


Matt Taylor comments:

Here is your code, modified for the term I would use:

if ( empty($_POST) || !wp_verify_nonce($_POST['drw_inventory'],'update_drw_postmeta') )
{ //if fail nonce check, exit script
exit;
}
else
{
global $post;
$postid = $post->ID;
$data = $_POST['_dappcf_i_priceone'];
update_post_meta($postid,'metakey',$data);
}


<form method="post" action="">
<?php wp_nonce_field('update_drw_postmeta','drw_inventory'); ?>
<label>This is label</label>
<input type='text' name='_dappcf_i_priceone' value='<?php echo $priceone ?>' />
<input type='submit' value='save' />
</form>


With the function as-is nothing is being displayed on the page, it is a white screen. if I take out the <em>if ( empty($_POST) ||</em> section the page template will display but any changes I make to the form are not saving.


Alessendro comments:

Sorry,

Please try these.
Rewritten and tested.

On the top of single.php, put the following.

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

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

global $post;

$postid = $post->ID;

$data = $_POST['_dappcf_i_priceone'];

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

}
?>



For your form below,



<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,'metakey',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>



After submit, you log into admin and see the custom field of that particular post,
you will find a name "metakey" and value.

Thanks.


Alessendro comments:

Sorry,

More code improvement for processing part. Form code remains the same.


<?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,'metakey',$data);

}

?>


Matt Taylor comments:

Thanks Alessendro!

2011-06-29

AdamGold answers:

Should be something like:

<?php
global $post;
if( isset($_POST['submit_meta']) ) {
if( ! empty($_POST['change_meta']) ) {
update_post_meta($post->ID, 'your_meta', $_POST['change_meta']);
}
}
?>
<form method="post" action="">
<input type="text" name="change_meta" value="" />
<input type="submit" name="submit_meta" value="Submit" />
</form>


This should be used in a post page.


Matt Taylor comments:

There should be a nonce and a sanitize data check correct?

2011-06-29

Maor Barazany answers:

Do you want to update_post_meta, or to trigger the action hook of wp_update_post?


Matt Taylor comments:

Will update_post trigger update_post_meta?


Maor Barazany comments:

No.
update_post function will update regular post fields.
update_post_meta - will update custom fields.


You may use an action hook when updating a post to trigger and activate your function. Something like -

add_action('wp_update_post' 'my_function');
function my_function($post_id) {
update_post_meta('$post_id, 'meta_key', 'meta_value');
}


your meta_value will be derived from the value stored in the $_POST['filed'] of your form.


Matt Taylor comments:

I need to update post_meta via a save button on the front end.