Skip to content

Instantly share code, notes, and snippets.

@plugin-republic
Created August 14, 2023 12:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save plugin-republic/3cbc85616cdda992d631c6741833b7cf to your computer and use it in GitHub Desktop.
Save plugin-republic/3cbc85616cdda992d631c6741833b7cf to your computer and use it in GitHub Desktop.
<?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