Creating Wordpress Shortcode with Variable

please forgive me I’m relatively new to PHP…

I would like to create a shortcode called [city-name] and have tried placing the following code in my functions.php file:

function get_city_name() {
return $current_cityinfo['cityname'];
}
add_shortcode( 'city-name', 'get_city_name' );

Then entered the [city-name] shortcode into a page to test and there is no luck…

Is there something wrong with my syntax…. I have entered the following into my category pages and that works absolutely fine:

<h1 class="loop-title"><?php single_cat_title(); ?> - <?php echo $current_cityinfo['cityname'];?></h1>

I hope someone can help me out here… been searching for over 2 hrs now and no luck so thanks in advance!

JAke

3 Answers
3

You need to get $current_cityinfo first. As in:

$current_cityinfo = get_option('current_cityinfo');

Or add this code:

global $current_cityinfo;

above your return line.

Leave a Comment