1

I'm trying to replace "More Details" text from the below functions using an add_filter hook

function awsm_job_more_details( $link, $view ) {
        $more_dtls_link = sprintf( '<div class="awsm-job-more-container"><%1$s class="awsm-job-more"%3$s>%2$s <span></span></%1$s></div>', ( $view === 'grid' ) ? 'span' : 'a', esc_html__( 'More Details', 'wp-job-openings' ), ( $view === 'grid' ) ? '' : ' href="' . esc_url( $link ) . '"' );
        echo apply_filters( 'awsm_jobs_listing_details_link', $more_dtls_link, $view );
    }

What I tried.....

function example_callback( $more_dtls_link, $view ) {
    $more_dtls_link = sprintf( '<div class="awsm-job-more-container"><%1$s class="awsm-job-more"%3$s>%2$s <span></span></%1$s></div>', ( $view === 'grid' ) ? 'span' : 'a', esc_html__( 'Hello World', 'wp-job-openings' ), ( $view === 'grid' ) ? '' : ' href="' . esc_url( $link ) . '"' );
    return $more_dtls_link;
}
add_filter( 'awsm_jobs_listing_details_link', 'example_callback',10, 2);

Text is getting replaced to "Hello World" but the link is broken on the Button. May I know what I'm missing here??

1
  • 1
    did you check your PHP error log? Your new function uses $link but there is no $link variable. Also the filter does not pass the original $link URL, it passes the HTML, so it doesn't work the way you thought it did. You can't copy paste the function and modify it to what you want, you'd need to do a search/replace of the HTML in $more_dtls_link instead. Where does awsm_job_more_details come from?
    – Tom J Nowell
    Commented Nov 24, 2022 at 14:06

1 Answer 1

-1

I just managed to fix it using string replacement function in php.

function replace_btn_text( $more_dtls_link, $view ) {
    $link = htmlspecialchars($more_dtls_link);
    $str = str_replace('More Details', 'View Details', $link);
    $new_link = htmlspecialchars_decode($str);
    return $new_link;
}
add_filter( 'awsm_jobs_listing_details_link', 'replace_btn_text',10, 2);

Suggest me if I need to do any improvements here..

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.