Site logo

Archived topic

Add function php in Element > hook ?

3 replies · Started by Yann on May 20, 2021

Viewing posts 1–4 of 4

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!

This archived topic is closed to new replies.