I've been reading about actions and filters for the past hour and I still can't find the answer I am looking for.
Let say I have a WP theme/site about 2 cities. London and Washington. These two are top parent pages to every other page. (about, what to visit, info etc) Certain content is different for each city.
For example:
- in footer we display city name for every children page.
- promoted content on front page in 3 divs (content loaded with
WP_Query
) - different header color for different city
- etc
This content is not stored in database so I though I'd make a simple function in functions.php file, like this (simplified, also I left out part when I get the slug):
function cityData () {
$currentCitySlug = 'xx';//code that finds top parent & returns 'london' or 'washington';
switch ($currentCitySlug ) {
case 'london':
$smth['footer'] = 'London';
$smth['frontpage-featured'] = array(190,200,201); //post IDs
break;
default: //washington
$smth['footer'] = 'Washington';
$smth['frontpage-featured'] = array(150,154,167);
break;
}
return $smth;
}
So here is what I don't get:
Where should I call
cityData()
so the returned variable is available everywhere in my theme? Do I use some global variable array in let say index.php, then header.php etc or anywhere where I need it (and then let say in footer do echo$somecity['footer']
)? I read we should avoid global variables.Should I make this function action? Almost all guides say actions do not return variable, however, the very example in https://codex.wordpress.org/Plugin_API#Create_an_Action_Function shows functions that Return variable. This confuses me. Where and why is this variable returned when actions don't return variable? Also, hook it to what WordPress action?
init
?after_setup_theme
? There are millions of these actions, how to know which one? If you can return variable from the action then why even use a filter?-
My guess is I need to use a filter. However, this is my main dilemma: should I use filter OR should I just use the ordinary function call like:
$city = cityData(); echo $city['footer'];
every time I need this array in my theme?
or should I use add_filter to this function in functions.php then call it with apply_filters function in my theme every time I need some data from an array? It is a very simple function, why use filters and not normal function?
- How do I call this function (or apply filters) and set this variable only once and then simply echo it like
$city['somesubfield']
whenever I need it anywhere in my code? Is this better than applying filter or calling function every time I need it?
I have a feeling I am over complicating of doing it completely wrong. Maybe I should even add custom fields to top parent pages (London,Washington) then get this data directly from the database? But this just makes additional database calls when maybe not needed.
If you had to store some additional data for 2 cities where would you store it? Remember, this additional data is not only needed when these 2 pages are displayed but for EVERY child page of these two top pages. Manually in function? In the database, custom fields for these 2 pages then read from DB every time you need it? In some special database field, like options? There are no more than 5 different items needed to be stored for each city.