Support

Account

Home Forums ACF PRO verify get_field value and redirect to a page

Solved

verify get_field value and redirect to a page

  • Hi,

    I have this page already created by a programmer who is reachable any more. This php pages is show the below given code.

    <?php echo get_field(‘model_name’); ?> Repair Services<br> </h3>

    I want help here to check for this GET_FIELD and if the value is “iPhone” it would be redirect to iPhone Page and if is “Samsung” it would be redirected to a Samsung Page.

    I am not much of a programmer. Could any help me here.

    Thank you in advance.

    Warm Regards,

    Venkatesha

  • Hello, you can try something like:

    <?php
    $model_name = get_field('model_name');
    
    switch ($model_name) {
        case 'iphone':
    		/* if no page output / print / echo to this function */
    		$redirect = "http://www.yourlinkredirect.com";
    		header("location:$redirect");
        break;
        
    	case 'samsung':
            /* with javascript help */
    		echo '<script language="JavaScript"> 
    					window.location="http://www.yourlinkredirect.com"; 
    			  </script> '
        break;
        
    	case 'others':
            /* ... */
        break;
    }
    ?>

    I hope that helps, hug…

  • Hi @venkatesham

    So to build on @felipeost answer with a real functional example using WordPress functionality. You should be able to paste this into your themes functions.php and just change the ID numbers (1 and 2) to the correct ones for your pages.

    
    function my_custom_redirect(){
    		
    	global $post;
    	$model_name = get_field( 'model_name', $post->ID );
    	
    	switch ($model_name) {
    	    case 'iphone':
    			$redirect = get_permalink(1); // Change 1 to the ID of your iphone page
    			wp_redirect($redirect);
    			exit();
    			break;
    		case 'samsung':
    	        $redirect = get_permalink(2); // Change 2 to the ID of your samsung page
    			wp_redirect($redirect);
    			exit();
    			break;
    	}
    	
    }
    add_action( 'template_redirect', 'my_custom_redirect' );
    
Viewing 3 posts - 1 through 3 (of 3 total)

The topic ‘verify get_field value and redirect to a page’ is closed to new replies.