Help identifying taxonomy of referrer page

Hello,
I was wondering if someone could help me figure out how to add a body class (taxonomy) to a post that matches the taxonomy of the referrer page (all of a custom post type). What I am trying to accomplish is having color coded ( by taxonomy) single product pages that display a list of accessories for that product. However, each accessory may belong to several different products. So if a user clicks on an accessory from my blue styled product page the accessory page should be styled blue. Yet if a user accesses that same accessory from a red styled product page the accessory page should be styled red.

I have been searching to a solution for months and implemented a ridiculous hack approach that works but involves incorporating extra pages (basically duplicate pages, one for each taxonomy) risking duplicate content issues but now I need to go back and find a cleaner solution. Any help would be greatly appreciated. Thanks

  • Kasia Swiderska

    Hello eschmidtke,

    If I understand you correctly you should be able to do that with body_class() function in your body tag https://developer.wordpress.org/reference/functions/body_class/ and filter for that function https://codex.wordpress.org/Plugin_API/Filter_Reference/body_class and rest is to check from what page user came in and depending on that write conditions that will change/add classes to body.
    To check what was previous page you would need to use $_SERVER['HTTP_REFERER'] http://php.net/manual/en/reserved.variables.server.php

    Let me know if you have more questions.

    kind regards,
    Kasia

  • eschmidtke

    Hi Kasia,
    So I got a chunk of time this afternoon to try to work through this based on the references you provided except I really couldn't digest the $SERVER php part but I thought wp_get_referer() may be an option?

    This is what I have so far if you could let me know if I am remotely close since I am a php novice.

    // Adds classes to the array of body classes.
    add_filter('body_class', 'inherit_tax_classes');
    function inherit_tax_classes($classes) {
    	$ref = wp_get_referer();
    	$refterm = get_the_term( $ref );
    	$termclass = '' ";
    
    	if ($refterm == cortak2 ) {
    		 $termclass = 'cortrak-inherit';
    	} elseif ( $refterm == naso ) {
    		 $termclass = 'naso-inherit';
    	} elseif ( $refterm == gastro ) {
        	        $termclass = 'gastro-inherit';
    	 } else {
    	 	//do nothing
    	 }
    	$termclass = $termstyle;
    	    return array_merge( $classes, array( $termstyle ) );
    }

    Of course I get the white screen—debugging error is undefined function get_the_term() which I was expecting because I'm stuck on how the get the term from the referer (above is kinda my pseudo code for what I think I need to do). The but maybe you can give me a hint at this point. Is get_the_term even appropriate here? I usually see it followed by a foreach loop which Im not sure how that would apply here. By the way, thanks for the references instead of just doing it for me, Im really trying to learn how to work with the codex and I think I may have gotten somewhere with this.

    Thanks Kasia

  • Kasia Swiderska

    Hello eschmidtke,

    Please take a look on codex for those functions:
    https://codex.wordpress.org/Function_Reference/wp_get_referer - this one returns url on sucess.
    https://developer.wordpress.org/reference/functions/get_the_terms/ this one gets terms from post object. And in parameters requires Post ID or object. And you try to pass to it url (string that is url).
    So that is not going to work.
    $refterm = get_the_term( $ref );

    You have to get term from the string using of of those functions http://php.net/manual/en/function.split.php - and the when url is split, you pass term to variabled and then you make if statemens.

    Let me know if you have more questions.

    kind regards,
    Kasia

  • eschmidtke

    Sorry, not really following. Since I had last posted I tried something like this after figuring out how to get the id of the referer but I still don't understand how to capture term of referee

    // Adds classes to the array of body classes.
    add_filter('body_class', 'inherit_tax_classes');

    function inherit_tax_classes($classes) {

    if( is_singular('products') ) {

    $ref = url_to_postid(wp_get_referer());
    $terms = get_the_terms( $ref , 'product-category'); // isn't $ref the $post->ID now?
    $termclass =" '';

    if ($ref->ID == '138' ) {
    $termclass = 'cortrak-inherit';
    } elseif ( $ref->ID == '144' ) {
    $termclass = 'naso-inherit';
    } elseif ( $ref->ID == '140' ) {
    $termclass = 'gastro-inherit';
    } else {
    //do nothing
    }
    }
    return array_merge( $classes, array( $termclass ) );
    }

    The last reference you gave me I had already seen but the codex has hardly any examples and I don't understand such brief explanations. Do you know of another resource that may teach these concepts more clearly? Thanks Kasia

  • Kasia Swiderska

    Hello eschmidtke,

    Is your referrer page or post - or it is archive for term? If its not page or post you can't use

    $ref = url_to_postid(wp_get_referer());

    because url_to_postid() will return ID of post or page ( https://codex.wordpress.org/Function_Reference/url_to_postid ). Archives of a taxonomy term does not have ID as this is not post object - its virtual page with archive.

    That is why you have to split url you get and get term from it.
    1) Check what url is returned from wp_get_referer() - you can past it to variable and echo variable:

    $ref = wp_get_referer();
    echo $ref;

    2) You have your url - split it with one of those functions http://php.net/manual/en/function.split.php - because it is url your divider will be /

    $pieces = explode("/", $ref);

    3) use for example var_dump() on $pieces

    var_dump($pieces);

    it will show you on what position your term name is. Please see in examples how explode works http://php.net/manual/en/function.explode.php

    Step 1 and 3 are only for debbuging use - when you have working function you should remove those echo and var_dump()

    4) Then is matter or again passing right array element to variable

    '$term = $pieces[3];`

    and now $term is you term from url and you can work with it.

    I know it looks complicated but writing code and learning to write is very often debugging - checking what functions are returning and if that can be used further. Codex is best source of the knowledge about that - other is reading source code.

    You can check our Academy courses here https://premium.wpmudev.org/academy/ - also sites with tutorials but they usually don't explain how functions are working.

    kind regards,
    Kasia

  • eschmidtke

    Hi Kasia,
    Sorry for the delayed response, I got side tracked. Thanks so much for your help. Just learning the var dump was tremendously helpful. I think I got this to work for the most part and it was a great exercise. The code I am using is:

    // Adds classes to the array of body classes.
    add_filter('body_class', 'inherit_tax_classes');
    
    function inherit_tax_classes($classes) {
    
    if( is_singular('products') && has_term('accessories', 'product-category') ) { 
    
    	$ref = wp_get_referer();
    	$pieces = explode("/", $ref);
    	$parent_page = $pieces[4];
    
    if( $parent_page == 'nasoenteric-product-page') {
    	//	echo "the referal page is naso tubes";
    		$classes[] = 'naso-inherit';
    	} elseif( $parent_page == 'gastrostomy-product-page') {
    	//	echo "the referal page is gastrostomy";
    		$classes[] = 'gastro-inherit';
    	} elseif( $parent_page == 'cortrak-product-page') {
    	//	echo "the referal page is gastrostomy";
    		$classes[] = 'cortrak2-inherit';
    	} elseif( $parent_page == 'gastric-decompression-product-page') {
    	//	echo "the referal page is gastrostomy";
    		$classes[] = 'gastric-decompression-inherit';
    	} elseif( $parent_page == 'entral-product-page') {
    	//	echo "the referal page is gastrostomy";
    		$classes[] = 'entral-inherit';
    	} elseif( $parent_page == 'nicu-product-page') {
    	//	echo "the referal page is gastrostomy";
    		$classes[] = 'nicu-inherit';
    	} elseif( $parent_page == 'accessory-product-page') {
    	//	echo "the referal page is accessories";
    		$classes[] = 'accessories-inherit';
    	} else {
    	//	echo "the referal page is somethng else";
    	} 	
    
    		}
    	    return $classes;
    }

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.