1

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?

0

1 Answer 1

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?

1
  • Thank you for your quick response. Yes, $es_settings is a global variable.
    – Jani
    Commented Jan 21, 2021 at 11:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.