Closed JiveDig closed 6 years ago
You're right. Since it's not part of the $link
variable, it's not currently filterable. We should add a filter right before it's used (line 490) so you can modify $attr
.
What type of information should that filter pass along, other than $attr
? Should we pass along $link
as well so you can access all that data (ex: what type of link it is)?
I'm not really sure at this point. For my current situation, I'm trying to grab an image URL from post meta (CMB2) and add it as data-pin-media so I can force a specific image when a post is shared on pinterest. So, I only need to get_the_ID(). But I was thinking I could do it in the ea_share_count_link
filter so I knew when I was on the pinterest button.
I guess that answers it, we need to pass some of the link data so we know what button we're dealing with.
Would it be worth using another attribute in the $link variable. Maybe $link['data']
that is an array of attributes with values. Then we can do
add_filter( 'ea_share_count_link', function( $link ) {
switch ( $link['type'] ) {
case 'pinterest':
$pinterest_image = get_post_meta( get_the_ID(), 'pinterest_image', true );
if ( $pinterest_image ) {
$link['data'] = array(
'pin-media' => esc_url( $pinterest_image ),
);
}
break;
}
return $link;
});
And the $link filter can loop through the data array if isset, the same way as before.
// Add data attributes
if ( isset( $link['data'] && ! empty( $link['data'] ) ) {
foreach ( $link['data'] as $key => $val ) {
$data .= ' data-' . $key . '="' . $val . '"';
}
}
Yeah, I think it would make sense to move the $data
to $link['data']
so it is accessible using existing filters.
@jaredatch thoughts?
Though it makes sense to make the above changes, I wanted to update that I could accomplish my goal with the img filter like this:
add_filter( 'ea_share_count_single_image', function( $url, $id ) {
$pinterest_image = get_post_meta( get_the_ID(), 'pinterest_image', true );
if ( $pinterest_image ) {
$url = esc_url( $pinterest_image );
}
return $url;
}, 10, 2 );
$link['data']
makes sense to me, definitely see the use case.
Closing. Created an issue on the new repo which will be the home of the next release.
I thought this was possible already since you kinda build them now via https://github.com/jaredatch/EA-Share-Count/blob/master/includes/class-front.php#L490, but the
ea_share_count_link filter (or others) don't actually allow you to modify the
$attr` variable. This seems like a nice addition, unless it's possible and I missed it.