PHP not always getting value from $_GET
I have a WordPress shortcode that will display the appropriate image based on some values passed through the query string. It gets the value with $_GET['utm_content']
.
Not sure why it would be a browser thing, but… it works as expected 100% of the time in non-incognito Chrome. With Chrome in incognito mode, Firefox and Safari, it only gets the value a percentage of the time. When it doesn’t get it isset($_GET['utm_content'])
fails….
Here’s the entire function. You can see my debugging code at the end.
add_shortcode('bene_show_img', 'bene_show_img'); function bene_show_img( $atts, $content = null ) { $utm_campaign = isset($_GET['utm_campaign']) ? $_GET['utm_campaign'] : 'not set'; $utm_content = isset($_GET['utm_content']) ? $_GET['utm_content'] : 'not set'; extract( shortcode_atts( array( 'base_path' => 'wp-content/uploads', 'img_path' => substr( substr_replace( substr($utm_campaign, 0, 8) , '/', 4, 0 ), 0, 7 ), 'img_base_name' => substr($utm_campaign, 8), 'img_ver' => $utm_content, 'img_ext' => 'png' ), $atts ) ); $bp = esc_attr($base_path); $p = esc_attr($img_path); $bn = esc_attr($img_base_name); $v = esc_attr($img_ver); $e = esc_attr($img_ext) . '?r=' . rand(); // TODO: if the final path does not point to an image, try decreasing then increasing the // month by 1... // Return a string to display on the page $tag = <<<EOD <img width="1024" height="535" src="/$bp/$p/$bn-$v-1024x538.$e" class="attachment-large size-large" alt="" srcset="/$bp/$p/$bn-$v-1024x538.$e 1024w, /$bp/$p/$bn-$v-300x175.$e 300w, /$bp/$p/$bn-$v-768x401.$e 768w, /$bp/$p/$bn-$v.$e 1200w" sizes="(max-width: 1024px) 100vw, 1024px"> EOD; return "<h2>XX $utm_content $utm_campaign XX</h2>" . $tag; //return $tag; }
What am I missing?
In case my error not overly visible, you can see the behavior live at: http://wordpress-310389-952347.cloudwaysapps.com/20200709-1/?utm_campaign=20200709page&utm_content=1a
UPDATE
Just to be sure it wasn’t any sort of weird shortcode thing, I added the following to functions.php and get consistent results when viewing the URL above
add_shortcode('getit', 'getit'); function getit() { return $_GET['utm_content']; } echo '<pre>' . do_shortcode('[getit]') . '</pre>'; echo '<pre>' . getit() . '</pre>';