最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Advanced Custom Fields conflicting with custom plugin when saving custom field

programmeradmin3浏览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.

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);
            }
        }

    }

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.

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);
            }
        }

    }
Share Improve this question asked Oct 2, 2019 at 16:52 IsaacIsaac 432 bronze badges 8
  • 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 Commented Oct 2, 2019 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 Commented Oct 2, 2019 at 17:08
  • What happens when you change the die('Validation error'); to return; ? – Sally CJ Commented Oct 2, 2019 at 17:29
  • Nothing, just get the same validation error – Isaac Commented Oct 2, 2019 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 Commented Oct 2, 2019 at 18:53
 |  Show 3 more comments

1 Answer 1

Reset to default 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.

发布评论

评论列表(0)

  1. 暂无评论