0

I'm writing a plugin for the practice and running into an issue where another plugin, Advanced Custom Fields is conflicting with my code. I have created a custom post type with it's own custom field. My save method works and the custom field data is saved if I disable ACF, however I am getting a validation error that I think comes from the acf plugin when trying to save/publish with the plugin active. See pic for the error message.

So I guess my question is, do I need to check if that plugin is active and use it's functions to handle this and if not do it the normal way, or am I doing something else wrong? Does every plugin that saves custom field data have to check if this plugin is active and handle it accordingly? That just doesn't seem right to me but this is my first time writing a plugin.

error message

Here is my save method:

public function save_metabox_details() {
        global $post;
        if(current_user_can('editor') || current_user_can('administrator') && isset($_POST['ipmyskills_nonce'])) {
            $nonce = $_POST['ipmyskills_nonce'];
            if( $nonce && !check_admin_referer( basename(__FILE__), 'ipmyskills_nonce' )) {
                die('Validation error');
            }
            if (isset($_POST['ipmyskills_input'])) {
                $sanitized_value = esc_html( sanitize_text_field( $_POST['ipmyskills_input'] ) );
                update_post_meta($post->ID, 'ipmyskills_input', $sanitized_value);
            }
        }

    }
  • Maybe try adding your initials to the beginning of your function because if there might be another function with that same name causing the problem. You could try bgs_save_metabox_details. – bsmith Oct 2 '19 at 16:57
  • No, that didn't do it. it's a class method. Shouldn't be an issue with the name of the method. – Isaac Oct 2 '19 at 17:08
  • What happens when you change the die('Validation error'); to return; ? – Sally CJ Oct 2 '19 at 17:29
  • Nothing, just get the same validation error – Isaac Oct 2 '19 at 18:04
  • Have you tried commenting out the line with the update_post_meta() call? And how are you calling the save_metabox_details() - is it hooked to save_post? But try an empty function.. function save_metabox_details() {} and see if the issue persists - if yes, then the problem is somewhere else.. – Sally CJ Oct 2 '19 at 18:53
0

So apparently I'm a bonehead. I was originally building this with Advanced Custom Fields plugin and doing it in the theme but decided to try and build my first plugin instead. I had created a custom field group and assigned it to the post type with ACF, so that's what was triggering the validation error. Once I deleted the field group everything works as expected.

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.