I am in the process of setting up WordPress with WooCommerce, one of the requirements is a bit of integration that takes a person from another website directly to a specific product. I have set the external product references in tags on the products.
An example incoming query would be:
http://localhost/TestSite/search?q=9404
I have written a short plugin that does this.
I am an average programmer, but very new to PHP, WordPress and WooCommerce. Can you see any potential errors, inefficencies or, security issues?
Any insight would be gladly recieved.
//Tag on Test Product: 98614
//example URL:
//http://localhost/TestSite/search?q=98614
function TagLinker(){
if (isset($_GET['q']))
{
$TagNumber = $_GET['q'];
$params=array(
'post_type' => 'product',
'post_status' => 'publish',
'tax_query' => array( array(
'taxonomy' => 'product_tag',
'field' => 'slug',
'terms' => $TagNumber
)
));
$wc_query = new WP_Query($params);
if ($wc_query -> have_posts() ) {
$wc_query -> the_post();
$product_id = get_the_ID($wc_query->ID);
$url = get_permalink( $product_id );
wp_reset_postdata();
if ( wp_redirect( $url ) ) {exit;}
};
}
}
add_action( 'init', 'TagLinker' );
?>