Skip to content

Instantly share code, notes, and snippets.

@TimBHowe
Created March 23, 2016 19:30
Show Gist options
  • Save TimBHowe/8f77fd5cdc50789f32de to your computer and use it in GitHub Desktop.
Save TimBHowe/8f77fd5cdc50789f32de to your computer and use it in GitHub Desktop.
Add post visibility to the lost of location options to ACF. Allowing you to add field to posts based on if it is public, password protected or privet.
<?php
// Add a custom rule type to ACF.
function acf_location_rules_types( $choices ) {
$choices['Post']['post_visibility'] = 'Post Visibility';
return $choices;
}
add_filter('acf/location/rule_types', 'acf_location_rules_types');
// Add options for rules for custom type to ACF.
function acf_location_rules_values_user( $choices ) {
$choices['public'] = "Public";
$choices['protected'] = "Password Protected";
$choices['private'] = "Private";
return $choices;
}
add_filter('acf/location/rule_values/post_visibility', 'acf_location_rules_values_user');
// Check the new location rules.
function acf_location_rules_match_user( $match, $rule, $options ) {
// If there is a password required the post is protected
if ( post_password_required() ) {
$post_status = 'protected';
} else {
// Else
$post_status = get_post_status();
switch( $post_status ) {
case 'publish':
case 'pending':
case 'draft':
case 'future':
$post_status = 'public';
break;
case 'private':
$post_status = 'private';
break;
default:
return; // excludes any status for auto-draft, inherit, trash.
}
}
$selected_status = $rule['value'];
if( $rule['operator'] == "==" ) {
$match = ( $post_status == $selected_status );
} elseif( $rule['operator'] == "!=" ) {
$match = ( $current_user->ID != $selected_user );
}
return $match;
}
add_filter('acf/location/rule_match/post_visibility', 'acf_location_rules_match_user', 10, 3);
@Gabrieluno
Copy link

Hello @TimBHowe, I tell you that I looked all over the internet and I did not find a code like this.

I'm trying to define a post as protected and a password with ACF fields, the problem is that I don't know how to implement your code to do that. Would you help me with this?

Thank you very much for your contributions on Github ♥

@TimBHowe
Copy link
Author

Hey @Gabrieluno,

The above lets you set the "Location" of your ACF fields by adding a "Post Visibility" option to the "Show this field group if" drop-down.
Then select to show/hide the ACF fields if the post is "Public", "Password Protected", and/or "Private".

We used it for a project where we wanted to show a message to users set in ACF when a page was password protected.
So I'm not sure if this code is what your looking for, but hopefully it helps.

The code can be added the the function.php file of your active WordPress theme.
You might want to wrap it in if( ! class_exists('ACF') ) : statement so it only runs when ACF is on the site and active.

Let me know if you have any followup questions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment