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?
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. :)
Related
I'm building a website on Wordpress with Bedrock/Timber/ACF.
This site is restricted for a big major part of it.
There is 4-5 pages available for anonymous users (home, contact, login & legal/privacy policy pages).
I'm currently managed it with Timber routing with like:
Routes::map('/blog/page/:num/', function ($params) {
if (!is_user_logged_in()) {
wp_redirect(home_url().'/login');
exit();
}
$user = wp_get_current_user();
if ($user->roles[0]=="subscriber") {
$post_type="publish";
} else {
$post_type="any";
}
$query = 'order=ASC&orderby=title&paged='.$params['num'].'&post_status='.$post_type;
Routes::load('list-blog.php', $params, $query, 200);
});
However I don't know if it's the good way to do it because I can't use wordpress template hierarchy, on the admin side everytimes I want to create a new page I have to create the road...
So my solution is not flexible and hard to maintain...
Do you have some advices?
EDIT:
I almost removed all my routes by using wordpress template hierarchy. But I still have routes link to the login page because I don't want to have mysite.com/wp/login but mysite.com/login.
I would use array_key_exists instead of checking for the first var because there a situation that the subscriber role is second.
If you are using ACF you could try to add a checkbox field to your pages and query for the variable of the field.
https://www.advancedcustomfields.com/resources/query-posts-custom-fields/
Routes::map('/blog/page/:num/', function ($params) {
if (!is_user_logged_in()) {
wp_redirect(home_url().'/login');
exit();
}
$user = wp_get_current_user();
$post_type = array_key_exists("subscriber", $user->roles) ? "publish" : "any";
$query = 'order=ASC&orderby=title&paged='.$params['num'].'&post_status='.$post_type;
Routes::load('list-blog.php', $params, $query, 200);
});
I have being looking in internet how can I disable the option of edit profile in Wordpress for a user and I did't find a good way. The problem is that I want that the 95% of users can edit their profiles (address, telephone,...) but there is another 5% that I don't want them to have access to the profile section.
The best solution for me is to select manually the users that I want to disable but I also thought about create a role or a group of users to manage it.
There is no plugin for this and the only thing I have found is the following code but it is not working.
add_action('admin_init', 'user_profile_fields_disable');
function user_profile_fields_disable() {
global $pagenow;
// apply only to user profile or user edit pages
if ($pagenow!=='profile.php' && $pagenow!=='user-edit.php') {
return;
}
// do not change anything for the administrator
if (current_user_can('administrator')) {
return;
}
if (current_user_can('custom_role') {
add_action( 'admin_footer', 'user_profile_fields_disable_js' );
}
}
/**
* Disables selected fields in WP Admin user profile (profile.php, user-edit.php)
*/
function user_profile_fields_disable_js() {
?>
<script>
jQuery(document).ready( function($) {
var fields_to_disable = ['email', 'role'];
for(i=0; i<fields_to_disable.length; i++) {
if ( $('#'+ fields_to_disable[i]).length ) {
$('#'+ fields_to_disable[i]).attr("disabled", "disabled");
}
}
});
</script>
<?php
}
You have to substitude the "custom_role" with the name of the role I have assign to the user.
Is that code obsolete or bad? Any idea of how to solve it?
Thanks.
I am trying to create a sampling process , where a user need to register or login to view my sample pdf documents , and once they register the user should be given a role as a sampler .
I am not a pro at wordpress but am learning . Can u guys suggest me some plugin or ideas on how to do it , i am using wordpress WordPress 3.3.1 .Thanks
http://codex.wordpress.org/Function_Reference/is_user_logged_in
http://codex.wordpress.org/Function_Reference/wp_login_form
As per your latest question, you will need to use these in tandem with each other, and make sure to define your redirect within your argument array to make sure the user is directed back to the page from which they logged in.
FINAL UPDATE: Enter this in your functions.php file and utilize as you see fit. This will allow you to output information according to the actual role of your user. This is also assuming that your plugin is creating actual custom roles. If not, you will have to utilize the Add Role Function:
function get_user_role()
{
global $current_user;
$user_roles = $current_user->roles;
$user_role = array_shift($user_roles);
return $user_role;
}
Then in the template of the page you want:
<?php
if(is_user_logged_in())
{
switch(get_user_role())
{
case 'customer' :
//CUSTOMER DISPLAY
break;
case 'subscriber' :
//SUBSCRIBER DISPLAY
break;
default :
//OTHER TYPE OF USER DISPLAY
}
}
else
{
//NO LOGGED USER DISPLAY
}
?>
When my node form saves, I want to close the admin overlay and redirect to a custom URL that is stored with the node. hook_form_alter() is setting $form['#redirect'] but I think that would only work with no admin overlay.
I have never used it before myself, but i think you can call the overlay_close_dialog(...) function from your hook_submit
See http://api.drupal.org/api/drupal/modules--overlay--overlay.module/function/overlay_close_dialog/7 for more info
Kept googling my way here as well.. this is the working solution:
Inside your form:
$form['somebutton']['#submit'] = array('your_custom_callback');
Add a custom callback
function your_custom_callback($form, &$form_state) {
//redirect users to Drupal.org
$url = "http://drupal.org";
if (module_exists('overlay') && overlay_get_mode() == 'child') {
unset($_GET['destination']);
overlay_close_dialog($url, array('external' => TRUE));
$form_state['redirect'] = FALSE;
} else {
$form_state['redirect'] = $url;
}
}
Kept googling my way here and wanted to post a final solution that worked for me on Drupal 7 (got here thanks to jakraska's suggestion). First use hook_form_FORM_ID_alter() to hook into the specific form you want to alter
/**
* Implementation of hook_form_FORM_ID_alter()
*
**/
function mymodule_form_FORM_ID_alter(&$form, &$form_state) {
$form['#submit'][] = 'mymodule_callback';
}
Then write your callback that will render the page. In my case I simply wanted to close the overlay, so that's why I used the $form_state['redirect'] = FALSE;
function mymodule_callback(&$form, &$form_state) {
// Form API will re-render the current page and pass the redirect information to the overlay JavaScript
overlay_close_dialog();
// stay on the same page after all submit callbacks have been processed.
$form_state['redirect'] = FALSE;
}
If you want to redirect to another path,
See https://api.acquia.com/api/drupal/modules!overlay!overlay.module/function/overlay_form_submit/7 - I believe that should get you there.
Make sure to use $form_state['rebuild'] = TRUE; inside your hook_submit function, otherwise it would not close overlay after form is submitted.
For example, in the registration form, there is "Username" and the text field for it which has the input type="text" name="name" ....
I need to know how can I get the title from the input field's name.
I'm expecting a function like:
$title = get_title_for_element('name');
Result:
assert($title == 'Username'); // is true
Is there something like this in Drupal?
Thanks.
You have the form and the form state variables available to your validation function. You should use form_set_error() to set the error.
There is no function that I am aware of which will map from the values array to the form array. But it is not dificult to work it out. Understanding the form data structure is one of the key skills you need when building drupal.
In this case the form in question is generated (in a roundabout way) by user_edit_form, you can see the data structure in there.
$form['account']['name'] is the username field. and the array key for the title is '#title' as it will be in most cases for form elements.
You can do it in two different ways as I see it. Let's create a module called mycustomvalidation.module (remember to create the mycustomvalidation.info file also).
Note: The code below has not been tested, so you might have to do some minor adjustments. This is Drupal 6.x code by the way.
1) Using hook_user()
What you need is a custom module containing your own implementation of hook_user() http://api.drupal.org/api/function/hook_user/6.
<?php
function mycustomvalidation_user($op, &$edit, &$account, $category = NULL) {
if ($op == 'validate') {
// Checking for an empty 'profile_fullname' field here, but you should adjust it to your needs.
if ($edit['profile_fullname'] != '') {
form_set_error('profile_fullname', t("Field 'Fullname' must not be empty."));
}
}
}
?>
2) Using form_alter() and a custom validation function
Personally, I would go for this option because I find it cleaner and more "correct". We're adding a custom validation function to our profile field here.
<?php
function mycustomvalidation_form_alter(&$form, $form_state, $form_id) {
// Check if we are loading 'user_register' or 'user_edit' forms.
if ($form_id == 'user_register' || $form_id == 'user_edit') {
// Add a custom validation function to the element.
$form['User information']['profile_fullname']['#element_validate'] = array('mycustomvalidation_profile_fullname_validate');
}
}
function mycustomvalidation_profile_fullname_validate($field) {
// Checking for an empty 'profile_fullname' field here, but you should adjust it to your needs.
if ($field['#value'] != '') {
form_set_error('profile_fullname', t("Field %title must not be empty.", array('%title' => $field['#title']));
}
}
?>