Skip to content

Instantly share code, notes, and snippets.

@amdrew
Last active June 25, 2020 09:12
Show Gist options
  • Save amdrew/8332cb786dd71ced47c0 to your computer and use it in GitHub Desktop.
Save amdrew/8332cb786dd71ced47c0 to your computer and use it in GitHub Desktop.
AffiliateWP - Custom tracking script without using the shortcode. Useful for when you need to pass in dynamic values
<?php
/**
* Ninja Forms integration
*
* 1. Set your ninja form to redirect to a "thanks page"
* 2. Enter the Field ID for the amount below if using a "total" field. Otherwise you can safely ignore it.
*/
function affwp_integration_ninja_forms() {
global $ninja_forms_processing;
// change these values if required
$total_field_id = 8; // the Field ID that contains the total
$status = 'unpaid'; // the status that the referral should be created as
// Make sure the conversion script is only injected into the success page
if ( ! isset( $ninja_forms_processing ) ) {
return;
}
// if no amount is specified the referral will be created as $0.00
$amount = isset( $ninja_forms_processing->data['fields'][$total_field_id] ) ? $ninja_forms_processing->data['fields'][$total_field_id] : '';
// referral arguments
$args = array(
'amount' => $amount,
'status' => $status,
'reference' => $ninja_forms_processing->data['form']['sub_id'],
'description' => $ninja_forms_processing->data['form']['form_title'],
'context' => 'ninja-forms' // used to link the reference number on the Affiliates -> Referrals page through to the edit submission page in Ninja Forms
);
// add the conversion script to the page
affiliate_wp()->tracking->conversion_script( $args );
}
add_action( 'wp_head', 'affwp_integration_ninja_forms' );
/**
* Optional - link the reference numbers on the Affiliates -> Referrals page to the Ninja Forms edit submission page
*/
function affwp_integration_ninja_forms_admin_ref_link( $reference = 0, $referral ) {
if ( ! function_exists( 'Ninja_Forms' ) ) {
return;
}
if ( empty( $referral->context ) || 'ninja-forms' != $referral->context ) {
return $reference;
}
$url = add_query_arg( 'ref', urlencode( add_query_arg( array() ) ), get_edit_post_link( $reference ) );
return '<a href="' . esc_url( $url ) . '">' . Ninja_Forms()->sub( $reference )->get_seq_num() . '</a>';
}
add_filter( 'affwp_referral_reference_column', 'affwp_integration_ninja_forms_admin_ref_link', 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment