• Resolved metaglyphics

    (@metaglyphics)


    Is there a way to add another placeholder? I’ve got a custom meta field in my media library for “photographer name.” I’d like to create a dynamic text watermark that would add “Photo By %photographer_name%”. Is there a filter to build an additional placeholder?

    Thanks

Viewing 10 replies - 1 through 10 (of 10 total)
  • Plugin Author Kuba Mikita

    (@kubitomakita)

    Hi, there is a way! We composed a guide.

    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:

    • resolve – returning the value of the placeholder
    • validate – checking if the value is correct
    • sanitize – sanitizing the value

    There are also 4 abstract placeholders available that implement the validate and sanitize methods for a given value type:

    • EasyWatermark\Placeholders\Abstracts\EmailPlaceholder
    • EasyWatermark\Placeholders\Abstracts\IntegerPlaceholder
    • EasyWatermark\Placeholders\Abstracts\StringPlaceholder
    • EasyWatermark\Placeholders\Abstracts\UrlPlaceholder

    Placeholder class

    
    use EasyWatermark\Placeholders\Abstracts\StringPlaceholder;
    
    /**
     * Abstract placeholder
     */
    class FavoriteColorPlaceholder extends StringPlaceholder {
    
    	/**
    	 * Constructor
    	 *
    	 * @return void
    	 */
    	public function __construct() {
    
    		$this->slug    = 'favorite_color';
    		$this->name    = __( 'Favorite color', 'textdomain' );
    		$this->example = __( 'blue', 'textdomain' );
    
    	}
    
    	/**
    	 * Resolves placeholder
    	 *
    	 * @param  EasyWatermark\Placeholders\Resolver $resolver Placeholders resolver instance.
    	 * @return string
    	 */
    	public function resolve( $resolver ) {
    
    		$user = wp_get_current_user();
    		return get_user_meta( 'favorite_color', $user->ID );
    
    	}
    }

    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

    
    /**
     * @param EasyWatermark\Placeholders\Resolver $resolver Placeholders resolver instance.
     */
    add_action( 'easy-watermark/placeholders/load', function ( $resolver ) {
        // Add custom placeholder instance to the resolver.
        $resolver->add_placeholder( new FavoriteColorPlaceholder() );
    } );
    
    Thread Starter metaglyphics

    (@metaglyphics)

    Thanks for this.

    To clarify, where would these be added? Would I add a new file with the placeholder class to the /placeholders/abstracts folder? Then would I register it in my functionality plugin?

    Thanks

    Plugin Author Kuba Mikita

    (@kubitomakita)

    Please don’t edit the plugin files! You’ll lose any changes with the next update.

    The class definition and registering should happen in your plugin or (child) theme.

    Thread Starter metaglyphics

    (@metaglyphics)

    Of course. But if I just add these snippets to my functionality plugin, they crash the site.

    Plugin Author Kuba Mikita

    (@kubitomakita)

    Could you paste the error? The most common issue is putting the use statement in the middle of the file, while it should be on top, before any other code.

    Thread Starter metaglyphics

    (@metaglyphics)

    Here’s the code I’ve added to my child theme functions file:

    https://pastebin.com/TPLsNeTt

    Your example is for user meta, but this will be for a media library custom field. I’m trying to get the attachment’s custom meta (be_photographer_name.)

    But when I edit the watermark, it’s not finding this new field %photographer%

    Thread Starter metaglyphics

    (@metaglyphics)

    Any suggestions?

    Thread Starter metaglyphics

    (@metaglyphics)

    Ooh! Turns out this was a caching issue. It’s definitely working.

    Thanks

    Plugin Support annaszalkiewicz

    (@annaszalkiewicz)

    Hello,

    Good to know it’s working, we’re glad!

    This thread and guide were useful, however, I am unable to get my additional custom placeholder to show up as well. @metaglyphics did you do anything unique to flush/reset your cache?

    Should be straightforward, extend the class in the child themes function.php right?

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘Additional Placeholder’ is closed to new replies.