mercredi 20 avril 2016

When to use filter action and when normal function in WordPress?

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 dont get:

  1. 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.

  2. Should I make this function action? Almost all guides say actions do not return variable however the very example in http://ift.tt/1SvwSyA shows functions that RETURNS variable. This confuses me. Where and why is this variable returned when actions dont 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 action then why even use filter?

  3. My guess is I need to use 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 array? It is a very simple function, why use filters and not normal function?

  1. 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 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 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.

Thanks for any help.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire