1

I submit a WordPress theme for Themeforest and one of the reasons for rejection was as follows:

Globals should always be within a function or a class and should be used restrictively & only if theme really needs to. It's highly recommended not to use them at all just to keep things out of the global namespace, they're poor coding practice.

Im using Smof Option Framework for theme options and here is a sample code from one of my theme files:

global $smof_data;
$td_header_manager_wide = $smof_data['td_header_blocks']['enabled'];

if ( $td_header_manager_wide ) {

        foreach ( $td_header_manager_wide as $key=>$value ) {

            switch( $key ) {

                case 'block_main_menu_wide': // wide menu
                echo '<div id="td-sticky" class="wide-menu ' . $td_sticky . '">';
                echo '<div id="wide-menu">';
                include( get_template_directory() . '/parts/menu-header-logo.php');
                echo '</div>';
                echo '</div>';
                break;
  }
}

It seems that global $smof_data; should be within a function but I'm really lost here how to accomplish this task to avoid include global $smof_data; in all of my theme files where I need it.

Update code using @jgraup method:

function matilda_customize_styles() {

    $css = '<style type="text/css" media="screen">';
    if ( ! empty( SMOFData::get( 'td_body_font_family' ) ) 
        && SMOFData::get( 'td_body_font_family' ) != 'none' ) 
    {
        $css .= 'body{font-family:' . esc_html( SMOFData::get( 'td_body_font_family' ) ) . ';}';
    }

}

Any help will be greatly appreciated. Thanks.

0

2 Answers 2

2

If you're dealing with framework that holds values in a global variable then there isn't much you can do about it. Here is an example of wrapping the variable in a static getter.

if ( ! class_exists( 'SMOFData' ) ):

    class SMOFData {

        static public function is( $key, $compare ) {
            $value = static::get( $key );
            return $value === $compare;
        }

        static public function not( $key, $compare ) {
            $value = static::get( $key );
            return $value !== $compare;
        }

        static public function has( $key ) {
            $value = static::get( $key );
            return ! empty( $value );
        }

        static public function get( $key ) {

            global $smof_data;

            if ( ! isset( $smof_data ) ) {
                return null;
            }

            return isset( $smof_data[ $key ] ) ? $smof_data[ $key ] : null;
        }

    }

endif; // SMOFData

To access the data just use

echo SMOFData::get('td_header_blocks')['enabled'];

function matilda_customize_styles() { 
    $css = '<style type="text/css" media="screen">';
    $td_body_font_family = SMOFData::get( 'td_body_font_family' ); 
    if ( ! empty( $td_body_font_family ) && $td_body_font_family != 'none' ) {
        $css .= 'body{font-family:' . esc_html( $td_body_font_family ) . ';}';
    } 
}

function matilda_customize_styles() { 
    $css = '<style type="text/css" media="screen">';
    if ( SMOFData::has( 'td_body_font_family' ) && SMOFData::not( 'td_body_font_family', 'none' ) ) {
            $css .= 'body{font-family:' . esc_html( SMOFData::get( 'td_body_font_family' ) ) . ';}';
    }
}

If you just want you're own globals you can wrap that in a class as well.

if ( ! class_exists( 'ThemeData' ) ):

    class ThemeData {
        private static $_values = array ();

        static public function get( $key ) {
            return isset( static::$_values[ $key ] ) ? static::$_values[ $key ] : null;
        }

        static public function set( $key, $value ) {
            static::$_values[ $key ] = $value;
            return $value;
        }
    }
endif; // ThemeData


// setter
ThemeData::set('foo', 'bar');

// getter
echo ThemeData::get('foo');
10
  • Thanks! The first method works great in my sample code above but I really don't know how to use the class in the following code: if( !empty( $smof_data['td_body_font_family'] ) and $smof_data['td_body_font_family'] != 'none' ) { $css .= 'body{font-family:'. esc_html( $smof_data['td_body_font_family']) .';}'; }Can you give me a little more help with this?
    – Knott
    Feb 26, 2016 at 9:34
  • Thanks. I guess your code is write but since most of my code where I use $smof_data['key'] are within a function, using SMOFData::get( 'key' ) I get a fatal error: Can't use function return value in write context Any clues?
    – Knott
    Feb 26, 2016 at 9:59
  • The error occurs on line if ( ! empty( SMOFData::get( 'td_body_font_family' ) ) but your update sample seems to work now. Thank you!
    – Knott
    Feb 26, 2016 at 10:22
  • Yes, they are. Best!
    – Knott
    Feb 26, 2016 at 10:57
  • Just a weird issue: on my xampp local server all things works fine but on a live server I get an error: Parse error: syntax error, unexpected '[' The line which its mocking about is this one:$td_header_layout = SMOFData::get('td_header_blocks')['enabled']; Any suggestion? May be the PHP version? Any help will be greatly appreciated. Thanks.
    – Knott
    Feb 27, 2016 at 19:52
3

First, I must congratulate the reply from Themeforest, that really deserves a beer. Globals are evil and one should never create globals and dirty the global space anymore that it already is. WordPress has made a huge mess of this already. Don't add to this mess.

One of the easiest ways to get pass the global issue is to create a function which you can use anywhere where you would need it. Here is a sample

function this_is_my_global()
{
    return $my_global_var = 'This is my global value';
}

You can then simply call this_is_my_global(); where you need it.

This is just something very basic. I would recommend that you read @kaiser and @gmazzap answers to the following question

That should give a more complex, reliable means to pass variables between templates

2
  • Thanks for your answer too. I'm still a litle confused. Can you update your answer according to my concrete example using $smof_data in your sample code? Thanks!
    – Knott
    Feb 26, 2016 at 10:02
  • I do not know your framework, so I do not know what $smof_data holds or where it is defined Feb 26, 2016 at 10:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.