I have a functionality plugin to extend an existing (original) plugin. (Before I was just forking, but after the update of the original plugin it became clear that it is not a good practice.) I saved the code snippets I used when forking, and want to add them to the original plugin.
I found a hook here in the original plugin:
<?php do_action( 'es_wishlist_add_button', get_the_ID() ); ?>
And I’d like to place this code:
<?php if ( $es_settings->share_facebook ) : ?>
<a class="a2a_button_facebook" href="https://www.addtoany.com/add_to/facebook?linkurl=<?php the_permalink(); ?>"><i class="fa fa-facebook" aria-hidden="true"></i></a>
<?php endif; ?>
<?php if ( $es_settings->share_twitter ) : ?>
<a class="a2a_button_twitter" href="https://www.addtoany.com/add_to/twitter?linkurl=<?php the_permalink(); ?>"><i class="fa fa-twitter" aria-hidden="true"></i></a>
<?php endif; ?>
<?php if ( $es_settings->share_google_plus ) : ?>
<a class="a2a_button_google_plus" href="https://www.addtoany.com/add_to/facebook?linkurl=<?php the_permalink(); ?>"><i class="fa fa-google-plus" aria-hidden="true"></i></a>
<?php endif; ?><?php if ( $es_settings->share_linkedin ) : ?>
<a class="a2a_button_linkedin" href="https://www.addtoany.com/add_to/facebook?linkedin=<?php the_permalink(); ?>"><i class="fa fa-linkedin" aria-hidden="true"></i></a>
<?php endif; ?>
<?php if ( $es_settings->use_pdf ) : ?>
<a href="<?php echo add_query_arg( "es-pdf", get_the_ID(), get_the_permalink() ); ?>" target="_blank"><i class="fa fa-file-pdf-o" aria-hidden="true"></i></a>
<?php endif; ?>
I use this in the functional plugin’s functions.php:
function epf_extra_buttons() {
echo '<?php if ( $es_settings->share_facebook ) : ?>
<a class="a2a_button_facebook" href="https://www.addtoany.com/add_to/facebook?linkurl=<?php the_permalink(); ?>"><i class="fa fa-facebook" aria-hidden="true"></i></a>
<?php endif; ?>
<?php if ( $es_settings->share_twitter ) : ?>
<a class="a2a_button_twitter" href="https://www.addtoany.com/add_to/twitter?linkurl=<?php the_permalink(); ?>"><i class="fa fa-twitter" aria-hidden="true"></i></a>
<?php endif; ?>
<?php if ( $es_settings->share_google_plus ) : ?>
<a class="a2a_button_google_plus" href="https://www.addtoany.com/add_to/facebook?linkurl=<?php the_permalink(); ?>"><i class="fa fa-google-plus" aria-hidden="true"></i></a>
<?php endif; ?><?php if ( $es_settings->share_linkedin ) : ?>
<a class="a2a_button_linkedin" href="https://www.addtoany.com/add_to/facebook?linkedin=<?php the_permalink(); ?>"><i class="fa fa-linkedin" aria-hidden="true"></i></a>
<?php endif; ?>
<?php if ( $es_settings->use_pdf ) : ?>
<a href="<?php echo add_query_arg( "es-pdf", get_the_ID(), get_the_permalink() ); ?>" target="_blank"><i class="fa fa-file-pdf-o" aria-hidden="true"></i></a>
<?php endif; ?>';
}
add_action('es_wishlist_add_button','epf_extra_buttons');
However, I do not receive the expected result. What do I do wrong?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
You’re trying to echo <?php tags, but that won’t work: they won’t be processed. Instead you need to replicate that logic in PHP statements in your function instead. Here’s a partial answer:
function epf_extra_buttons( $post_ID ) {
// TODO: where does $es_settings come from?
$encoded_permalink = urlencode(get_the_permalink());
if ( $es_settings->share_facebook ) {
echo '<a class="a2a_button_facebook" href="https://www.addtoany.com/add_to/facebook?linkurl=' . $encoded_permalink . '"><i class="fa fa-facebook" aria-hidden="true"></i></a>';
}
if ( $es_settings->share_twitter ) {
echo '<a class="a2a_button_twitter" href="https://www.addtoany.com/add_to/twitter?linkurl=' . $encoded_permalink . '"><i class="fa fa-twitter" aria-hidden="true"></i></a>';
}
if ( $es_settings->share_google_plus ) {
echo '<a class="a2a_button_google_plus" href="https://www.addtoany.com/add_to/facebook?linkurl=' . $encoded_permalink . '"><i class="fa fa-google-plus" aria-hidden="true"></i></a>';
}
if ( $es_settings->share_linkedin ) {
echo '<a class="a2a_button_linkedin" href="https://www.addtoany.com/add_to/facebook?linkedin=' . $encoded_permalink . '"><i class="fa fa-linkedin" aria-hidden="true"></i></a>';
}
if ( $es_settings->use_pdf ) {
$pdf_url = add_query_arg( "es-pdf", get_the_ID(), get_the_permalink() );
echo '<a href="' .esc_url( $pdf_url ) . '" target="_blank"><i class="fa fa-file-pdf-o" aria-hidden="true"></i></a>';
}
}
add_action('es_wishlist_add_button','epf_extra_buttons');
Note that I’ve added some escaping and URL encoding that you were missing. I’ve also added $post_ID as an argument as you can see from the do_action call that it’s passed in.
However this won’t work as is, because you don’t have the $es_settings variable available in this function. You’ll have to work out where that comes from: is it global? Can you look it up or compute it from the information you do have, which is the $post_ID?
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0