Forum Replies Created

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter growsmartehg

    (@growsmartehg)

    Thank you a lot. I have managed everything so far.

    When I inserted the lead via PHP code from formregistration to SliceWP the Date is “-0001-11-30 02:00” and not the current time. I have tried to modify the code so the real date is beeing used but I was unable to do so. Also I can not insert the original URL into entry, maybe it is the same problem with the date. But I was able to transfert the register e-mail to customer, so I believe the solution is just a little changement. Here is my code for that, maybe you can tell me where the error is (please see under the section D Lead conversion to SliceWP):

    Thanks a lot!

    Cheers, Tobias

    <?php
    /**

    • Source Information & Lead Conversion for WP-Slice (minimal parameter set).
      *
    • This code:
    • A) Registers and saves three meta fields (affiliate_id, platform_source, platform_medium).
    • B) Displays a custom “Source Information” section in the user profile.
    • C) Fills form fields on the frontend via URL (using JavaScript).
    • D) Immediately after registration, converts the visitor into a lead in WP-Slice,
    • if an affiliate_id is present – passing only the necessary parameters,
    • including a valid timestamp (formatted as “Y-m-d H:i” without seconds) and
    • the user’s email and the original URL in the “entry” field.
      */

    /* A1. Register meta fields officially */
    function my_register_source_meta_fields() {
    $fields = array(
    ‘affiliate_id’ => ‘Affiliate ID for tracking’,
    ‘platform_source’ => ‘Platform Source for tracking (e.g. META, YT, etc.)’,
    ‘platform_medium’ => ‘Platform Medium for tracking (e.g. Ads abc, Youtubevideo 22, etc.)’
    );
    foreach ( $fields as $key => $description ) {
    register_meta( ‘user’, $key, array(
    ‘type’ => ‘string’,
    ‘description’ => $description,
    ‘single’ => true,
    ‘show_in_rest’ => true,
    ) );
    }
    }
    add_action( ‘after_setup_theme’, ‘my_register_source_meta_fields’ );

    /* A2. Add fields to user contact methods (so external plugins like WP-Slice can detect them) */
    function add_custom_mautic_contactmethods_generic( $contactmethods ) {
    $fields = array(
    ‘affiliate_id’ => ‘Affiliate ID’,
    ‘platform_source’ => ‘Platform Source’,
    ‘platform_medium’ => ‘Platform Medium’
    );
    foreach ( $fields as $key => $label ) {
    $contactmethods[$key] = $label;
    }
    return $contactmethods;
    }
    add_filter( ‘user_contactmethods’, ‘add_custom_mautic_contactmethods_generic’ );

    /* A3. Save the fields during user registration (separated for POST and GET) */
    add_action(‘user_register’, ‘my_save_source_fields_to_user’, 10, 1);
    function my_save_source_fields_to_user($user_id) {
    $keys = array(‘affiliate_id’, ‘platform_source’, ‘platform_medium’);
    foreach ($keys as $key) {
    $value = ”;
    // First: Check for a POST value in the form_fields array
    if ( isset($_POST[‘form_fields’][$key]) && $_POST[‘form_fields’][$key] !== ” ) {
    $value = sanitize_text_field($_POST[‘form_fields’][$key]);
    }
    // Then: Check for a GET parameter (overwrites POST if present)
    if ( isset($_GET[$key]) && $_GET[$key] !== ” ) {
    $value = sanitize_text_field($_GET[$key]);
    }
    if ($value !== ”) {
    update_user_meta( $user_id, $key, $value );
    }
    }
    }

    /* B. Display in the user profile: Custom “Source Information” section */
    function my_show_source_user_profile( $user ) {
    $keys = array(‘affiliate_id’, ‘platform_source’, ‘platform_medium’);
    $labels = array(
    ‘affiliate_id’ => ‘Affiliate ID’,
    ‘platform_source’ => ‘Platform Source’,
    ‘platform_medium’ => ‘Platform Medium’
    );
    ?>

    ID, $key, true ); ?>
    <?php
    }
    add_action( ‘show_user_profile’, ‘my_show_source_user_profile’ );
    add_action( ‘edit_user_profile’, ‘my_show_source_user_profile’ );

    /* B2. Save the fields during profile update */
    function my_save_source_user_profile_fields( $user_id ) {
    if ( ! current_user_can( ‘edit_user’, $user_id ) ) {
    return false;
    }
    $keys = array(‘affiliate_id’, ‘platform_source’, ‘platform_medium’);
    foreach ( $keys as $key ) {
    if ( isset( $_POST[$key] ) ) {
    update_user_meta( $user_id, $key, sanitize_text_field( $_POST[$key] ) );
    }
    }
    }
    add_action( ‘personal_options_update’, ‘my_save_source_user_profile_fields’ );
    add_action( ‘edit_user_profile_update’, ‘my_save_source_user_profile_fields’ );

    /* C. Fill form fields via URL on the frontend (JavaScript)
    Note: Your registration form must include fields with the names:
    form_fields[affiliate_id], form_fields[platform_source], and form_fields[platform_medium]
    ideally as hidden fields.
    */
    add_action(‘wp_footer’, ‘my_fill_fields_from_url’);
    function my_fill_fields_from_url() {
    $keys = array(‘affiliate_id’, ‘platform_source’, ‘platform_medium’);
    $js = “”;
    echo $js;
    }

    /* D. Lead Conversion to WP-Slice:
    Immediately after registration, if an affiliate_id exists,
    convert the visitor into a lead in WP-Slice by calling slicewp_insert_lead().
    Only the essential parameters are passed, including a valid timestamp (formatted as “Y-m-d H:i” without seconds),
    the user’s email, and the original URL in the “entry” field.
    */
    function my_convert_visitor_to_lead( $user_id ) {
    // Retrieve affiliate_id from user meta
    $affiliate_id = get_user_meta( $user_id, ‘affiliate_id’, true );
    if ( empty( $affiliate_id ) ) {
    error_log(“User $user_id has no affiliate_id – skipping lead conversion.”);
    return;
    }

    // Create a timestamp using current_time('mysql') and trim seconds (format: Y-m-d H:i)
    $mysql_date = current_time('mysql'); // e.g., "2025-02-23 14:35:42"
    $created_at = substr($mysql_date, 0, 16); // "2025-02-23 14:35"
    
    // Get the user's email from their data
    $user_data = get_userdata( $user_id );
    $email = $user_data ? $user_data->user_email : '';
    
    // Get the original URL (if needed)
    $protocol = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == "on") ? "https" : "http";
    $original_url = $protocol . "://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
    
    // Prepare lead parameters with minimal required data
    $lead_data = array(
        'affiliate_id' => $affiliate_id,
        'user_id'      => $user_id,
        'email'        => $email,
        'description'  => 'Lead generated from registration form',
        'type'         => 'lead',
        'created_at'   => $created_at,
        'entry'        => $original_url
    );
    
    if ( function_exists( 'slicewp_insert_lead' ) ) {
        $lead_id = slicewp_insert_lead( $lead_data );
        error_log("WP-Slice: Lead (ID: $lead_id) for user $user_id created (via slicewp_insert_lead) with date: $created_at, email: $email, and entry: $original_url.");
    } else {
        error_log("WP-Slice: Function slicewp_insert_lead() not found.");
    }

    }
    add_action( ‘user_register’, ‘my_convert_visitor_to_lead’, 35, 1 );

    Thread Starter growsmartehg

    (@growsmartehg)

    I was able to add a user after formsubmit via custom PHP code to an affiliate via URL parameter. Now I am struggling with creating a lead. I am wondering if SliceWP plugin has a build n function to generate a lead like slicewp_add_lead() or a function to set up a commission like slicewp_add_commission()

    In your documentation you have a lot of get-functions for retrieving data. If you can procide me the code for creating a lead and creating a tcommission that would help me a lot and I have solved this problem or at least I made big steps.

Viewing 2 replies - 1 through 2 (of 2 total)