This may seem like a silly question but I cannot figure this out.

I’ve created a variable in my functions.php – and I’m trying to echo it in my template files.

I’m guessing it’s because of scope.

What’s the simpliest what to echo a variable or create a function to allow me to output this facebook ID in various template files.

This is what I’ve currently got in my functions.php…

$fb_app_id = '68366786876786';

And this is how I was trying to echo it…

<?php echo $fb_app_id; ?>

Any ideas would be hugely helpful, thanks you very much

3 s
3

If you don’t have access to the value, it’s likely a scope issue. Make sure you globalize it first:

<?php
global $fb_app_id;
echo $fb_app_id;
?>

Alternatively

I’m not a fan of global variables, so the alternative I recommend is using WordPress’ built-in filter mechanism. You add the filter in your functions.php file and apply it where needed.

In functions.php:

add_filter( 'fb_app_id', 'return_fb_app_id' );
function return_fb_app_id( $arg = '' ) {
    return '68366786876786';
}

In your template files:

echo apply_filters( 'fb_app_id', '' );

Or use an action hook

In functions.php

add_action( 'fb_app_id', 'echo_fb_app_id' );
function echo_fb_app_id() {
    echo '68366786876786';
}

In your template files:

do_action( 'fb_app_id' );

Whether you use an action hook or a filter is entirely up to you. I recommend a filter because it can return a value. This is far more flexible than just injecting echo calls in your template.

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *