Support

Account

Home Forums Backend Issues (wp-admin) Custom Location Rules for Media files / Library

Unread

Custom Location Rules for Media files / Library

  • Have been struggling with setting up custom location rules for media types in the wordpress media library. I’m looking to add a location rule that allows me to display a field set for a media file, based on whether it it an audio file.

    I’ve looked at the documentation about this here :

    To go through the stages I have completed so far:

    1) have taken the Add Custom Types code example and swapped “user” for “media”. This shows up in the drop down menu as an option when selecting Location Rule types in the Edit Field Group page.

    Can anyone confirm that just because it shows up in the list, it is therefore a valid option. Or do i have to A) define “media” somewhere else or B) used a different term altogether for media files

    Presume this use of the term “media” is correct, I then tried to adjust the code example for section 3 Add Custom Values (I don’t need to edit section 2 “Add Custom Operators” as I would use the default option “value equals”)

    I looked at the basic files for wordpress and noticed in media-upload.php there was the following:

    @param string $type The default media upload type. Possible values include ‘image’, ‘audio’, ‘video’, ‘file’, etc. Default ‘file’

    So if anyone could tell me: am I right in presuming that the variable $type will allow me to choose between “image”, “audio”, “video” or “file” when selecting media in the location rules (this is what I eventually want to achieve)

    Trying to use the code example from the ACF documentation as follows:

    add_filter('acf/location/rule_values/user', 'acf_location_rules_values_user');
    function acf_location_rules_values_user( $choices )
    {
        $users = get_users();
    
        if( $users )
        {
            foreach( $users as $user )
            {
                $choices[ $user->data->ID ] = $user->data->display_name;
            }
        }
    
        return $choices;
    }

    I’ve swapped $user for either $media or $type to get the following:

    add_filter('acf/location/rule_values/media', 'acf_location_rules_values_media');
    function acf_location_rules_values_media( $choices )
    {
        $media = get_types();
    
        if( $media )
        {
            foreach( $media as $type )
            {
                $choices[ $type->data->ID ] = $type->data->display_name;
            }
        }
    
        return $choices;
    }

    My knowledge of this level of php is not great. I understand that it’s a foreach loop here but am unable to write out the syntax correctly. Can anybody help me please?

    To summarise: I’m looking to add a location rule that allows me to display a field set for a media file, defined by whether it is audio, image, video or file.

    Many thanks in advance

    Phil

Viewing 1 post (of 1 total)

The topic ‘Custom Location Rules for Media files / Library’ is closed to new replies.