plugin development – Catching a GET parameter from the URL and save it in the user meta when a user registers

Question

I am trying to catch a GET parameter from the URL and update the user meta when a user registers.

Currently, the approach I am using is:

  1. Call a function that runs in the plugin constructor, which basically will run on every page change or refresh

  2. The function stores the GET params, $_GET['ref_code'] inside a class variable.

  3. The function also adds an action on the user_register hook, which updates the user meta using the class variable that had its value set earlier.


    <?php
    /**
     * Plugin Name:       My Plugin
     * Description:       My Plugin
     * Version:           1.0.1
     */
    
    
    if (!defined('ABSPATH')) {
        echo 'I am a plugin I cannot do anything when called directly.';
        exit;
    }
    
    
    
    class MyPlugin
    {
        private $background_tasks;
        
        private $ref_code;
    
        
        public function __construct()
        {
    
            add_action('wp_enqueue_scripts', array($this, 'my_plugin_site_scripts'));
    
            register_activation_hook(__FILE__, array($this, 'my_plugin_activation'));
            register_deactivation_hook(__FILE__, array($this, 'my_plugin_deactivation'));
    
            $this->check_and_set_referral_code();
        }
    
        public function check_and_set_referral_code()
        {
    
            if(!$this->check_if_is_account_page()) return;
    
            $this->ref_code = $_GET['ref_code'];
    
            add_action('user_register', array($this, 'set_referred_by'), 10, 1);
        }
    
        public function set_referred_by($id)
        {
            if (update_user_meta($id, $this->referred_by_meta_key, $this->ref_code))
                return true;
    
            return false;
        }
    
    
    
        public function my_plugin_site_scripts()
        {
            // Css
            wp_register_style('my_plugin_user_styles', my_plugin_PLUGIN_URL . 'style.css');
            wp_enqueue_style('my_plugin_user_styles');
    
            wp_enqueue_script('my_plugin_vuejs', 'https://cdn.jsdelivr.net/npm/vue/dist/vue.min.js');
            wp_enqueue_script('my_plugin_axios', 'https://cdnjs.cloudflare.com/ajax/libs/axios/0.21.1/axios.min.js');
        }
    
        public function check_if_is_account_page(){
            // Logic to check if the page is my-account page
    
            return true;
        }
    
      
        public function my_plugin_activation()
        {
            error_log('Plugin Activated');
        }
    
    
        public function my_plugin_deactivation()
        {
            error_log('Plugin Deactivated');
        }
    }
    
    
    new MyPlugin();

The code seems to run on a barebones WordPress site with Woocommerce. But this approach just doesn’t feel right and doesn’t run in more complicated environments. I would be very grateful if someone could show me how it can be done in a clean proper bug-free way. Thanks a lot!!

0
Roshan Chapagain 2 weeks 2022-01-05T12:43:38-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse