Ask your WordPress questions! Pay money and get answers fast! (more info)

What's wrong with this code? WordPress

  • SOLVED

What's wrong with this code?
Gives an error when i try and add it as a plugin.
Also, any way of optimising it?
Thanks.

<?php
/*
Plugin Name: Site Block
Plugin URI:
Description: Block all for for logged-out users except for register pages
Version:
Author:
Author URI:
License:
License URI:
*/

function redirect_to_specific_page() {

if ( !is_user_logged_in() ) {

if ( !is_page ( array ( '916', '940', '1015', '1017', '1044', '1046', '1048', '1051', '1388' ) ) {

wp_redirect( 'http://www.example.com/login/', 302 );
exit;
}

}

if ( is_user_logged_in() && rcp_is_expired() ) {

if ( (rcp_get_subscription_id( $GLOBALS['user_ID'] ) == 1) || (rcp_get_subscription_id( $GLOBALS['user_ID'] ) == 2) || (rcp_get_subscription_id( $GLOBALS['user_ID'] ) == 3) || (rcp_get_subscription_id( $GLOBALS['user_ID'] ) == 7) || (rcp_get_subscription_id( $GLOBALS['user_ID'] ) == 8) ) {

if ( !is_page ( array ( '916', '940', '1015', '1017', '1044', '1046', '1048', '1289', '1291', '1322', '1051', '1388' ) ) {

wp_redirect ( 'http://www.example.com/expired1/', 302 );
exit;
}

}

if ( (rcp_get_subscription_id( $GLOBALS['user_ID'] ) == 4) || (rcp_get_subscription_id( $GLOBALS['user_ID'] ) == 5) || (rcp_get_subscription_id( $GLOBALS['user_ID'] ) == 6) ) {

if ( !is_page ( array ( '916', '940', '1015', '1017', '1044', '1046', '1048', '1289', '1291', '1217', '1051', '1388' ) ) {

wp_redirect ( 'http://www.example.com/expired2/', 302 );
exit;
}

}
}
}

add_action( 'template_redirect', 'redirect_to_specific_page' );

Answers (2)

2017-01-06

Kyle answers:

if ( !is_page ( array ( '916', '940', '1015', '1017', '1044', '1046', '1048', '1051', '1388' ) ) {

Should be

if ( !is_page ( array ( '916', '940', '1015', '1017', '1044', '1046', '1048', '1051', '1388' ) ) ) {

That needs to apply to all threee of the page checks


Kyle comments:

Also, replace the $GLOBALS['user_id'] stuff with

$user_id = get_current_user_id();

2017-01-06

Shoeb mirza answers:

You forgot to close bracket of array of is!Page.. check