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
|
Show 3 more comments
1 Answer
Reset to default 0So 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.
die('Validation error');
toreturn;
? – Sally CJ Commented Oct 2, 2019 at 17:29update_post_meta()
call? And how are you calling thesave_metabox_details()
- is it hooked tosave_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