• Resolved BarbaraCee

    (@barbaracee)


    Hi there,

    I am trying to append URL parameters (for affiliate purposes) dynamically to certain links on my website. I followed the tips provided in this post, however have been unlucky. When I click the call to action button in the page linked here, “Try for free”, it no longer redirects but the URL looks like so: https://wttdev.wpengine.com/out/Array (I’m testing this in a dev environment right now, but just showing you for reference). It seems odd that this would not work with an array, given that the hook was set up for an array. What am I doing wrong?

    Here is my code, I created a separate plugin to handle this code and can confirm that the plugin is active in dev environment, where I am trying this all out.

    // affiliate-links.php

    <?php
    /*
        Plugin Name: Affiliate Links Plugin
        Description: Adding URL param to Wix-only affiliate links
        Version: 1.0
        Author: [email protected]
        License: GPL2
    */
    
    // Adding filter hook from Pretty Links Plugin to our Wix CTA links
    function append_to_target_url($pretty_link_array) {
      $wix_post_id = '&s3=' . $post->ID;
      $pretty_link_array['url'] = $pretty_link_array['url'] . $wix_post_id;
      return $pretty_link_array;
    }
    add_filter('prli_target_url', 'append_to_target_url');

    // PrliUtils.php

    // This is where the magic happens!
      public function track_link($slug,$values) {
        global $wpdb, $prli_click, $prli_options, $prli_link, $plp_update;
    
        $query = "SELECT * FROM ".$prli_link->table_name." WHERE slug='$slug' LIMIT 1";
        $pretty_link = $wpdb->get_row($query);
        $pretty_link_array = array( 
                              'url' => $pretty_link->url, 
                              'link_id' => $pretty_link->id, 
                              'redirect_type' => $pretty_link->redirect_type
                            );
        $pretty_link_target = apply_filters('prli_target_url', $pretty_link_array);
        $prli_edition = ucwords(preg_replace('/-/', ' ', PRLI_EDITION));
    
        // Error out when url is blank
        if($pretty_link->redirect_type != 'pixel' && (!isset($pretty_link_target['url']) || 
        empty($pretty_link_target['url']))) {
          return false;
        }
    
        $pretty_link_url = $pretty_link_target;
        $track_me = apply_filters('prli_track_link', $pretty_link->track_me);

    Thanks in advance for your help!

    The page I need help with: [log in to see the link]

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author cartpauj

    (@cartpauj)

    I think you need to add global $post; at the top of your function.

    You’re trying to use $post without defining it.

    Thread Starter BarbaraCee

    (@barbaracee)

    The issue for that peculiar error was actually, I erased a critical piece of code in PrliUtils.php, as in:

    $pretty_link_url = $pretty_link_target;

    should actually be:

    $pretty_link_url = $pretty_link_target['url']; as you have it originally written in your plugin *face palm*

    However, now that I have put that back in, the redirect is working as expected however I am completely unable still to append any parameters. I thought at one point the issue was that (1) post ID was not available as here I am calling it outside the loop and/or (2) the post ID was not available yet as the plugin is running too quickly, and hence I have plopped that into the template_redirect hook. Though, even the first bit of the parameter of &s3= is not being appended so I really am at a loss here.

    Your help would be appreciated!

    P.S. here is what my plugin looks like now:

    <?php
    /*
        Plugin Name: Affiliate Links Plugin
        Description: Adding URL param to Wix-only affiliate links
        Version: 1.0
        Author: [email protected]
        License: GPL2
    */
    add_action('template_redirect', function() {
      global $wp_query;
      $postID = $wp_query->get_queried_object_id();
       // Adding filter hook from Pretty Links Plugin to our Wix CTA links
      function append_to_target_url($pretty_link_array) {
        $wix_id = '&s3=' . $postID;
        $pretty_link_array['url'] = $pretty_link_array['url'] . $wix_id;
        return $pretty_link_array;
      }
      add_filter('prli_target_url', 'append_to_target_url');
    });
    
    • This reply was modified 5 years, 1 month ago by BarbaraCee.
    Plugin Author cartpauj

    (@cartpauj)

    The redirect happens during plugins_loaded action in WP, so template_redirect is going to be too late to hook in.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Appending URL parameters not working with existing hook’ is closed to new replies.