Support

Account

Home Forums ACF PRO Use value from options page in functions.php

Helping

Use value from options page in functions.php

  • Hi,

    I’m trying to check if an ACF Field from the Option page is true in functions.php, but I can’t get it to work. Below is my code from functions.php. Any ideas?

    	function my_acf_save_post($post_id){
    			
    		$var = get_field('test_switch', 'option' );
    			
    		if($var == 'true' ) {
    			
    			function codex_custom_init() {
    	
    				register_post_type(
    					'evenementen', array(
    					    'labels' => array('name' => __( 'Feesten en evenementen' ),
    										'singular_name' => __( 'Feest of evenement' ),
    										'add_new' => __( 'Feest of evenement toevoegen' ),
    										'add_new_item' => __( 'Feest of evenement toevoegen' ),
    										'edit_item' => __( 'Feest of evenement bewerken' ),
    										'new_item' => __( 'Feest of evenement toevoegen' ),
    										),
    					    'public' => true,
    					    'has_archive' => true,
    					    'supports' => array('title', 'editor', 'thumbnail')
    					 )
    				);
    				add_action( 'init', 'codex_custom_init' );
    			};
    		}
    	};
    	
    	add_action('acf/save_post', 'my_acf_save_post', 'option');
  • This function is only running during acf/save_post, so you’re init action will never be added unless a post is being saved.

    
    <?php 
        
      function codex_custom_init() {
        $var = get_field('test_switch', 'option' );
        if($var == 'true' ) {
      
        register_post_type(
          'evenementen', array(
              'labels' => array('name' => __( 'Feesten en evenementen' ),
              'singular_name' => __( 'Feest of evenement' ),
              'add_new' => __( 'Feest of evenement toevoegen' ),
              'add_new_item' => __( 'Feest of evenement toevoegen' ),
              'edit_item' => __( 'Feest of evenement bewerken' ),
              'new_item' => __( 'Feest of evenement toevoegen' ),
            ),
            'public' => true,
            'has_archive' => true,
            'supports' => array('title', 'editor', 'thumbnail')
           )
          );
        }
      }
      add_action( 'init', 'codex_custom_init' );
    ?>
    
Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘Use value from options page in functions.php’ is closed to new replies.