0

I have meta box in my custom post type where I receive data from a front end form. The issue is that somewhere in this functions, something goes wrong and the data changed in admin edit panel does not apply to the post (update_post_meta does not get his job done I guess) Here is my code:


// HANDLER FOR THE FORM AND SAVING THE RIGHT DATA
<?php elseif(isset($_POST['test'])){ 
     $location = $_POST['_location'];
     $new_post = array(
        'post_title'    =>   $_POST['title'],
        'post_content'  =>   $_POST['description'],
        'post_status'   =>   'pending',          
        'post_type' =>   'eventstest'       
        );

        $pid = wp_insert_post($new_post);

        add_post_meta($pid, 'location', $location, true);
}

do_action('save_post', 'eg_save_events_meta', 1, 2); // save the custom fields
do_action('wp_insert_post', 'wp_insert_post'); 


// ADD THE META BOX
function add_events_metaboxes() { 
   add_meta_box('wpt_events_location', 'Event Location', 'wpt_events_location', 'eventstest', 'side', 'default');}

//DISPLAING META BOXES
function wpt_events_location() { 
global $post;

echo '<input type="hidden" name="eventmeta_noncename" id="eventmeta_noncename" value="' . 
wp_create_nonce( plugin_basename(__FILE__) ) . '" />';

$_location = get_post_meta($post->ID, 'location', true);

echo '<input type="text" name="_location" value="' . $_location  . '" />';}


//SAVING AND UPDATE META FIELDS
function eg_save_events_location( $post_id ){

if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) 
    return;
if ( !wp_verify_nonce( $_POST['myplugin_noncename'], plugin_basename( __FILE__ ) ) )
    return;
if ( 'page' == $_POST['post_type'] ) 
{
    if ( !current_user_can( 'edit_page', $post_id ) )
    return;
}
else
{
    if ( !current_user_can( 'edit_post', $post_id ) )
    return;
}

$mydata = $_POST['_location'];
update_post_meta($post->ID, 'location', $mydata);}

add_action('save_post', 'eg_save_events_location', 1, 2); // save the custom fields once again in functions.php too
  • You should probably use $post_id in update_post_meta instead of $post->ID. – larsemil Aug 13 '14 at 8:06
  • That does not work too. – worldwildwebdev Aug 13 '14 at 8:09
  • Have you tried logging so you know appropriate functions are being run? – larsemil Aug 13 '14 at 8:31
  • I know that data is beeing displayed in that metabox in admin editor. The only problem is when trying to update that data and nothing's happen. Did I answer the question correctly? – worldwildwebdev Aug 13 '14 at 8:43
  • Is there something with getting the post ID with function hooked to 'edit-post' action? And then 'update-post-meta' on 'save- post' action again? – worldwildwebdev Aug 13 '14 at 10:19
0

After I removed all the wp_nonce fields everything is working slightly. As far as I can remember, their job is to prevent sending requests from undesireable places and some actions. I guess I can't make a proper nonce verification.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.