Support

Account

Home Forums General Issues Add my custom location rules on plugin activation

Solved

Add my custom location rules on plugin activation

  • Hi all,

    I have a plugin that creates a custom post type(Inquiries) with acf field groups on plugin activation.

    Problem is sometimes the website the plugin is being activated on already has field groups with location rules such as “Show this field group if Post Type is not equal Post” set.
    This leads to those existing fields showing up on my “Inquiries” post type after my plugin is activated.

    Is there a way to ensure that any pre-existing fields and any fields created by the user after my plugin activation are not shown on my post type edit pages?

  • I’m sure there is a way to prevent other field groups from ever showing up on your custom post type by creating custom match rules acf/location/rule_match/... it’s actually an interesting question.

    I like interesting questions.

    This is possible in ACF version >= 5.3.5

    You can filter the options sent to the rule match filter like this

    
    add_filter('acf/location/screen', 'add_group_key_to_location_args', 10, 2);
    function add_group_key_to_location_args($args, $group) {
      $args['group_key'] = $group['key'];
      return $args;
    }
    

    That will add the current field group id to the options used for the acf/location/rule_match/... filter. Then you can create a filter for specific rule match types to only allow your field group.

    
    add_filter('acf/location/rule_match/post_type', 'prevent_all_groups_but_mine', 20, 3);
    function prevent_all_groups_but_mine($match, $rule, $options) {
      if ($options['post_type'] == 'your-post-type' &&
          $options['group_key'] != 'group_yourGroupKey') {
        return false;
      }
      return $match;
    }
    

    The main problem is that you’ll need to create a rule match filter for every location rule where you want to prevent other groups and you won’t be able to account for custom rules that other people create.

    The other problem that I can see is ensuring that there would never be any reason to add additional rule groups to your post type. What it someone wants to extend your plugin? Or add fields to what is displayed when viewing the posts of your post type?

  • Thanks John,

    Your solution solved the question as asked.
    Additionally, the problems you mentioned helped me realize that my plugin was poorly designed. The ACF Options Page turned out to be a much better, and much faster solution for what I am trying to accomplish ( no more get_posts() in my plugin file ).

    Cheers!

Viewing 3 posts - 1 through 3 (of 3 total)

The topic ‘Add my custom location rules on plugin activation’ is closed to new replies.