• I would like to change below code to redirect some external link rather than redirecting to login page. How can I do this?

        <?php
    function vbcr_simple_content_restriction() {
        global $wp_query;
        if( !is_user_logged_in() ) {
            if( is_category() || is_archive() || is_single() || is_tax() || is_tag() || is_feed() || is_comment_feed() || is_attachment() || is_search() ) {
                $option_redirect_to = get_option('redirect-to');
                if(empty($option_redirect_to)) {
                    // retrieve WP login URL
                    $option_redirect_to = wp_login_url();
                }
                $status = "302";
                wp_redirect( $option_redirect_to, $status ); exit;
            }
        }
        ob_flush();
    }
    
    function content_restriction_output_buffer() {
        // this is needed for the redirection
        ob_start();

    As you can see it goes to wp login url:

    $option_redirect_to = get_option('redirect-to');
                if(empty($option_redirect_to)) {
                    // retrieve WP login URL
                    $option_redirect_to = wp_login_url();

    How can I change it to this link: https://example.com/external ?

Viewing 5 replies - 1 through 5 (of 5 total)
  • Change this line
    $option_redirect_to = wp_login_url();
    to
    $option_redirect_to = 'https://example.com/external';

    But it is only going to the login page if the option for redirection is empty. So you could put that address in the option for redirection which your plugin probably has on its settings page.

    Thread Starter dosyhost

    (@dosyhost)

    No there is no settings page.
    The entire code is:

    ?>
    <?php
    function vbcr_simple_content_restriction() {
        global $wp_query;
        if( !is_user_logged_in() ) {
            if( is_category() || is_archive() || is_single() || is_tax() || is_tag() || is_feed() || is_comment_feed() || is_attachment() || is_search() ) {
                $option_redirect_to = get_option('redirect-to');
    			if(empty($option_redirect_to)) {
    				// retrieve WP login URL
                    $option_redirect_to = wp_login_url();
                }
                $status = "302";
                wp_redirect( $option_redirect_to, $status ); exit;
            }
        }
        ob_flush();
    }
    
    function content_restriction_output_buffer() {
    	// this is needed for the redirection
        ob_start();
    }
    
    add_action('init', 'content_restriction_output_buffer');
    add_action('pre_get_posts', 'vbcr_simple_content_restriction');
    
    /**
     * Add options page to settings menu
     */
    
    if ( is_admin() ){ // admin actions
        add_action('admin_menu', 'wp_content_restriction_menu');
    }
    
    function wp_content_restriction_menu() {
        add_submenu_page( 'options-general.php', 'Content Restriction', 'Content Restriction', 'manage_options', 'content_restriction_page', 'content_restriction_page_callback' );
        add_action( 'admin_init', 'register_content_restriction_settings' );
    }
    
    function content_restriction_page_callback() {
        echo '<div class="wrap">';
        echo '<h2>Content Restriction</h2>';
        echo '<form method="post" action="options.php">';
        echo settings_fields( 'content-restriction-group' );
        echo '<label>Redirect to (absolute URL) <input type="text" name="redirect-to" value="'.get_option('redirect-to').'" /></label>';
        echo '<p>If this field is empty, we will redirect people to the login page of your blog.</p>';
        echo submit_button();
        echo '</form></div>';
    }
    
    function register_content_restriction_settings() {
        register_setting( 'content-restriction-group', 'redirect-to', 'vbcr_sanitize' );
    }
    
    function vbcr_sanitize($input) {
    	$input = esc_url($input);
    	return $input;
    }
    ?>
    

    That code shows a settings page called Content Restriction. It also shows a place to set the option for the page to redirect to.

    Thread Starter dosyhost

    (@dosyhost)

    So what should I do?

    Go to the admin panel and look for a menu item called Content Restriction. The code says it’s under Settings > General.
    That should have a place to enter your redirect address.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘How to change redirection way on very basic content restriction?’ is closed to new replies.