Support » Plugins » Redirect before 404

  • Hello,

    I have a big problem with a plugin I´m writing:
    The visitor should be forwarded to a newsletter registration page, if he visits a post within a blocklist. Unfortunately the forwarding does not work for guests, but when I am logged in, everything works as it should be. The guests only see the 404 page.
    The posts, on which this redirect should be fired, are in „draft“ status!

    public function newsletter_lock_redirect(){
            if (is_single()) {
    	        $cid = $this->nse_get_campaign_id();
    
    	        if(isset($_GET['origin']))
    	        	$origin = filter_var($_GET['origin'], FILTER_SANITIZE_STRING);
    
                global $post;
                global $wpdb;
    	   
    	    $post_tags = get_the_tags($post->ID);
    	            if (!$post_tags)
    	                return;
    
    	            $post_tags_array = json_decode(json_encode($post_tags), true);
    	            if (!$this->in_array_r($this->tag_name, $post_tags_array))
    	                return;
    
    	            $query = 'SELECT * FROM '.$wpdb->newsletter_lock.' WHERE lock_postid = '.$post->ID.' AND lock_status = 1';
    	            $locked = $wpdb->get_row($query, OBJECT);
    
    	            if(empty($locked))
    	                return;
    
    	            if($locked->lock_status == "0")
    	                return;
    
    	            $permalink = get_post_permalink($this->location) . "&cid=" . $cid . "&origin=nl_locked";
    	            wp_safe_redirect( $permalink, $this->status );
    	            exit;
            }
        }
    
    add_action('template_redirect', array($this->public, 'newsletter_lock_redirect'), 10, 2);

    Moderation Note: Hi. I close this thread as you have posted your question in the German support forums but used English to phrase your question. Please either ask your question in the English support forums (Developing with WordPress seems to be best suitable) or rephrase your question in German. Thank you, Bego

    • Dieses Thema wurde geändert vor 6 Jahren, 10 Monaten von nightillusions.
    • Dieses Thema wurde geändert vor 6 Jahren, 10 Monaten von nightillusions.
    • Dieses Thema wurde geändert vor 6 Jahren, 10 Monaten von Bego Mario Garde. Grund: Closing English thread in German support forum
  • Das Thema „Redirect before 404“ ist für neue Antworten geschlossen.