Automattic\WooCommerce\Blocks\BlockTypes
ProductFilters::get_canonical_url_no_pagination
Get the canonical URL without pagination.
Method of the class: ProductFilters{}
No Hooks.
Returns
string. Canonical URL without pagination.
Usage
// private - for code of main (parent) class only $result = $this->get_canonical_url_no_pagination( $filter_params );
- $filter_params(array) (required)
- Filter parameters.
ProductFilters::get_canonical_url_no_pagination() ProductFilters::get canonical url no pagination code WC 11.1.2
private function get_canonical_url_no_pagination( $filter_params ) {
$canonical_url_no_pagination = is_singular() ? get_permalink() : get_pagenum_link( 1 );
$decoded_url = html_entity_decode( $canonical_url_no_pagination, ENT_QUOTES, get_bloginfo( 'charset' ) );
$parsed_url = wp_parse_url( $decoded_url );
// If there are active filters, $parsed_url['query'] is empty for page or post but not empty for archives.
if ( empty( $filter_params ) || empty( $parsed_url['query'] ) ) {
return $decoded_url;
}
foreach ( array_keys( $filter_params ) as $key ) {
$parsed_url['query'] = remove_query_arg( $key, $parsed_url['query'] );
}
$url = '';
if ( isset( $parsed_url['scheme'] ) ) {
$url .= $parsed_url['scheme'] . '://';
}
if ( isset( $parsed_url['host'] ) ) {
$url .= $parsed_url['host'];
}
if ( isset( $parsed_url['port'] ) ) {
$url .= ':' . $parsed_url['port'];
}
if ( isset( $parsed_url['path'] ) ) {
$url .= $parsed_url['path'];
}
if ( ! empty( $parsed_url['query'] ) ) {
$url .= '?' . $parsed_url['query'];
}
if ( isset( $parsed_url['fragment'] ) ) {
$url .= '#' . $parsed_url['fragment'];
}
return $url;
}