Wordpress REST API for favicon and logo images - wordpress

Can someone help me how to get to the endpoint for logo and favicon images in Wordpress rest api? When I use http:// .... / wp-json / endpoint, I'm getting the title or name and description with no links to images?

For logo you need add this function to functions.php:
// wordpress api for logo extension
add_action( 'rest_api_init', 'add_logo_to_JSON' );
function add_logo_to_JSON() {
register_rest_field( 'post', 'page_logo_src', array( // post for where to register - page_logo_src is the name for api
'get_callback' => 'get_logo_src',
'update_callback' => null,
'schema' => null,
)
);
}
function get_logo_src( $object, $field_name, $request ) {
$size = 'full';
$custom_logo_id = get_theme_mod( 'custom_logo' );
$feat_img_array = wp_get_attachment_image_src($custom_logo_id, $size, true);
return $feat_img_array[0];
}
Then logo is available here: http://your-domain/wp-json/wp/v2/posts under page_logo_src.
You can use this function to grab any of wordpress data you need, for favicon use get_site_icon_url.

Related

Woocommerce Rest APi get custom meta field

I'm trying to pull a products custom meta and custom taxonomy value from woocommerce using the rest api. But no matter what I try a can't seem to get it to work. Not sure what I'm missing.
Example, my products have a custom meta value "product_location". The post meta value exists and works because I can use this to pull the value into the woocommerce product page:
get_post_meta($product->get_id(), 'product_location', true );
In my functions.php file I've added:
add_action( 'rest_api_init', 'handle_location' );
function handle_location() {
register_rest_field( 'post', 'product_location', array(
'get_callback' => array( $this, 'get_the_product_location' ),
'schema' => null
));
}
function get_the_product_location( $post, $field_name, $request ) {
return get_post_meta( $post[ 'id' ], $field_name, true );
}
However, when I make a call say https://*******.com/wc-api/v2/products/302341/ the meta data isn't included. So basically how can I include the additional meta data? I plan on trying this for a custom taxonomy as well. Any help is appreciated - Thanks
Try changing your bottom function to
function get_the_product_location( $post, $field_name, $request ) {
return get_post_meta( $post[ 'id' ], 'product_location', $meta_value );
}

Post a WordPress post on post request

How can I post a post on WordPress through a post request without using the UI?
If possible I would also like to have guidance on how I can implement this with ACF fields.
You could create a child theme or a plugin to write your custom functionality.
You can handle AJAX requests with a specific action, and call wp_insert_post() to create posts.
Example to get you started:
add_action( 'wp_ajax_create_post', 'create_post_ajax_handler' );
/**
* Handle the create post ajax request
*/
function create_post_ajax_handler() {
// Get the post title from the ajax request
// You can get whatever you have passed here
// Also, perform any validations you might want
$post_title = $_POST['post_title'];
// Create the post
$post_id = wp_insert_post( array(
'post_title' => $post_title,
'post_status' => 'publish'
// you could also specify the 'post_type', 'meta_input' etc
), true );
// Error handling
if ( is_wp_error( $post_id ) ) {
// Send error response
wp_send_json_error( $post_id->get_error_message() );
}
// Send success response
wp_send_json_success( $post_id );
}
add_action( 'wp_enqueue_scripts', 'enqueue_ajax_script' );
/**
* Enqueue the ajax script
*/
function enqueue_ajax_script() {
// Enqueue your JavaScript file with 'jquery' as a dependency
wp_enqueue_script(
'ajax-script',
plugin_dir_url( __FILE__ ) . 'ajax-script.js',
array( 'jquery' )
);
// Expose the url to admin-ajax.php as `ajax_object.ajaxurl`
wp_localize_script(
'ajax-script',
'ajax_object',
array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) )
);
}
// Set the url as `ajax_object.ajaxurl` which is the url to admin-ajax.php
$.ajax(ajax_object.ajaxurl, {
method: 'POST',
data: {
// Your action should match the name of your 'wp_ajax_{action}' hook
action: 'create_post',
// Pass any data you want
post_title: 'Example post title'
}
})
.done((response) => {
// Do whatever you want with the response (in this example, this would be the post id)
console.log(response);
})
.fail((error) => {
// Handle the errors
console.error(error);
});
You might also want to look into Nonces, which help protect against Cross-Site Request Forgery (CSRF).
Disclaimer: I haven't tested this code, but I hope you get the idea.
Edit: Since you mentioned that you use Advanced Custom Fields:
$post_id = wp_insert_post( array(
'post_title' => $post_title,
'post_status' => 'publish',
// You can set ACF fields in the 'meta_input' array
'meta_input' => array(
'acf_custom_field_name' => 'an example value'
)
), true );
Edit #2: Please read more about AJAX in WordPress on the Codex.
Replying to your comment:
to which URL should I make the ajax request in order for it to trigger?
On the Codex, under AJAX in Plugins > Ajax on the Viewer-Facing Side
You might also use wp_localize_script() to make the URL available to your script, and generate it using this expression: admin_url( 'admin-ajax.php' )
how do I insert the action "wp_ajax_create_post" in my post request?
On the Codex, AJAX in Plugins > Ajax on the Administration Side
Notice how the 'action' key's value 'my_action', defined in our JavaScript above, matches the latter half of the action 'wp_ajax_my_action' in our AJAX handler below.

