最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to pull code snippet from functionality plugin?

programmeradmin0浏览0评论

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="=<?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="=<?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="=<?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="=<?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="=<?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="=<?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="=<?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="=<?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?

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/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/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/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/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/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/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/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/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?

Share Improve this question asked Jan 21, 2021 at 9:11 JaniJani 156 bronze badges 0
Add a comment  | 

1 Answer 1

Reset to default 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/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/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/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/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?

发布评论

评论列表(0)

  1. 暂无评论