Policies
Overview
Create, attach, inspect, and detach JSON access policies. Select the subject with AAM::api()->policies($access_level, $settings). Omit $access_level to use the current user or visitor.
PHP class: AAM_Framework_Service_Policies.
Method reference
| Method | Purpose |
|---|---|
get_policies($args = []) |
List attached policies. |
policies($args = []) |
Alias of get_policies(). |
get_policy($policy_id) |
Get an attached policy. |
policy($policy_id) |
Alias of get_policy(). |
attach($policy_id) |
Attach a policy by ID. |
detach($policy_id) |
Detach a policy by ID. |
create($policy, $status = 'publish', $apply = true) |
Create a policy. |
get_statements($resource_pattern = null, $args = []) |
Get effective policy statements. |
get_params($key_pattern = null, $args = []) |
Get effective policy parameters. |
get_param($key, $args = []) |
Get one effective parameter. |
statements($resource_pattern = null, $args = []) |
Alias of get_statements(). |
params($key_pattern = null, $args = []) |
Alias of get_params(). |
param($key, $args = []) |
Alias of get_param(). |
reset() |
Remove one rule or all rules for this level. |
is_customized() |
Check for a direct rule or preference. |
is_attached($policy_id) |
Check whether a policy is attached. |
is_detached($policy_id) |
Check whether a policy is detached. |
Return values vary by method. Rule updates usually return bool; reads can return a value, null, or an array. Errors follow the service’s error_handling setting and may produce WP_Error.
Accepted identifiers
Pass the numeric ID of an existing aam_policy post to attach(), detach(), get_policy(), or the attachment checks. create() instead accepts a policy JSON string or an array containing json and optional post fields such as title; its input defines a new policy rather than identifying an existing one.
Example
$policies = AAM::api()->policies('role:subscriber');
$policies->attach(123);
$attached = $policies->is_attached(123);
The policy with ID 123 must already exist. attach() assigns it to the Subscriber role, and $attached checks that assignment.
Developer notes
- A policy must exist before it can be attached by ID.
- Use
get_statements()andget_params()to inspect effective policy content. - See the JSON Access Policy reference for statement syntax and conditions.
- The selected access level determines whose rules are stored or evaluated. See Access levels.
Behavior and parameters
attach($policy_id)anddetach($policy_id)change which policies apply to this level.get_policies()lists them;is_attached()andis_detached()test attachment state.create($policy, $status = "publish", $apply = true)creates a policy.get_statements()andget_params()inspect its effective rules.reset()clears this level’s policy assignments.- See JSON Access Policy for policy document syntax.