Add a missing response from Learndash API

After playing around with the Learndash API, I realised it was not possible to get the user progression on the courses he is enrolled to.
I contacted the Learndash support that confirmed it was not possible.
I am new to wordpress dev (I am more familiar with app development), and I would like to know how to implement this feature by myself ? Do I have, for example, to create a custom endpoint ?
I would like to know how to add some 'responses' missing for a plugin API.
What I understand, is that everything from Learndash (courses, topics etc) are like custom posts, so I am guessing it is like adding custom endpoints for posts.
Thanks
I have done this for the course list API. I wanted to remove the "_links" parameter from the response.
I have worked with the object-oriented concept and here is my code:
/* Filter sfwd-courses object */
add_filter( 'rest_prepare_sfwd-courses', array( $this, 'prepare_ld_courses' ), 999, 3 );
public function prepare_ld_courses( $response, $object, $request ) {
/* Remove all links from response */
foreach ( $response->get_links() as $k_l => $v_k ) {
$response->remove_link( $k_l );
}
$data = $response->get_data();
$base_course_id = $data['id'];
$content = array( 'ID' => $course_id, 'parent_id' => (int) $base_course_id, 'title' => $course_obj->post_title, 'description' => $course_obj->post_content, 'cover_image' => null, 'thumbnail' => null);
/* Set thumbnail and cover_image */
$content = EI_Rest_Course::setup_images( $content, $base_course_id );
$response->set_data( $content );
return $response;
}
public static function setup_images( $content, $course_id ) {
/* logic to get course image - $image */
$content['thumbnail'] = $image;
/* logic to get course cover_image - $cover_image */
$content['cover_image'] = $cover_image;
return $content;
}

Wordpress API V2 custom fields with custom post types

I'm trying to add my custom posts to the Wordpress API, which also have custom fields.
I can view the custom posts types on the API via, /wp-json/wp/v2/fruit.
But the custom fields aren't displaying, how do you add them to the API?
Take a look at "Extending the REST API / Modifying Responses", and in particular at register_rest_field.
Check out this example:
add_action( 'rest_api_init', 'create_api_posts_meta_field' );
function create_api_posts_meta_field() {
// register_rest_field ( 'name-of-post-type', 'name-of-field-to-return', array-of-callbacks-and-schema() )
register_rest_field( 'post', 'post-meta-fields', array(
'get_callback' => 'get_post_meta_for_api',
'schema' => null,
)
);
}
function get_post_meta_for_api( $object ) {
//get the id of the post object array
$post_id = $object['id'];
//return the post meta
return get_post_meta( $post_id );
}

How to Get visual composer page through rest api

I'm developing an ionic app in which I have a rest api to get the blog contents from wordpress page. I'm trying to get the visual composer page through api but I'm getting raw html format and css or missing for visual component pages. Can I get the visual composer page fully along the properties through api or is there any restriction to use api ??
I think you find your answer here: https://github.com/WP-API/WP-API/issues/2578
The example below was taken from the link above (thank you, bradmsmith!)
Here is an example how to render the VC shortcodes on the content of a post:
add_action( 'rest_api_init', function ()
{
register_rest_field(
'page',
'content',
array(
'get_callback' => 'compasshb_do_shortcodes',
'update_callback' => null,
'schema' => null,
)
);
});
function compasshb_do_shortcodes( $object, $field_name, $request )
{
WPBMap::addAllMappedShortcodes(); // This does all the work
global $post;
$post = get_post ($object['id']);
$output['rendered'] = apply_filters( 'the_content', $post->post_content );
return $output;
}

Resources