0

I've installed a plugin which displays a custom widget and I'm trying to add a simple filter to change the target attribute in a link from _blank to self.

This is the code that I'm trying to modify:

<?php
                        $link   = "<a href='{$affiliate_link}' 
rel='nofollow' target='_blank' class='wppr-bttn'>" . __( 
$instance['cwp_tp_buynow'], 'cwppos' ) . '</a>';
                    if ( ! empty( $instance['cwp_tp_buynow'] ) ) {
                        echo apply_filters( 
'wppr_widget_style1_buynow_link', $link, get_the_ID(), $affiliate_link, 
$instance['cwp_tp_buynow'] );
                    }

                        $link   = "<a href='{$review_link}' rel='nofollow' 
target='_blank' class='wppr-bttn'>" . __( $instance['cwp_tp_readreview'], 
'cwppos' ) . '</a>';
                    if ( ! empty( $instance['cwp_tp_readreview'] ) ) {
                        echo apply_filters( 
'wppr_widget_style1_readreview_link', $link, get_the_ID(), $review_link, 
$instance['cwp_tp_readreview'] );
                    }
                    ?>

The code displays 2 buttons. It's the second (read review) link that I want to modify.

I tried the following code but it caused my site to crash:

add_filter( 'wppr_widget_style1_readreview_link', 'review_link_custom' );

function review_link_custom( $link, get_the_ID(), $review_link, 
$instance['cwp_tp_readreview'] ) {
$link   = "<a href='{$review_link}' rel='nofollow' target='_self' 
class='wppr-bttn'>" . __( $instance['cwp_tp_readreview'], 'cwppos' ) . 
'</a>';
return $link;
}

1 Answer 1

2

You have invalid PHP (which should have been obvious from the PHP error log).

function review_link_custom( $link, get_the_ID(), $review_link, $instance['cwp_tp_readreview'] ) {

Functions can have neither other functions nor array+key as an argument, you can only specify the name for an argument passed. Also, when adding a filter you have to specify how many arguments are passed to it (default is 2 - you have 4, so it needs to be explicitly specified). Try something like this instead

add_filter( 'wppr_widget_style1_readreview_link', 'review_link_custom', 10, 4 );
function review_link_custom( $link, $id, $review_link, $cwp_tp_readreview ) {
    $link   = "<a href='{$review_link}' rel='nofollow' target='_self' class='wppr-bttn'>" . __( $cwp_tp_readreview, 'cwppos' ) . '</a>';
    return $link;
}
4
  • Thanks, that's working except the button text is missing.
    – jrcollins
    Commented Jul 22, 2017 at 4:36
  • @jrcollins Please see my update
    – kero
    Commented Jul 22, 2017 at 8:56
  • Thanks for your help. It's working perfectly now. Could you please explain how you get the $cwp_tp_readreview variable from $instance['cwp_tp_readreview'] and the $id variable from get_the_ID()?
    – jrcollins
    Commented Jul 22, 2017 at 9:25
  • @jrcollins It is a bit difficult to explain for me here .. These are just names you assign, you could also say function review_link_custom($foo, $bar, $baz, $hello) and it would work. I find the official PHP doc helps alot, ie here and there
    – kero
    Commented Jul 22, 2017 at 10:30

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.