• Resolved Quentin

    (@qur)


    Hello,

    I am trying to use a theme options page to include or not files in my functions.php.
    But I get this : Fatal error: Call to undefined function cmb2_get_option().
    I can’t understand the different answers I have found around. I guess this is because, functions.php is loaded before the plugins?

    This is what I need:
    if ((myprefix_get_option( ‘settings_events’ ) == ‘on’)) {
    require_once($tempdir .’/inc/myprefix_calendar.php’);
    }

    Or maybe there is a better way and not include this in functions.php but can’t see where. How can I use my theme options in functions.php ?

    Best regards,

    Quentin

    https://wordpress.org/plugins/cmb2/

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Author Justin Sternberg

    (@jtsternberg)

    Try this:

    function myprefix_cmb2_after_init_include_calendar() {
    	if ( 'on' === myprefix_get_option( 'settings_events' ) ) {
    		require_once( $tempdir .'/inc/myprefix_calendar.php' );
    	}
    }
    add_action( 'cmb2_after_init', 'myprefix_cmb2_after_init_include_calendar' );

    Basically, you can’t use CMB2 functionality until it’s been initiated. If you use this hook, you can be sure all CMB2 functionality has been bootstrapped.

    Thread Starter Quentin

    (@qur)

    Hello,

    Thanks for the reply. It seems to be partly working.
    But maybe what I am trying to do does not work like that.
    I have an option page with a checkbox. When it is checked I wanted to include the file myprefix_calendar.php which loads a custom post type “Events”. But maybe this can’t be done like this as it is loaded after init, does cpt need to be load inside init?

    Best regards,

    Quentin

    Thread Starter Quentin

    (@qur)

    Maybe I should activate my custom post types and have an option to show or hide it in admin and frontend.

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Yes, post types should be registered on the init hook, as per the register_post_type codex page.

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘call cmb2_get_options in functions.php’ is closed to new replies.