I’ve read over it a few times and I think I’m starting to understand that it’s an old example of how to create a new post before the introduction of the new_post
argument.
My comment is just about the documentation I don’t need assistance with my code. What is the purpose of the if( $post_id != 'new' )
part of the example in the docs?
Also worth noting that the priority would have to be 4 in order for new_post
to still be available.
I think I figured this out, it requires 2 filters and leverages the “acf_form” variable which is passed by the front-end form on validation.
1)
// Allow empty values in admin (verify acf_screen == acf_form because this is set in the frontend form but not the admin
// and both are technically is_admin() since the action takes place in the backend
add_filter('acf/validate_value/key=field_123', 'custom_vehicle_submission_required_fields', 10, 5);
function custom_vehicle_submission_required_fields($valid, $value, $field, $input) {
if (is_admin() && acf_request_arg( '_acf_screen', false ) !== 'acf_form') {
// valid is set to false if the value is empty, but allow 0 as a valid value
if ( empty( $value ) && ! is_numeric( $value ) ) {
$valid = true;
}
}
return $valid;
}
2)
// Do not add the 'required' attribute on the field in the admin
add_filter('acf/prepare_field/key=field_123', 'custom_vehicle_submission_required_fields2');
function custom_vehicle_submission_required_fields2($field) {
if (is_admin()) {
$field['required'] = false;
}
return $field;
}
Discovering now that these solutions cause problems with validation messages. The required
attribute gets set on the frontend field but since the validation technically happens in the “admin” – custom messages are not applied and we just see the default browser message…
You can re-order items using flexbox. Try applying this CSS to the .acf-input
element:
display:flex;
flex-direction: column-reverse;
Old one but here’s a quick way to do this:
add_filter( 'acf/load_attachment', 'custom_load_attachment', 10, 3);
function custom_load_attachment ($response, $attachment, $meta){
if ($response['type'] == 'image'){
$response['icon'] = $response['sizes']['thumbnail'];
}
return $response;
}
Actually scratch that, acf_load_field
is too powerful and actually prevents you from setting the fields to be required anywhere.
My solution was to set the fields to be NOT required and then make them required only on the frontend form, like so:
add_filter('acf/load_field/key=field_XYZ', 'custom_field_required_fields');
function custom_field_required_fields($field) {
if (!is_admin()) {
$field['required'] = true;
}
return $field;
}
Since this was the first hit in my searching, the correct answer is acf/load_field
as described here: https://support.advancedcustomfields.com/forums/topic/setting-required-to-false-inside-acfprepare_field-doesnt-have-any-effect/.
Here’s a quick bit of SASS code to make it behave horizontally:
.acf-taxonomy-field{
ul.acf-checkbox-list{
&.acf-bl {
> li {
float: left;
margin-right: 20px;
}
}
}
}
Just came here to say the same thing!
This was helpful, thank you.
Although I don’t think that the original issue is still relevant (the select2 search field searches through sub-terms just fine as far as I can tell), here is a method to remove the hyphen from a specific taxonomy field. This is useful in my case where I am creating a select list with child terms only, and it’s cleaner to have them listed without the hyphen:
add_filter( 'acf/fields/taxonomy/result', 'xyz_remove_hyphen_from_tax_terms', 10, 4);
function xyz_remove_hyphen_from_tax_terms ($title, $term, $field, $post_id){
if ($field['key'] == 'field_123'){ // Your custom field key
$title = str_replace('- ', '', $title);
}
return $title;
}
I figured out how to do this but do not recommend it. A better way would be to add an “other” option which reveals a text field and then use the acf/save_post
action to add the content of the text field as a new taxonomy term.
Here is the process to solve the originally asked problem. Start by creating a user named “Website Visitor” or similar with Subscriber-level access. Then integrate the following code.
// Allow all users, even non-signed in users to manage categories/taxonomies
add_filter( 'user_has_cap', 'custom_allow_all_edit_tax');
function custom_allow_all_edit_tax($allcaps){
$allcaps['manage_categories'] = true;
return $allcaps;
}
// Set the user to your manually created "Website Visitor" user with Subscriber-level access
add_action('after_setup_theme', 'custom_set_user_website_visitor');
function custom_set_user_website_visitor(){
// You will want to check if the user is not logged in already before modifying the user here
wp_set_current_user(#); // Your Website Visitor's user number
}
This seems like a bug. If I add a random submit button outside of the acf_form() somewhere on the page it submits the acf form.
I’m trying to do the same thing, but have a different problem I’d like to solve before digging into the capabilities. When I click the “add new taxonomy” plus button I get a popup with a form to add a new one, but when I submit that form, it also submits the main “new post” form on the page. Any ideas?
A simple solution if the goal is to keep templates clean would be to create a helper function like
function get_image_field_src_by_size($field_name, $size){
$image_id = get_field($field_name);
$image = wp_get_attachment_image_src($image_id, $size);
return $image[0];
}
<div style="background-image:url(<?php echo get_image_field_src_by_size('image_field_name', 'image_size_name') ?>)"></div>
Or, without a function and just a bit uglier inline
<div style="background-image:url(<?php echo wp_get_attachment_image_src(get_field('image_field_name'), 'image_size_name')[0] ?>)"></div>
My issue was resolved by changing the priority of some other actions so that the options fields were available. Specifically I had my acf_add_options_page
calls inside the init
hook instead of the acf/init
hook, and the taxonomies referenced inside those functions were not yet registered, so I needed to change their registration priority as well.
I’m also having difficulty fetching ACF fields into the admin-new-order
email. I believe the issue is related to hook order when that particular email is sent (ACF is not loaded yet?).
A solution I came up for this is to add a class to the field and then use CSS to hide the edit/action buttons. e.g.
add_filter( 'acf/prepare_field', 'marc_acf_prepare_field');
function marc_acf_prepare_field($field){
$screen = get_current_screen();
// If we are editing a page in the admin
if (is_admin() && $screen->parent_base == 'edit'){
if (Whatever your condition for disabling the field is)
$field['disabled'] = true;
$field['readonly'] = true;
$field['wrapper']['class'] .= ' custom-class-disabled-field';
}
}
return $field;
}
.custom-class-disabled-field .acf-actions{
display:none !important;
}
For anyone coming across this in 2021 the solution is to set a textdomain via code like so:
add_action('acf/init', 'acf_l10n_textdomain_setting');
function acf_l10n_textdomain_setting()
{
acf_update_setting('l10n_textdomain', 'yourtextdomain');
}
Once this setting is defined it will automatically wrap them on export.
I think that you need to have another layer of while have_rows
looping inside using the name of the cloned field, rather than using get_fields()
which as you can see returns an array.
I noticed this as well. Since my custom fields are set up via code in the theme, the solution was to change the label temporarily, re-load the edit page, then change the label back to the original, reload the edit page, make the changes, and save. Seems like some kind of cache needs a kick in the butt.
Thanks James. Perhaps a note in the docs would be helpful to others in the same situation.
Actually it seems like the parent repeater field simply returns the number of subfields as well, no idea why it doesn’t work as described. I’m using ACF v4.4.8 w/ Repeater field add-on v1.1.1.
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!
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.