Friday, March 22, 2013

Getting the names of custom fields from Wordpress JSON API


This is the necessary Wordpress plugin modification so that the list of custom fields can be pulled from Wordpress via the JSON API.

First you need to add the JSON API plugin to your Wordpress site.
Then go to Plugins -> JSON API -> Edit
Open the json-api/controllers/core.php file and add the following code as the last functions in the class.

function get_acf_fields()
{
  global $json_api;
  extract($json_api->query->get(array('id', 'slug', 'page_id', 'page_slug', 'children')));

  // vars
  $return = array();
  $keys = get_post_custom_keys($id);

  if($keys)
  {
    foreach($keys as $key)
    {
      if(strpos($key, 'field_') !== false)
      {
        $field = $this->get_acf_field($key, $id);
        $return[$field['order_no']] = $field;
      }
    }
   
    ksort($return);
  }

  // return fields
  return $return; 
}

function get_acf_field($field_name, $post_id = false)
{
  $post_id = $post_id ? $post_id : $this->get_post_meta_post_id($field_name);
  $field = get_post_meta($post_id, $field_name, true);
  return $field; 
}

16 comments:

  1. Thanks, it works. I set up the plugin,
    it only remains to add Google Maps https://elfsight.com/google-maps-widget/, so geolocation is displayed for site visitors, and I think this is pretty well website for the online store will be ready soon. I chose not the most difficult of the Wordpress themes, but the site turned out to be pretty nice thanks to the design, and optimized by carefully selected content, in general, I coped with such task and the customer is satisfied my work actually.

    ReplyDelete
  2. Many people with a WordPress website wonder what to do when they see the notice in the WordPress admin prompting them to upgrade to the latest version.
    https://edkentmedia.com/website-design-toronto

    ReplyDelete
  3. Majority each company has their own after sales support center. Customer service is the backbone of any online or offline enterprise. The majority of the companies consider their contracted and temporary workers, their customers as their internal team members, and most.
    https://servicecenterguru.com

    ReplyDelete
  4. If you don't know what a content management system is (CMS), it is the ability to manage your own content without the need of editing files. Quite simply, it's the best thing to ever happen to the way we manage our content.etikečių spausdinimas

    ReplyDelete
  5. Took me time to read all the comments, but I really enjoyed the article. It proved to be Very helpful to me and I am sure to all the commenters here! It’s always nice when you can not only be informed, but also entertained! wordpress virus remova

    ReplyDelete
  6. If you are looking for more information about flat rate locksmith Las Vegas check that right away. Ozzun Website design and development company

    ReplyDelete
  7. Thanks for another wonderful post. Where else could anybody get that type of info in such an ideal way of writing? extension opencart

    ReplyDelete
  8. Majority each company has their own after sales support center. Customer service is the backbone of any online or offline enterprise. The majority of the companies consider their contracted and temporary workers, their customers as their internal team members, and most. Wordpress webshop laten maken

    ReplyDelete
  9. Sites we Like…… [...] Every once in a while we choose blogs that we read. Listed below are the latest sites that we choose [...]…… web design new york

    ReplyDelete
  10. Thank you for the auspicious writeup. It in truth used to be a amusement account it. Glance complex to more added agreeable from you! However, how could we be in contact? website design company nyc

    ReplyDelete
  11. The writer, through this blog, has earned respect from many for all the right reasonsSpanish

    ReplyDelete
  12. This is my first time i visit here. I found so many interesting stuff in your blog especially its discussion. From the tons of comments on your articles, I guess I am not the only one having all the enjoyment here keep up the good work this website

    ReplyDelete
  13. hello!! Very interesting discussion glad that I came across such informative post. Keep up the good work friend. Glad to be part of your net community. custom writing service

    ReplyDelete
  14. This blog is really great. The information here will surely be of some help to me. Thanks!. schlüsseldienst

    ReplyDelete