Hi,
I'm trying to save fields from an API (JSON format) to custom fields created using Toolset.
So far I can save everything in the WP fields like title and post content.
How can I save to toolset custom field?
Hello,
I assume you are going to save the JSON data into multiple fields.
here are my suggestions:
1) You can turn the JSON data into an PHP array:
hidden link
2) Then iterate over above PHP array:
hidden link
And save each item into different field:
https://codex.wordpress.org/Function_Reference/update_post_meta
Notice: If it is custom field created with Types plugin, Types plugin will pre-pend "wpcf-" before field slug, so in your PHP codes, you will need to use the meta_key like this: wpcf-my-field-slug
Hello,
Thank you for your suggestions. Come up with this:
if (!class_exists('RemoteCoin')) :
class RemoteCoin
{
public $raw;
public $CoinName;
public $Symbol;
public $Id;
public $Algorithm;
public function __construct($dirty_data)
{
$this->parse_data($dirty_data);
}
private function sanitize_remote_data($data_as_array)
{
foreach ($data_as_array as $inx => $data) {
$data_as_array [$inx] = sanitize_text_field($data);
}
return $data_as_array;
}
private function parse_data($dirty_data_as_array)
{
$clean_data = $this->sanitize_remote_data($dirty_data_as_array);
$this->raw = $clean_data;
$this->CoinName = $this->raw ['CoinName'];
$this->Symbol = $this->raw ['Symbol'];
$this->Id = $this->raw ['Id'];
$this->Algorithm = $this->raw ['Algorithm'];
}
}
endif;
if (!class_exists('RemoteCoins')) :
class RemoteCoins
{
public static $remote_data;
public static $coins;
private static function GetRemoteCoins($url)
{
$response = wp_remote_get($url);
if (is_wp_error($response) || !isset($response['body'])) return;
$body = wp_remote_retrieve_body($response);
if (is_wp_error($body)) return;
$data = json_decode($body, true);
if (!$data || empty($data)) return;
if (isset($data['page']) && isset($data['no_of_pages'])) {
$page = $data['page'];
$pages = $data['no_of_pages'];
$next_page_link = $data['next_page_link'];
if ($page !== $pages) {
}
}
return $data;
}
public static function LoadRemoteCoins($to_create_posts)
{
self::$remote_data = self::GetRemoteCoins('<em><u>hidden link</u></em>');
if (!isset(self::$remote_data ['Data'])) return;
$works = array();
foreach (self::$remote_data ['Data'] as $inx => $remote_item_data) {
$coin = new RemoteCoin($remote_item_data);
$works[] = $coin;
}
self::$coins = $works;
if ($to_create_posts) {
$count = 0;
foreach (self::$coins as $inx => $coin) {
$count++;
$post_id = self::CreateLocalCoinPost($coin);
if ($count >= 5) return self::$remote_data;
}
}
return self::$remote_data;
}
public static function CreateLocalCoinPost($coin)
{
if (!$coin) return -1;
$template = '';
$post_type = 'post';
$img_url = $coin->raw['ImageUrl'];
$Symbol = $coin->Symbol;
$Algorithm = $coin->Algorithm;
$content = "<p>${Symbol}</p><p>${Algorithm}</p>";
$new_post_id = self::CreateNewCoinPost($post_type, $coin->CoinName, $content, $template, $coin);
return $new_post_id;
}
public static function CreateNewCoinPost($post_type = 'post', $CoinName, $content, $template_rel_path = '', $data)
{
$post_id = -1;
if (!current_user_can('publish_posts')) {
return $post_id;
}
$author_id = get_current_user_id();
if (!$author_id) {
return $post_id;
}
$CoinName = sanitize_text_field(wp_strip_all_tags($CoinName));
$CoinName = esc_html(wp_unslash($CoinName));
$slug = sanitize_title_with_dashes($CoinName);
$post_type = post_type_exists($post_type) ? $post_type : 'post';
if (null == get_page_by_title($CoinName) && empty(get_posts(array('name' => $slug)))) {
$post_id = wp_insert_post(
array(
'post_name' => $slug,
'post_title' => $CoinName,
'post_content' => $content,
'post_type' => $post_type,
'post_author' => $author_id,
'comment_status' => 'closed',
'ping_status' => 'closed',
'post_status' => 'publish',
'wpcf-algorithm' => $Algorithm,
)
);
if ($post_id && $post_id > 0 && !empty($template_rel_path)) {
$template_full_path = trailingslashit(get_stylesheet_directory()) . $template_rel_path;
if (file_exists($template_full_path)) {
update_post_meta($post_id, '_wp_page_template', $template_rel_path);
}
}
} else {
$post_id = -2;
}
return $post_id;
}
}
endif;
Please see this: 'wpcf-algorithm' => $Algorithm
"algorithm" is the slug of the field so I attached wpcf, still the content is not saving but working when I put it on the wp content field.
Any other step that I should do?
Thanks!
I assume we are talking about this line of your PHP codes:
'wpcf-algorithm' => $Algorithm,
You can debug your codes manually:
1) Make sure $Algorithm outputs correct value
2) Then use WordPress built-in function update_post_meta() to save it into database, for example:
update_post_meta($post_id, 'wpcf-algorithm', $Algorithm);
See the document I mentioned above:
https://codex.wordpress.org/Function_Reference/update_post_meta
The function update_post_meta() updates the value of an existing meta key (custom field) for the specified post.
This may be used in place of add_post_meta() function. The first thing this function will do is make sure that $meta_key already exists on $post_id. If it does not, add_post_meta($post_id, $meta_key, $meta_value) is called instead and its result is returned.