• Resolved pejamide

    (@pejamide)


    I want to define some data in functions.php of theme child and to be able to read in my some code snippets those data. I tried to use $GLOBALS[‘bbl_var1’], $GLOBALS[‘bbl_var2’] and $GLOBALS[‘bbl_var3’], but they are undefined in my code snippets!?

    What is the best solution for such global data?

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author Shea Bunge

    (@bungeshea)

    Globals generally aren’t a great idea – they tend to be a little messy and difficult to trace.

    However, I think they should work in this situation, with the caveat that snippets are loaded before the theme functions.php file. In order to access this data, you might need to use an action hook that runs after the theme is loaded:

    add_action( 'after_setup_theme', function () {
        $value = $GLOBALS['bbl_var1'];
        // do something with the data
    } );
    Thread Starter pejamide

    (@pejamide)

    Thank you for your reponse, but I have two new questions:
    1:
    What and how I should use instead of $GLOBALS variables? Please give me two examples:
    The one in functions.php for initialization of three variables (bbl_var1, bbl_var2 and bbl_var3) and

    the other for PHP snippet in order to read three variables: bbl_var1, bbl_var2 and bbl_var3.

    Thread Starter pejamide

    (@pejamide)

    2:
    If I use three $GLOBALS[‘bbl_var1] = ‘111’, $GLOBALS[‘bbl_var] = ‘222’ and $GLOBALS[‘bbl_var3] = ‘333’, where and what I should put such initialization in functions.php?
    And what should is code in a PHP snippet in order to read three $GLOABLS variables?

    Thread Starter pejamide

    (@pejamide)

    I created the functions.php for my theme-child:

    <?php
    add_action('wp_enqueue_scripts', 'child_enqueue_styles',
      99);
    function child_enqueue_styles(){
      $parent_style = 'parent-style';
      wp_enqueue_style($parent_style,
        get_template_directory_uri() . '/style.css');
      wp_enqueue_style('child-style',
        get_stylesheet_directory_uri() . '/style.css',
        array($parent_style));  }
    if(get_stylesheet() !== get_template()){
      add_filter('pre_update_option_theme_mods_' .
        get_stylesheet(),
        function($value, $old_value){
          update_option('theme_mods_' . get_template(),
          $value);
    
          return $old_value;}, 10, 2);
      add_filter('pre_option_theme_mods_' . get_stylesheet(),
        function($default){return get_option('theme_mods_' .
          get_template(), $default);});}
    /*---Initialize globald data ---*/  add_action('after_setup_theme', function(){
      $value1 = $GLOBALS['bbl_var1'];
      $value2 = $GLOBALS['bbl_var2'];
      $value3 = $GLOBALS['bbl_var3'];});
    $GLOBALS['bbl_var1'] = 'JdV111';
    $GLOBALS['bbl_var2'] = 'JdV222';
    $GLOBALS['bbl_var3'] = 'JdV333';
    ?>

    And my PHP-snippet:

    <?php
    echo '<br>' . $GLOBALS['bbl_var1'];
    echo '<br>' . $GLOBALS['bbl_var2'];
    echo '<br>' . $GLOBALS['bbl_var3'];
    

    I got the PHP message:

    Warning: Undefined array key "bbl_var1" in /......./wp-content/plugins/insert-php-code-snippet/shortcode-handler.php(100) : eval()'d code on line 2

    What should I have done wrong?

    Plugin Author Shea Bunge

    (@bungeshea)

    functions.php loads after code snippets, so if you want to access globals set there, you’ll probably need to use an action hook:

    add_action( 'init', function () {
        echo '<br>' . $GLOBALS['bbl_var1'];
        echo '<br>' . $GLOBALS['bbl_var2'];
        echo '<br>' . $GLOBALS['bbl_var3'];
    } );
Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘How to pass data to code snippet?’ is closed to new replies.