سؤال

Im making a wordpress theme with a feature to allow users to choose their own fonts from Google. Obviously because this is a wordpress theme there is no way to create a new API key for each installation.

All I need is the list of fonts in alphabetical order.

Bonus points if someone gives me a way to group them by category!

هل كانت مفيدة؟

المحلول

Check out the developer API. You can query it for a list of the fonts. I'm not sure it categorises them, but you do get a bunch of details.

Update:

You only need an API key beyond usage limits, so depending on how many calls you make, you could just parse this JSON.

Further update:

Seems that you do now need an API key to make calls to the Developer API.

نصائح أخرى

I would build my list with minimum values I need and store it locally. You can store it as json string on your page.

For example open in browser: https://www.googleapis.com/webfonts/v1/webfonts?key=YOUR-API-KEY

var allGoogleFonsts = JSON.parse(JSON_RESULT);

var fontFamilyWithCategory = allGoogleFonsts.items.map(fontItem => {
    return {
        family: fontItem.family,
        category: fontItem.category,
    };
});

var groupBy = function(xs, key, value) {
  return xs.reduce(function(rv, x) {
    (rv[x[key]] = rv[x[key]] || []).push(x[value]);
    return rv;
  }, {});
};

var fontFamilyByCategory = groupBy(fontFamilyWithCategory, 'category', 'family');

Now you can store and use it without api calls:

console.log(JSON.stringify(fontFamilyByCategory));
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top