get php variable from functions php and echo it in theme template files [closed]

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.

Leave a Comment