0

I have custom field data that I would like to modify on its way to the database.

wp_insert_post_data filter seems to be the closest thing I can find to achieving this. I dumped the $data and $postarr values but I don't see how I can access my custom field data as those fields aren't in the dump.

Can wp_insert_post_data be used to save custom field data, and if not, what is the preferred filter / practice?

EDIT: additional detail:

I have a front end form that asks for a user's company website address. If the user simply types in 'example.com' then the target for that website in his post defaults to mywebsite.com/example.com instead of opening up example.com.

If http:// is not part of the text they enter, then I want to add that to it right before it saves. There is javascript to help with validation, but those who have it turned off get by it.

1

You can access the raw $_POST data from the wp_insert_post_data filter, but there are probably better ways to intercept that data.

  • save_post or similar hook would probably work
  • As you are dealing with post_meta, update_post_meta uses update_metadata which provides the update_{$meta_type}_meta and updated_{$meta_type}_meta filters. Those may be the best for you but your question is shy on detail so I can't be sure.

Based on the edit, use the update_{$meta_type}_meta filter. Your question is still light on detail but the following filter should dump all of the data so you can at least see what you are dealing with:

add_filter(
  'update_post_meta',
  function($meta_type,$object_id,$meta_key,$meta_value,$prev_value) {
    var_dump($meta_type,$object_id,$meta_key,$meta_value,$prev_value);
    die;
  },
  10,5
);

Note: that will break your site but not permanently. It is purely for debugging/development. It is just to give you a glance at the data that comes through the filter.

  • Thanks for the quick response! I added a bit more detail that helps clarify what I'm doing. – user658182 Jan 25 '14 at 16:12
  • the save_post was just what I was looking for. I was able to get the meta fields for my post @ $post->ID and then manipulate them however, and then use update_post_meta in the callback of the save_post action to do just what I wanted. Thanks for the direction! – user658182 Jan 26 '14 at 18:16

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.