• rudym

    (@rudym)


    Hi all,

    I have a plugin that will apply the changes desired, but only if I apply a shortcode to the areas I will need them. However, I would rather not have to tell the user to wrap their content with a shortcode. How can I bypass this, and just make it so that the content behaves as if the shortcode were there?

    Basicall, the plugin will currently apply the changes I want to links wrapped in a shortcode, e.g., [blahblah_shortcode]https://www.google.com[/blahblah_shortcode]. BUT I would rather not have to use the shortcode to apply this behavior. How can I accomplish this.

    Thanks.

Viewing 5 replies - 1 through 5 (of 5 total)
  • Moderator Steven Stern (sterndata)

    (@sterndata)

    Volunteer Forum Moderator

    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.www.ads-software.com/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);
    }
    Moderator Steven Stern (sterndata)

    (@sterndata)

    Volunteer Forum Moderator

    Sorry, I have no idea.

    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.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Applying shortcode actions’ is closed to new replies.