Created
August 14, 2023 12:31
-
-
Save plugin-republic/3cbc85616cdda992d631c6741833b7cf to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Find which category is protected by a given password | |
* @return term_id | |
*/ | |
function prefix_get_product_category_by_password( $password ) { | |
$terms = get_terms( array( | |
'taxonomy' => 'product_cat' | |
) ); | |
if( $terms ) { | |
foreach( $terms as $index=>$term ) { | |
$term_id = $term->term_id; | |
$passwords = get_term_meta( $term_id, 'wcmo_passwords', true ); | |
$passwords = explode( "\n", str_replace( "\r", "", $passwords ) ); | |
if( $passwords ) { | |
foreach( $passwords as $p ) { | |
if( $p == $password ) { | |
// This is the ID of the protected category | |
return $term_id; | |
} | |
} | |
} | |
} | |
} | |
return false; | |
} | |
/** | |
* Filter the redirect URL to redirect to category protected by specific password | |
*/ | |
function prefix_redirect_url( $url, $password ) { | |
if( $password ) { | |
$cat_id = prefix_get_product_category_by_password( $password="music" ); | |
if( $cat_id ) { | |
$url = get_term_link( $cat_id ); | |
} | |
} | |
return $url; | |
} | |
add_filter( 'wcmo_redirect_url', 'prefix_redirect_url', 10, 2 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment