carbon-fields icon indicating copy to clipboard operation
carbon-fields copied to clipboard

How to fetch carbon fields from wp-json Rest API

Open AlexanderBorysenko opened this issue 4 years ago • 1 comments

How i can fetch for example theme_option from wp-json.

There is an wp-json/carbon-fields/v1 but it is useless =(

AlexanderBorysenko avatar Jul 15 '21 15:07 AlexanderBorysenko

I've created simple solution for this problem

function get_theme_options()
{
	global $wpdb;
	$theme_options_db = $wpdb->get_results("SELECT * FROM 'wp_options'WHERE 'option_name' LIKE '%_crb%'");
	$theme_options = array();
	foreach ($theme_options_db as $value) {
		$theme_options[$value->option_name] = $value->option_value;
	}
	return $theme_options;
}

add_action('rest_api_init', function () {
	register_rest_route('custom-fields/', 'theme_options', [
		'methods' => 'GET',
		'callback' => 'get_theme_options',
	]);
});

I've just registered my own Rest-Api route so on /wp-json/custom-fields/theme_options i can get all theme options

AlexanderBorysenko avatar Jul 15 '21 18:07 AlexanderBorysenko

Hi @AlexanderBorysenko ,

Currently, this is not safe because everyone can access it, and if there is some sensitive information (API Key, Username, or Password) it might be leaked.

In Carbon Fields, you could find the /wp-json/carbon-fields/v1/options/ (GET|POST) endpoint that would allow you to retrieve the data, but please note that this endpoint requires manage_options to be accessed.

You could find the other REST Endpoints in core/REST_API/Router.php.

HTMLBurger-NG avatar Feb 27 '23 14:02 HTMLBurger-NG