admin管理员组

文章数量:1287148

I am using a filter to decrypt an api_key that is stored with encryption. I have registered the following hook:

// Decrypt API key after it is retrieved
add_filter('pre_option_percify_api_key', array( __CLASS__, 'decrypt_api_key') );

The problem is, I cannot get at the stored value in the callback:

public static function decrypt_api_key($encrypted) {
  // $encrypted is empty:

  echo($encrypted);
  // ...

Am I calling the function correctly? How do I access the stored value of percify_api_key within decrypt_api_key?

I am using a filter to decrypt an api_key that is stored with encryption. I have registered the following hook:

// Decrypt API key after it is retrieved
add_filter('pre_option_percify_api_key', array( __CLASS__, 'decrypt_api_key') );

The problem is, I cannot get at the stored value in the callback:

public static function decrypt_api_key($encrypted) {
  // $encrypted is empty:

  echo($encrypted);
  // ...

Am I calling the function correctly? How do I access the stored value of percify_api_key within decrypt_api_key?

Share Improve this question asked Nov 7, 2021 at 14:09 user3574603user3574603 3411 gold badge2 silver badges11 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

The pre_option_{$option} hook is used to filter the value of the option before it's retrieved. You need to hook after the value is retrieved so you can manipulate it.

In this case, you can use the option_{$option} hook. So your code will look like this:

add_filter('option_percify_api_key', array( __CLASS__, 'decrypt_api_key') );

For more info - take a look at the source of the get_option function here. Specifically line #225.

本文标签: hooksHow do I use preoptionoptionname correctly