0

I am trying to store the baseurl to options table in wordpress for non logged in users. Basically if the user was referred to url mysite.com/go/affilatename I want to store affilatename (baseurl) to the options table... I tried storing it as an option with their ip as the key and then the baseurl as the value

Basically its an affiliate/referral site where signed up affiliates refer their customers to our site that sells mobile data subscriptions... but when they sign up as an affiliate they want their own logo as site logo and stylesheet... rather than create pages for each of them in Divi they all use one global page as their product page e.g. mysite.com/go/affilatename... but i load their branding on that page and change all links to pages as in terms conditions to load their branding... works well so far if signed in

The idea is that it then serves a site logo and css based on the affilatename value, also then it rewrites all WordPress slugs by adding /go/affilatename to the end of every link or post on the site

This is working for logged in users BUT I cant get it to to work for non logged in users

I tried setting transients and also set option but the value somehow always gets set to ?wc-ajax=get_refreshed_fragments and not the value I am passing to the set transient or set option

 if( !$user_ID) { $affiliate = basename($_SERVER['REQUEST_URI']); $user_ip = $_SERVER['REMOTE_ADDR']; set_transient ('affiliate',$affiliate, 60*60*1) ; $client = $_SERVER['REMOTE_ADDR']; update_option( $user_ip, $affiliate ); } 

The idea is that I can that a[[end my url rewrite strings with the value stored and server the css and logo based on that value for that IP address

6
  • 1
    Note that doing this opens you up to rehaust exhaustion attacks, a user can repeatedly visit your site adjusting the URL to fill your database with junk data and slow down your site by making this option very very big. What are you trying to build that requires this? Is it a customer portal? You've asked how to fix your solution but did not explain what the original problem was Commented Aug 30, 2023 at 14:19
  • Thank you for advice re junk data.... the idea is to load css/site logo based on a referal link eg.site.com/go/thiscompany... i use a dynamic divi page but load the logo & also css based on the thiscompany variable ... as i said works well if user is signed in as i get that from his user meta ... but non signed in users no... BTW the links send out to someone to sign up is privately sent to them and the site has a password on all pages that is sent to them too.. Commented Aug 30, 2023 at 14:32
  • so what i was trying to do is get the baseurl when the user clicks on the link sent to him... store it somewhere then 1> load logo & css from example siteurl/branding/thiscompany.css 2> rewrite all links he clicks on afterwards (still logged out) e.g. sign up page to site.com/sign_up/ to site.com/sign_up/go/thiscompany (once again to load correct logo and css) Commented Aug 30, 2023 at 14:32
  • that doesn't help as you already said that in your question. What I was hoping for was the user story, you would never tell the client about options and css etc, right now what you're describing does not sound like a logical thing to do but if you shared what your site did then it would answer a lot of questions as well as open up suggestions, e.g. maybe custom post types would be more appropriate. What are you trying to build? Right now your technical only description is confusing and refers to things such as logos/css that are missing etc Commented Aug 30, 2023 at 16:20
  • i get you and appreciate your input Basically its an affiliate/referral site where signed up affiliates refer their customers to our site that sells mobile data subscriptions... but when they sign up as an affiliate they want their own logo as site logo and stylesheet... rather than create pages for each of them in Divi they all use one global page as their product page e.g. mysite.com/go/affilatename... but i load their branding on that page and change all links to pages as in terms conditions to load their branding... works well so far if signed in Commented Aug 30, 2023 at 17:24

1 Answer 1

0

Ok, I managed to get this far and hope it helps someone else a bit... so I solved the non-logged-in user issue like this (in theme functions.php or in my case my plugin functions)

function first_visit(){ $ip = $_SERVER['REMOTE_ADDR']; $value = get_option($ip); if ($value == '') { update_option($ip,1); // test $affiliate = basename($_SERVER['REQUEST_URI']); // in my case is site.com/signup/whatevercompany which returns whatevercompany update_option($ip,$affiliate); // writesoption key = ip and whatevercompany as value to site options table } } /**** so in case user not logged in i get the referral name from wp_options table ****/ $user_query = new WP_User_Query( $args ); $user_ID = get_current_user_id(); if ( !$user_ID) { first_visit(); $ip = $_SERVER['REMOTE_ADDR']; $affiliate = get_option($ip); if ($affiliate == '') { $affiliate = 'default'; //set to a default.css/logo update_option($ip,$affiliate); } $affiliatelogo ='/branding/'.$affiliate.'/logo.png'; $affiliatecss ='/branding/'.$affiliate.'/custom.css'; } /***** then in my themes (DIVI) child theme header file i add this .... this loads the css for that whatevercompany name ****/ function my_theme_enqueue_styles() { wp_enqueue_style( 'parent-style', get_template_directory_uri() . '/style.css' ); } add_action( 'wp_enqueue_scripts', 'my_theme_enqueue_styles' ); /* Load child theme stylesheet with query string */ function enqueue_child_style() { $style = get_stylesheet_directory() . '/style.css'; $cache_buster = date("YmdHi", filemtime( $style ) ); wp_dequeue_style('divi-style'); wp_deregister_style('divi-style'); } add_action('wp_enqueue_scripts', 'enqueue_child_style', 15); $path = ''; wp_enqueue_style( 'css-customer', esc_url($path.'/branding/'.$affiliate.'/custom.css' )); 

Now I'm trying to append any link or URL in site (slugs) say from (until at least user is registered and signed up)

mysite.com/product/topup30gb/ to mysite.com/product/topup30gb/go/affiliate/ 

I have tested above and so far so good... I will have to clean up site options after user proceeds and registers via buying the product by deleting keys for his IP in site meta also all empty keys... once he signs up I add his referrer to his user meta.

1
  • i know its a bad hack but hey im trying Commented Aug 30, 2023 at 17:41

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.