If you were searching for the content, could you definitively identify it without a shortcode? If so, you can write a filter on the_content and “magically” adjust that piece of it.
https://developer.wordpress.org/reference/hooks/the_content/
If not, you need the shortcode.
Thread Starter
rudym
(@rudym)
I guess I’d need a regex to identify the targets, it would be all where the href points to a PDF. I’ll check out the link you posted, thanks!
Thread Starter
rudym
(@rudym)
@sterndata,
Thanks again. I tried adding the filter, and seems to work okay. HOWEVER, in the plugin code, I was working with a creating, setting, and checking the value for a $_SESSION variable. How would this work now since now I’ve managed to eliminate the need for the shortcode? Below is a breakdown of the changes I made:
In the main PHP code for the plugin, I added add_filter( 'the_content', 'hash_links' ); in one of the functions that get called upon instantiation.
I then added the following functions OUTSIDE the plugin class:
// Replace all PDF links with the hash values.
function hash_links( $content ) {
$regex = "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*\.pdf)+/";
preg_match_all($regex, $content, $link_matches);
$content .= "<BR><BR><BR>";
foreach($link_matches[0] as $match){
$content = str_replace($match, hashLink($match), $content);
}
return $content;
}
// This will return the hash value for the link. Here, we will also add the hash value and plaintext to the option table.
function hashLink($link) {
$ins = $GLOBALS['reCaptchaProtectedDownloadsCore'];
$hash = apply_filters("rcpdl_hash", md5($link), $link);
if ( $ins->isNetworkActive() ){
add_site_option("rcpdl_{$hash}", esc_attr(esc_url( $link )));
} else{
add_option("rcpdl_{$hash}", esc_attr(esc_url( $link )));
}
return apply_filters('rcpdl_link', "#rcpdl={$hash}", $link, $hash);
}
Thread Starter
rudym
(@rudym)
Ah, got it. What I had was actually SESSION info AND a javascript variable that controlled the link behavior. The javascript variable kept being reset because the link would open in the same window/tab, thereby refreshing the page on return, and the javascript with it.
In short, I just had to change it so that the links open in new tabs/windows instead. Thanks again for your help in resolving my first issue.