Support

Account

Home Forums Backend Issues (wp-admin) Only Display Field Group if Page is Password Protected

Solved

Only Display Field Group if Page is Password Protected

  • On a site I’m working on, I’d like the ability to create some custom content that appears above the Password form on a Password Protected page. My hope is to create a field group that has a WYSIWYG field that only appears (in the admin) if a page is set to be Password Protected.

    I have the code needed to modify the password form, so I’d like to include a get_field call to a field that sets some introductory text prior to outputting the form, but I’d rather the field group not appear in the admin for non-password pages.

    Any ideas?

  • Hi @jsites

    An idea would be to create a set of custom location rules to target the password protected pages: https://www.advancedcustomfields.com/resources/custom-location-rules/

  • I was able to solve this with the following code:

    /* SHOW FIELD SET ON PASSWORD PROTECTED PAGES */
    add_filter('acf/location/rule_types', 'acf_location_rules_types');
    
    function acf_location_rules_types( $choices ) {
    	
      $choices['Post']['visibility'] = 'Post Visibility';
    
      return $choices;
      
    }
    
    add_filter('acf/location/rule_values/visibility', 'acf_location_rules_values_visibility');
    
    function acf_location_rules_values_visibility( $choices ) {
    
      //var_dump($choices);
      $choices['password'] = 'Password Protected';
    
      return $choices;
    }
    
    add_filter('acf/location/rule_match/visibility', 'acf_location_rules_match_visibility', 10, 3);
    function acf_location_rules_match_visibility( $match, $rule, $options )
    {
      $the_post = get_post($options->post_id);
      $pw = $the_post->post_password;
      if(isset($pw)){
        if(!empty($pw)){
          $match = true;
        }
      }else{
        $match = false;
      }
    
      return $match;
    }
Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.