Myzwer / foothillschurch

Bootcamp II is a wordpress theme (as well as an inside joke) designed to suit the needs of foothillschurch.com. It makes use of webpack, Babel, Sass, Tailwind, Browsersync, PostCSS, ESLint, Stylelint, Prettier and more. It is meant for that site, but if you can use it by all means go for it.
1 stars 1 forks source link

Event Location fields: Use less rigid values and logic #60

Open rain2o opened 1 month ago

rain2o commented 1 month ago

https://github.com/Myzwer/foothillschurch/blob/effc5bbf81497ae99f0561ab7849412fe9272bfd/single-event.php#L81-L105

So, there are a few things that concern me about this. Using maps URLs as values makes things harder to read, the code longer, and potentially easier to break. I've got a few recommendations. I'll post them from lowest to highest effort. Adversely, they will be from least to most flexible.

  1. Use the labels This doesn't address the base concern here, but it can simplify things a bit. You can use the radio option label instead of having a condition for each option and echoing a hard-coded string. Like this
    $selected_location = get_field('event_location');
    if ($selected_location === 'other') {
    the_field('event_location_name');
    } else {
    $field_object = get_field_object('event_location');
    echo $field_object['choices'][$value];
    }

Or, you could choose the "Both (Array)" option for Return Value of the field.

  1. Use simpler values. Instead of using the URLs as values, you could use a key. For example maryville, knoxville, etc.. Then you can have a map somewhere, like a constant you define in a single place with the URLs.
    // e.g. constants.php
    $LOCATION_URLS = [
    'maryville' => 'https://maps.com/someurl',
    'knoxville' => 'https://maps.com/someurl',
    ];

Then use that like $LOCATION_URLS[$selected_location];. The reason this could simplify things is because every time you need to check the selected option, you're not doing if ($selected_location = 'https://some.long/url'), instead you're doing if ($selected_location = 'short_key').

  1. Preferred option - Use dynamic content Make a custom post type called Location. Each Location post could have fields like TItle, Maps URL, and whatever else you want.

Then use the Post Object Field Type to let users select the post itself. You can extract what you need where you need it. This is the most flexible option because as more locations are added, or if a maps URL changes they can manage it in the CMS.

I think I recall seeing some hard-coded location logic in other files earlier as well. So this could also improve that, anywhere you're using locations-related info really.


The 2nd portion where you're echoing the link to the location, you can simplify this right now too (though this will no longer be valid if you make the changes above).

if ($selected_location === 'other') {
    the_field('event_location_link');
} else {
    echo $selected_location;
}