Custom Placeholder
Each placeholder is defined with a class responsible for replacing a specific string tag with a dynamic value. Placeholder class needs to extend EasyWatermark\Placeholders\Abstracts\Placeholder and should implement the following methods:
  1. 1.
    resolve - returning the value of the placeholder
  2. 2.
    validate - checking if the value is correct
  3. 3.
    sanitize - sanitizing the value
There are also 4 abstract placeholders available that implement the validate and sanitize methods for a given value type:
  1. 1.
    EasyWatermark\Placeholders\Abstracts\EmailPlaceholder
  2. 2.
    EasyWatermark\Placeholders\Abstracts\IntegerPlaceholder
  3. 3.
    EasyWatermark\Placeholders\Abstracts\StringPlaceholder
  4. 4.
    EasyWatermark\Placeholders\Abstracts\UrlPlaceholder

Placeholder class

1
use EasyWatermark\Placeholders\Abstracts\StringPlaceholder;
2
3
/**
4
* Abstract placeholder
5
*/
6
class FavoriteColorPlaceholder extends StringPlaceholder {
7
8
/**
9
* Constructor
10
*
11
* @return void
12
*/
13
public function __construct() {
14
15
$this->slug = 'favorite_color';
16
$this->name = __( 'Favorite color', 'textdomain' );
17
$this->example = __( 'blue', 'textdomain' );
18
19
}
20
21
/**
22
* Resolves placeholder
23
*
24
* @param EasyWatermark\Placeholders\Resolver $resolver Placeholders resolver instance.
25
* @return string
26
*/
27
public function resolve( $resolver ) {
28
29
$user = wp_get_current_user();
30
return get_user_meta( 'favorite_color', $user->ID );
31
32
}
33
}
34
Copied!
The above class will replace a %favorite_color% string in the watermark text with the value of a favorite_color meta for logged-in user.

Registering the Placeholder

1
/**
2
* @param EasyWatermark\Placeholders\Resolver $resolver Placeholders resolver instance.
3
*/
4
add_action( 'easy-watermark/placeholders/load', function ( $resolver ) {
5
// Add custom placeholder instance to the resolver.
6
$resolver->add_placeholder( new FavoriteColorPlaceholder() );
7
} );
Copied!
Last modified 8mo ago