Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added utility to extract WP images from ACF values #134

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

falconmick
Copy link
Contributor

@falconmick falconmick commented Aug 31, 2018

Adds a utility that streamlines extracting WP images from ACF in such a way that allows for:

  1. Extracting the raw URL
  2. Extract Thumnail URL's
  3. Map Extract Thumbnail/raw URL's
  4. Extract Featured Images
  5. Extract captions
  6. Extract alt tag

Example usages:

extractWpImageUrl

import { WP_SIZE_MEDIUM_LARGE } from '../../util/constants';
const mediumLargeThumb = extractWpImageUrl(acfImageProp, WP_SIZE_MEDIUM_LARGE);

mapExtractWpImageUrl

import { WP_SIZE_MEDIUM_LARGE } from '../../util/constants';
const listOfMediumLargeThumbnailURLs = listOfAcfImageProps.map(mapExtractWpImageUrl(WP_SIZE_MEDIUM_LARGE))

extractWpFeaturedImageUrl

same as extractWpImageUrl, except it works on featured images

getWpImageAlt

const imgAlt = getWpImageAlt(someImageProp);

getWpImageCaption

const imgCaption = getWpImageCaption(someImageProp);

By doing this we standardise how we are extracting image URL's and Thumbnails for easier usage by developers. It would be recomended practice that the ACF component extracts the image url or array of url's before passing it down to lower level components as that lets us remove the idea of WP/ACF from our components, thus making them simpler (i.e. in your acf/Hero.js extract the image url's then feed them to the slider as URL's not as WP/ACF objects.

Also do note the new config item of WP_RESOURCE_URL this allows us to configure the location of the images to be either the WP location (this will be used in local dev) or we can change it to be an external CDN OR if we need to proxy the images through NGINX (if we are exposing /feed/ or sitemap.xml on WP via proxying we also need to proxy images otherwise the re-writing of wp.somesite.com will leave 404's for the images). If you were to be proxying the images via nginx you would just change this variable to be WP_RESOURCE_URL = ''

I have a bunch more utility functions i can upload, however for now this is a really important one to standardise on.

@falconmick
Copy link
Contributor Author

constants.js is a file for placing things that don't change per environment but do change per project.

i.e. WP thumbs, some projects have different thumbs, but no matter the environment they will not change (thus we want to keep it out of app-template.js)

// Wordpress
export const WP_URL = 'http://localhost/starward_wp';
export const WP_URL = APP_WP_URL;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The app needs WP_URL and cannot reference a /server/ file, thus we move it to the /app/config/app.js

@@ -11,6 +11,9 @@ export const SEARCH_SLUG = 'search';

// Starward
export const baseURL = `http://${HOST}:${PORT}`;
export const WP_URL = 'http://localhost/starward_wp';
export const WP_RESOURCE_URL = WP_URL; // change to '' and images will attempt to resolve to the app host, then if you have nginx setup you can proxy the WP through the APP hosting's address
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this new config value is required so that we can per-environment change where we fetch images. This allows us to proxy images through someawesomesite.com instead of wp.someawesomesite.com

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant