value only displaying from echo

This is probably a really simple question and Im sorry if it is. However I have been searching and cannot see a solution.

I am using wordpress as a CMS and am enqueue google fonts based on a variable as so

if (!function_exists('opd_load_google_style'))  {
    /* Add Google Fonts */
    global $opd_albaband;
    $google_font = $opd_albaband['typography_h1']['font-family'];

    function opd_load_google_style() {
        if (!is_admin()) {
            wp_register_style('googleFont','http://fonts.googleapis.com/css?family='.$google_font.' 400,700');
            wp_enqueue_style('ggl', get_stylesheet_uri(), array('googleFont') );
        }
    }
    add_action('wp_enqueue_scripts', 'opd_load_google_style');
}

However this produces a Undefined variable $google_font. I can get $google_font to display using echo $google_font but this would not work with wp_register_style...

Am I missing something? Sorry for its simplicity.

Solutions

Your $google_font variable is out of scope so you can't useit in your opd_load_google_style function. Instead your code should look like this

if (!function_exists('opd_load_google_style'))  {

    function opd_load_google_style() {

        /* Add Google Fonts */
        global $opd_albaband;
        $google_font = $opd_albaband['typography_h1']['font-family'];

        if (!is_admin()) {
            wp_register_style('googleFont','http://fonts.googleapis.com/css?family='.$google_font.' 400,700');
            wp_enqueue_style('ggl', get_stylesheet_uri(), array('googleFont') );
        }
    }
    add_action('wp_enqueue_scripts', 'opd_load_google_style');
}

Or you could pass the $google_font variable to your function by doing

function opd_load_google_style($google_font) {
    //Inside your function you now have access to $google_font
}

You just need to shuffle things around a bit, as your opd_load_google_style function does not have access to $google_font.

if (!function_exists('opd_load_google_style'))  {
    function opd_load_google_style() {
        /* Add Google Fonts */
        global $opd_albaband;

        $google_font = $opd_albaband['typography_h1']['font-family'];
        if (!is_admin()) {
            wp_register_style('googleFont','http://fonts.googleapis.com/css?family='.$google_font.' 400,700');
            wp_enqueue_style('ggl', get_stylesheet_uri(), array('googleFont') );
        }
    }
}
Tags: PHP / Wordpress

Similar questions

Syntax for using echo inside an echo (wordpress slider)
this is the piece of code I'm not sure how to deal with: I simply want to insert the first echo instead of InsertHere. The first echo should output the content of a custom field. The second should recall the slider with the specific number inserted in the custom field. When trying different possibilities I only get errors. Can anybody help? Thank y...
How to add php echo within echo shortcode in Wordpress
I'm trying to use an image for my title using the jQuery Collapse-O-Matic plugin, however I am using the do_shortcode echo and am stumbling a bit on how to add an echo within an echo. I've tried a few options including the Heredoc method but I am not sure how to implement with my particular code. I've added my code below, can anyone point me in the...
I am trying to echo an alert when input field was empty, but it alway echo alert
I am using wordpress login. I am trying to call a function when the login or password input was empty. I am trying the following code, no matter I enter something into the input or nothing, it always echos alert, why? I know inside input, value="", means nothing before I enter anything, even if I enter something, on the console, I still see the val...
How do I rewrite an PHP echo inside a PHP echo?
I want to place this code: inside of this code: where it says [email protected] Can someone write this so I can understand?
CSS dosen't work with my PHP code (echo/no echo)
I am trying to convert an html theme to work with Wordpress and I got stuck on a rather simple code and I don't understand why. Why does this code: not work the same as this (CSS doesn't work)?
Converting line, echo within an echo in Wordpress
so I looked through a number of similar topics here and couldn't for the life of me replicate the techniques to this. I'm working Wordpress and instead of adding the following line: ..directly into Wordpress template files, I'm trying to hook in via functions.php. So first I added the function to the hook where I want it to go: Then created the fun...

Also ask

We use cookies to deliver the best possible experience on our website. By continuing to use this site, accepting or closing this box, you consent to our use of cookies. To learn more, visit our privacy policy.