Home › Forums › Backend Issues (wp-admin) › Custom location rules for attachment modals
I’ve succesfully extended ACF (latest version) custom location rules to hide a custom field on attachment pages based on media type.
The following field rules are applied:
AND
The rules hide the custom field on attachment pages where wp_attachment_is_image() returns true. However, the custom rule is not applied on media modals (e.g. when clicked on Edit file in an ACF field with type: Image). Effectively, it only works in the media library.
The first rule (ACF native) is able to control custom field display in media modals. What am I missing?
The code that adds the custom field rule:
add_filter('acf/location/rule_types', 'acf_location_rules_types');
function acf_location_rules_types( $choices )
{
$choices[__("Other",'acf')]['mediatype'] = __("Media Type",'acf');
return $choices;
}
add_filter('acf/location/rule_values/mediatype', 'acf_location_rules_values_mediatype');
function acf_location_rules_values_mediatype( $choices )
{
$choices[ 'image' ] = 'Image';
return $choices;
}
add_filter('acf/location/rule_match/mediatype', 'acf_location_rules_match_mediatype', 10, 3);
function acf_location_rules_match_mediatype( $match, $rule, $options )
{
if($rule['operator'] == "==")
{
return wp_attachment_is_image( $options["post_id"] );
}
elseif($rule['operator'] == "!=")
{
return !wp_attachment_is_image( $options["post_id"] );
}
return $match;
}
I have problem getting the post_id
$options array is all 0 (like default values)
any hint?
I’ve had the same problem myself. I was looking for a way to only show fields when the attachment was an image. Unfortunately I was never able to do so. What I did was I set my acf/location/rule_match/attachment filter to output information to a file. I tried outputting everything I could think of to this file to see if there was anything that would tell me the post id or any useful information so that I could show the images on the WP modals with no luck.
Anyway, if someone else would like to try this and see if they can come up with anything that I missed, this is the code I put into my filter, if outputs stuff into a txt file in the same folder as the file with the function in it.
add_filter('acf/location/rule_match/attachment', array($this, 'match_attachment_image'), 20, 3);
function match_attachment_image($match, $rule, $options) {
global $post;
$file = dirname(__FILE__).'/output.txt';
if (($handle = fopen($file, 'a')) !== false) {
ob_start();
echo "\r\n",'$match = '; var_dump($match);
echo "\r\n",'$rule = '; var_dump($rule);
echo "\r\n",'$options = '; var_dump($options);
echo "\r\n",'$post = '; var_dump($post);
echo "\r\n".'$_POST = '; var_dump($_POST);
echo "\r\n".'$_GET = '; var_dump($_GET);
fwrite($handle, ob_get_clean());
fclose($handle);
}
return $match;
}
I know this is an ancient thread, but I’m having exactly the same issues as pimschaaf, which is why I’m posting here. I’ve created a custom rule value named ‘Image’, and am trying to get it to match specifically for display/hiding purposes when editing a post with an image attachment. Absolutely nothing I’ve done so far has worked.
add_filter( 'acf/location/rule_values/ef_media', 'acf_location_rule_values_ef_media' );
add_filter( 'acf/location/rule_match/ef_media', 'acf_location_rule_match_ef_media', 10, 3 );
function acf_location_rule_values_ef_media ( $choices ) {
$choices['image'] = 'Image';
return $choices;
}
function acf_location_rule_match_ef_media ( $match, $rule, $options ) {
$id = get_the_ID();
if ( $rule['param'] = 'post_type' && $rule['value'] = 'attachment' ) {
if( $rule['operator'] === "==" ) {
$match = wp_attachment_is_image( $id );
} elseif ( $rule['operator'] === "!=" ) {
$match = !wp_attachment_is_image( $id );
}
} else {
$match = false;
}
return $match;
}
Has this issue really not been resolved?
The topic ‘Custom location rules for attachment modals’ is closed to new replies.
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 Privacy Policy. If you continue to use this site, you consent to our use of cookies.