Hi!
I want to create a field editable only by the admin on a real estate theme (Houzez on theme forest).
The user should be able to submit a property trough front end, but on the same type of post, a field should be only editable by the admin, not the user.
The field should be visible to the user, logged or not logged.
Is this possible?
If so, how?
Im using ACF pro
https://www.advancedcustomfields.com/resources/acf-prepare_field/
// replace with your field key
// or adjust hook as described in above doc
add_filter('acf/prepare_field/key=field_XYZ123', 'edit_admin_only');
function edit_admin_only($field) {
$readonly = true;
if ((is_user_logged_in() && current_user_can('administrator')) ||
(is_user_logged_in() && !$field['value'])) {
// user is logged in and is an admin
// or user is logged in and the field has no value
$readonly = false;
}
$field['readonly'] = $readonly;
return $field;
}
Running on a MAMP server – Local.
I have tried this code using the key for the field I want to control but, with only changing the key and then logging in with a user not an admin, when I try to edit the record it casts and error. Admin has no issues.
My field is a number and called “Stock id”
Field name: vehicle_stock_id
Key: field_5f115aa7bf4ef
Fatal error: Uncaught Error: Call to undefined function is_user_logged_id() in /private/var/tmp/8-read-only-stock-id.php:10 Stack trace: #0 /Users/john/Sites/wp-agent-kaz-20/wp-includes/class-wp-hook.php(307): edit_admin_only(Array) #1 /Users/john/Sites/wp-agent-kaz-20/wp-includes/plugin.php(233): WP_Hook->apply_filters(Array, Array) #2 /Users/john/Sites/wp-agent-kaz-20/wp-content/plugins/advanced-custom-fields-pro/includes/acf-hook-functions.php(109): apply_filters_ref_array(‘acf/prepare_fie…’, Array) #3 /Users/john/Sites/wp-agent-kaz-20/wp-includes/class-wp-hook.php(307): _acf_apply_hook_variations(Array) #4 /Users/john/Sites/wp-agent-kaz-20/wp-includes/plugin.php(189): WP_Hook->apply_filters(Array, Array) #5 /Users/john/Sites/wp-agent-kaz-20/wp-content/plugins/advanced-custom-fields-pro/includes/acf-field-functions.php(556): apply_filters(‘acf/prepare_fie…’, Array) #6 /Users/john/Sites/wp-agent-kaz-20/wp-content/plugins/advanced-custom-fields-pro/includes/acf-field-functions.php(648): acf_prepare_field(Array) #7 /U in /private/var/tmp/8-read-only-stock-id.php on line 10
Log back in as admin user, no issues but the record remains “locked” and I have to take over.
Wordpress version 5.9
I’m a little bit late to the party, but the previous post is obvious, I hope @jmcbade has solved it meanwhile.
The Fatal error says it all:
Fatal error: Uncaught Error: Call to undefined function is_user_logged_id()
It’s probably a mistyped function name, maybe it should’ve been is_user_logged_in?
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
❓Ever wondered when and why ACF uses JSON instead of the database? Check out our summary of the most recent session of ACF Chat Friday for the answer, and make sure to register for the next session.
— Advanced Custom Fields (@wp_acf) February 23, 2023
👉 https://t.co/3UtvQbDwNmhttps://t.co/wfFEVcXVKc
© 2023 Advanced Custom Fields.
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.