Viewing 7 replies - 1 through 7 (of 7 total)
  • Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    What errors are you getting? Undefined function?

    I generally stick to the principle of anything you put in functions.php should be inside callbacks. Chances are if it’s, say at the top of the functions.php file, other areas that have been hooked in via a hook/callback like what defines this function, haven’t run quite yet.

    Alternately, you could just do the fallback that that example uses:

    // Fallback to get_option if CMB2 is not loaded yet.
    $opts = get_option( myprefix_admin()->key, $key, $default );
    
    Thread Starter David Vongries

    (@davidvongries)

    Hey @tw2113,

    thanks for your fast response.

    Not getting any errors, it’s just that the value isn’t available yet. a var_dump gives back NULL.

    The fallback works if a setting has been saved. if no setting has been saved yet, I get the following error:

    Warning: array_key_exists() expects parameter 2 to be array, string given in /Applications/XAMPP/xamppfiles/htdocs/testwp/wp-content/themes/mytheme/inc/options/mytheme-cmb2-global.php on line 256

    which is this line:

    } elseif ( array_key_exists( $key, $opts ) && false !== $opts[ $key ] ) {

    Thanks!

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Hmm, at least based on the function declaration example, null would be the default return value.

    function myprefix_get_option( $key = '', $default = null ) {}
    

    Looks like you’re hitting the standard get_option() call, but we’re actually passing too much in to that function.

    I believe we need to change this line: $opts = get_option( myprefix_admin()->key, $key, $default ); to $opts = get_option( myprefix_admin()->key, $default );

    Thread Starter David Vongries

    (@davidvongries)

    Tried that, no luck ??

    If no settings being saved, the $opts variable is not giving an array, but the actual option as a string

    lets say we have this option (in my case it’s a multicheck) mytheme_get_option('my_option');

    (if no changes has been made) a var_dump of the $opts variable results in string(9) 'my_option' instead of an array. same for $key, which should be just fine.

    Thread Starter David Vongries

    (@davidvongries)

    i changed function myprefix_get_option( $key = '', $default = null ) {

    to

    function myprefix_get_option( $key = '', $default = array() ) {

    and

    $opts = get_option( myprefix_admin()->key, $key, $default );

    to

    $opts = get_option( myprefix_admin()->key, $default );

    as suggested and it seems to do the trick.
    Does that make sense?

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Whatever works, in my books. This isn’t a function you’re going to have potentially overwritten by CMB2 updates, so do what you want with the code ??

    Thread Starter David Vongries

    (@davidvongries)

    Thanks for troubleshooting!

    Keep up the great work! ??

    Best,
    David

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘cmb2 loading too late?’ is closed to new replies.