wc_fix_product_attachment_link

Prevent product attachment links from breaking when using complex rewrite structures.

Description

(string) wc_fix_product_attachment_link( (string) $link, (id) $post_id ); 

Returns (string)

Parameters (2)

0. $link (string)
The link.
1. $post_id (id)
The post id.

Usage

  1. if ( !function_exists( 'wc_fix_product_attachment_link' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'woocommerce/includes/wc-core-functions.php'; 
  3.  
  4. // The link. 
  5. $link = ''; 
  6.  
  7. // The post id. 
  8. $post_id = null; 
  9.  
  10. // NOTICE! Understand what this does before running. 
  11. $result = wc_fix_product_attachment_link($link, $post_id); 
  12.  

Defined (1)

The function is defined in the following location(s).

/includes/wc-core-functions.php  
  1. function wc_fix_product_attachment_link( $link, $post_id ) { 
  2. global $wp_rewrite; 
  3.  
  4. $post = get_post( $post_id ); 
  5. if ( 'product' === get_post_type( $post->post_parent ) ) { 
  6. $permalinks = wc_get_permalink_structure(); 
  7. if ( preg_match( '/\/(.+)(\/%product_cat%)$/', $permalinks['product_rewrite_slug'], $matches ) ) { 
  8. $link = home_url( '/?attachment_id=' . $post->ID ); 
  9. return $link;