1

I have a custom content type called cards.

I have 3 custom meta fields called:

my_cards_activity (select field type; options 0 and 1)

my_cards_user (select type; many options)

my_cards_datetime (text field type; yyyy-mm-dd hh:mm format)

When I press SAVE button I want to get the value of the my_cards_activity field (value can be 0 or 1) and if the value is 0 then change my_cards_datetime and my_cards_user to empty value and only after the change save it all. If it is 1 then do nothing.

How to do such before save thing? How should my code in functions.php look like?

3
  • 1
    Use the hook save_post see here for a ton of examples and put an if inside the function that do the save.
    – gmazzap
    Aug 22, 2013 at 11:29
  • @G.M. How do I limit custom save_post function only for a certain content type? In my case cards?
    – Derfder
    Aug 22, 2013 at 11:38
  • @G.M. Should I use add_filter ? Could you provide an example, please?
    – Derfder
    Aug 22, 2013 at 11:39

1 Answer 1

1

Use the hook save_post and put an if inside the function that do the save after the 'normal' save meta fields routine.

add_action( 'save_post', 'this_is_the_function_name' ); 

function this_is_the_function_name( $post_id ) { 

    // No auto saves 
    if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return; 

    // make sure the current user can edit the post 
    if( ! current_user_can( 'edit_post' ) ) return;

    // assure the post type
    if ( ! get_post_type($post_id) == 'cards' ) return;

    // assure the data are sended by the form
    if (
       ! isset($_POST['my_cards_activity']) ||
       ! isset($_POST['my_cards_datetime']) || ! isset('my_cards_user')
    ) return;

    $activity = $_POST['my_cards_activity'];

    // and now the simply logic
    if ( $activity == 1 ) {
        $datetime = $_POST['my_cards_datetime'];
        $user = $_POST['my_cards_user'];
    } else {
        $datetime = '';
        $user = '';    
    }

    // save data as array
    $meta_data = compact("activity", "datetime", "user");
    update_post_meta($post_id, 'card_data', $meta_data);

    // if you want can save data as 3 different meta fields in this case
    // delete the previous 2 lines and uncommente the following
    // update_post_meta($post_id, 'card_activity', $activity);
    // update_post_meta($post_id, 'datetime', $datetime);
    // update_post_meta($post_id, 'user', $user);        

} 

If in your metabox have you setted a nonce field check for it before save.

See Codex for:

4
  • Hm for some reason everything works, except the update_post_meta() function. I have tried to save/upadet only one field update_post_meta($post_id, 'datetime', $datetime); to eliminate errors. But still it seems not be working. Do you know can I var_dump all the meta fields of a content type?
    – Derfder
    Aug 22, 2013 at 13:44
  • Function is correct. Maybe you have to chack the name of the inputs <input name="LOOK HERE" and see if they are equal to what is inside $_POST['LOOK HERE']. For var_dump, just var_dump($var); and then wp_die(); to stop execution and let you read.
    – gmazzap
    Aug 22, 2013 at 15:06
  • no if i print_r and die every variable before update_post_meta it shows everything correct, the 0 and 1 and the if condition too. The problem is that it's not saved for some reason ;(. $post_id is correct too. The only problem could be in the name of my metabox.
    – Derfder
    Aug 22, 2013 at 16:10
  • OK, thanks. It's working except for the datetime field, but that's the problem of a meta box plugin I am using wordpress.stackexchange.com/questions/111248/…
    – Derfder
    Aug 22, 2013 at 21:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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