Allow admin to bypass required fields in certain situations with ACF?
I've created an ACF frontend form using acf_form; these fields are added to the User record on the backend; however because this form has required fields this means the admin cannot make basic changes to the user on the backend unless the user has filled in this form.
So I'm wondering if it's possible in certain situations to allow the admin to bypass being required to fill in required fields and if so, how do I go about doing this?
wordpress advanced-custom-fields acfpro
add a comment |
I've created an ACF frontend form using acf_form; these fields are added to the User record on the backend; however because this form has required fields this means the admin cannot make basic changes to the user on the backend unless the user has filled in this form.
So I'm wondering if it's possible in certain situations to allow the admin to bypass being required to fill in required fields and if so, how do I go about doing this?
wordpress advanced-custom-fields acfpro
add a comment |
I've created an ACF frontend form using acf_form; these fields are added to the User record on the backend; however because this form has required fields this means the admin cannot make basic changes to the user on the backend unless the user has filled in this form.
So I'm wondering if it's possible in certain situations to allow the admin to bypass being required to fill in required fields and if so, how do I go about doing this?
wordpress advanced-custom-fields acfpro
I've created an ACF frontend form using acf_form; these fields are added to the User record on the backend; however because this form has required fields this means the admin cannot make basic changes to the user on the backend unless the user has filled in this form.
So I'm wondering if it's possible in certain situations to allow the admin to bypass being required to fill in required fields and if so, how do I go about doing this?
wordpress advanced-custom-fields acfpro
wordpress advanced-custom-fields acfpro
asked Dec 28 '18 at 12:59
BrettBrett
6,24739114220
6,24739114220
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
Ok, found a way to do this - this is a way to do it for the User screen, it may differ for other post types.
We not only have to disable server side validation, but also client side validation, to do this, we do something a little like this:
add_action('acf/input/admin_head', 'my_acf_admin_head');
function my_acf_admin_head() {
if (!function_exists('get_current_screen')) {
return;
}
// Get current page/screen
$screen = get_current_screen();
// Get current user
$user = wp_get_current_user();
if (is_object($screen) and is_a($screen, 'WP_Screen')) {
if (($screen->id == 'user-edit' or ($screen->id == 'user' and $screen->action == 'add')) and in_array('administrator', $user->roles)) {
?>
<script type="text/javascript">
window.acf.validation.active = false;
</script>
<?php
}
}
}
This will add some Javascript to any page that matches our qualifers to disable ACF client-side validation.
Now, to disable backend validation, we do something like this:
add_action('acf/validate_save_post', 'my_acf_validate_save_post', 10, 0);
function my_acf_validate_save_post() {
if (!function_exists('get_current_screen')) {
return;
}
// Get current page/screen
$screen = get_current_screen();
// Get current user
$user = wp_get_current_user();
if (is_object($screen) and is_a($screen, 'WP_Screen')) {
if (($screen->id == 'user-edit' or ($screen->id == 'user' and $screen->action == 'add')) and in_array('administrator', $user->roles)) {
// clear all errors so they can bypass validation for user data
acf_reset_validation_errors();
}
}
}
Note that because get_current_screen()
isn't always available, these methods do not support front end forms.
Also note that this code could definitely be improved to be a lot more DRY, but I will leave that up to you. :)
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53958999%2fallow-admin-to-bypass-required-fields-in-certain-situations-with-acf%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Ok, found a way to do this - this is a way to do it for the User screen, it may differ for other post types.
We not only have to disable server side validation, but also client side validation, to do this, we do something a little like this:
add_action('acf/input/admin_head', 'my_acf_admin_head');
function my_acf_admin_head() {
if (!function_exists('get_current_screen')) {
return;
}
// Get current page/screen
$screen = get_current_screen();
// Get current user
$user = wp_get_current_user();
if (is_object($screen) and is_a($screen, 'WP_Screen')) {
if (($screen->id == 'user-edit' or ($screen->id == 'user' and $screen->action == 'add')) and in_array('administrator', $user->roles)) {
?>
<script type="text/javascript">
window.acf.validation.active = false;
</script>
<?php
}
}
}
This will add some Javascript to any page that matches our qualifers to disable ACF client-side validation.
Now, to disable backend validation, we do something like this:
add_action('acf/validate_save_post', 'my_acf_validate_save_post', 10, 0);
function my_acf_validate_save_post() {
if (!function_exists('get_current_screen')) {
return;
}
// Get current page/screen
$screen = get_current_screen();
// Get current user
$user = wp_get_current_user();
if (is_object($screen) and is_a($screen, 'WP_Screen')) {
if (($screen->id == 'user-edit' or ($screen->id == 'user' and $screen->action == 'add')) and in_array('administrator', $user->roles)) {
// clear all errors so they can bypass validation for user data
acf_reset_validation_errors();
}
}
}
Note that because get_current_screen()
isn't always available, these methods do not support front end forms.
Also note that this code could definitely be improved to be a lot more DRY, but I will leave that up to you. :)
add a comment |
Ok, found a way to do this - this is a way to do it for the User screen, it may differ for other post types.
We not only have to disable server side validation, but also client side validation, to do this, we do something a little like this:
add_action('acf/input/admin_head', 'my_acf_admin_head');
function my_acf_admin_head() {
if (!function_exists('get_current_screen')) {
return;
}
// Get current page/screen
$screen = get_current_screen();
// Get current user
$user = wp_get_current_user();
if (is_object($screen) and is_a($screen, 'WP_Screen')) {
if (($screen->id == 'user-edit' or ($screen->id == 'user' and $screen->action == 'add')) and in_array('administrator', $user->roles)) {
?>
<script type="text/javascript">
window.acf.validation.active = false;
</script>
<?php
}
}
}
This will add some Javascript to any page that matches our qualifers to disable ACF client-side validation.
Now, to disable backend validation, we do something like this:
add_action('acf/validate_save_post', 'my_acf_validate_save_post', 10, 0);
function my_acf_validate_save_post() {
if (!function_exists('get_current_screen')) {
return;
}
// Get current page/screen
$screen = get_current_screen();
// Get current user
$user = wp_get_current_user();
if (is_object($screen) and is_a($screen, 'WP_Screen')) {
if (($screen->id == 'user-edit' or ($screen->id == 'user' and $screen->action == 'add')) and in_array('administrator', $user->roles)) {
// clear all errors so they can bypass validation for user data
acf_reset_validation_errors();
}
}
}
Note that because get_current_screen()
isn't always available, these methods do not support front end forms.
Also note that this code could definitely be improved to be a lot more DRY, but I will leave that up to you. :)
add a comment |
Ok, found a way to do this - this is a way to do it for the User screen, it may differ for other post types.
We not only have to disable server side validation, but also client side validation, to do this, we do something a little like this:
add_action('acf/input/admin_head', 'my_acf_admin_head');
function my_acf_admin_head() {
if (!function_exists('get_current_screen')) {
return;
}
// Get current page/screen
$screen = get_current_screen();
// Get current user
$user = wp_get_current_user();
if (is_object($screen) and is_a($screen, 'WP_Screen')) {
if (($screen->id == 'user-edit' or ($screen->id == 'user' and $screen->action == 'add')) and in_array('administrator', $user->roles)) {
?>
<script type="text/javascript">
window.acf.validation.active = false;
</script>
<?php
}
}
}
This will add some Javascript to any page that matches our qualifers to disable ACF client-side validation.
Now, to disable backend validation, we do something like this:
add_action('acf/validate_save_post', 'my_acf_validate_save_post', 10, 0);
function my_acf_validate_save_post() {
if (!function_exists('get_current_screen')) {
return;
}
// Get current page/screen
$screen = get_current_screen();
// Get current user
$user = wp_get_current_user();
if (is_object($screen) and is_a($screen, 'WP_Screen')) {
if (($screen->id == 'user-edit' or ($screen->id == 'user' and $screen->action == 'add')) and in_array('administrator', $user->roles)) {
// clear all errors so they can bypass validation for user data
acf_reset_validation_errors();
}
}
}
Note that because get_current_screen()
isn't always available, these methods do not support front end forms.
Also note that this code could definitely be improved to be a lot more DRY, but I will leave that up to you. :)
Ok, found a way to do this - this is a way to do it for the User screen, it may differ for other post types.
We not only have to disable server side validation, but also client side validation, to do this, we do something a little like this:
add_action('acf/input/admin_head', 'my_acf_admin_head');
function my_acf_admin_head() {
if (!function_exists('get_current_screen')) {
return;
}
// Get current page/screen
$screen = get_current_screen();
// Get current user
$user = wp_get_current_user();
if (is_object($screen) and is_a($screen, 'WP_Screen')) {
if (($screen->id == 'user-edit' or ($screen->id == 'user' and $screen->action == 'add')) and in_array('administrator', $user->roles)) {
?>
<script type="text/javascript">
window.acf.validation.active = false;
</script>
<?php
}
}
}
This will add some Javascript to any page that matches our qualifers to disable ACF client-side validation.
Now, to disable backend validation, we do something like this:
add_action('acf/validate_save_post', 'my_acf_validate_save_post', 10, 0);
function my_acf_validate_save_post() {
if (!function_exists('get_current_screen')) {
return;
}
// Get current page/screen
$screen = get_current_screen();
// Get current user
$user = wp_get_current_user();
if (is_object($screen) and is_a($screen, 'WP_Screen')) {
if (($screen->id == 'user-edit' or ($screen->id == 'user' and $screen->action == 'add')) and in_array('administrator', $user->roles)) {
// clear all errors so they can bypass validation for user data
acf_reset_validation_errors();
}
}
}
Note that because get_current_screen()
isn't always available, these methods do not support front end forms.
Also note that this code could definitely be improved to be a lot more DRY, but I will leave that up to you. :)
answered Dec 28 '18 at 17:38
BrettBrett
6,24739114220
6,24739114220
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53958999%2fallow-admin-to-bypass-required-fields-in-certain-situations-with-acf%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown