Archived topic
Add function php in Element > hook ?
3 replies · Started by Yann on May 20, 2021
Hi,
I need to add this Php code :
function woocommerce_hide_non_registered() {
if( ( is_shop() || is_single() || is_product() || is_product_category() || is_category() || is_archive() || is_post_type_archive() || is_singular( $post_types = 'page' ) && ! is_front_page() && ! is_page('5779') || is_tag() ) && ! is_user_logged_in() ) {
wp_redirect( site_url( '/' ) );
exit();
}
}
add_action( 'template_redirect','hide_non_registered' );
Can't put that in function.php because it's a multisite installation, and the php code is about one sigle site.
I try to add a hook in Elements, but it doesn't work.
Can help ?
Thanks
Hi there,
not sure if this will work but try adding just the functions code to the Hook ie.
<?php
if( ( is_shop() || is_single() || is_product() || is_product_category() || is_category() || is_archive() || is_post_type_archive() || is_singular( $post_types = 'page' ) && ! is_front_page() && ! is_page('5779') || is_tag() ) && ! is_user_logged_in() ) {
wp_redirect( site_url( '/' ) );
exit();
}
?>
Then in Hook list select Custom Hook and in the field provided add: template_redirect
Then set the Display Rules to Entire Site.
Not sure about this though....
Just try it,
Apparently doesn't work(the field of custom hook doesn't take "template_redirect", when I save, the field go back to blank)
Any other idea ?
Thanks!
Yep - didn't think it would as it opens a security risk.
Aside of creating an independent child theme for that site, theres this method:
Or by using the Code Snippets pliugin:
https://wordpress.org/support/topic/multi-site-run-snippet-on-one/#post-10059829