update_callback not calling proper function when sending post - wordpress

So, here's my code so far:
add_action( 'rest_api_init', 'rest_api_user_meta_fields' );
function rest_api_user_meta_fields() {
// register_rest_field ( 'name-of-post-type', 'name-of-field-to-return', array-of-callbacks-and-schema() )
register_rest_field( 'user', 'grad', array(
'get_callback' => 'get_user_acf_fields',
'update_callback' => 'update_user_acf_fields',
'schema' => null,
)
);
}
function get_user_acf_fields( $object ) {
//get the id of the post object array
$grad = get_field('grad', 'user_32');
error_log($object);
return $grad;
}
function update_user_acf_fields( $value, $object, $field_name ) {
error_log($value);
error_log($field_name);
error_log($object);
}
Now, I expect to have get_user_acf_fields function to be executed when I send GET request to /users/ endpoint, and update_user_acf_fields to be run when I send POST request to said endpoint. In both cases it executes former, get_user_acf_fields, function. What am I doing wrong here?

Maybe you didn't include the proper nonce?
For developers making manual Ajax requests, the nonce will need to be
passed with each request. The API uses nonces with the action set to
wp_rest. These can then be passed to the API via the _wpnonce data
parameter (either POST data or in the query for GET requests), or via
the X-WP-Nonce header. If no nonce is provided the API will set the
current user to 0, turning the request into an unauthenticated
request, even if you’re logged into WordPress.
— Read more on https://developer.wordpress.org/rest-api/using-the-rest-api/authentication/
Here's your code that I modified and tested working on WordPress 4.9.5 with Advanced Custom Fields 4.4.12.
add_action( 'rest_api_init', 'rest_api_user_meta_fields' );
function rest_api_user_meta_fields() {
// register_rest_field ( 'name-of-post-type', 'name-of-field-to-return', array-of-callbacks-and-schema() )
register_rest_field( 'user', 'grad', array(
'get_callback' => 'get_user_acf_fields',
'update_callback' => 'update_user_acf_fields',
'schema' => [
'description' => 'User grad blah',
'type' => 'string',
],
)
);
}
// $user_arr is an `array` of the user data; e.g. `id`, `username`, and `name`.
// $field_name is the name of the ACF custom field; i.e. in this case, it's `grad`.
function get_user_acf_fields( $user_arr, $field_name ) {
// Get the value of the 'grad' custom field.
$grad = get_field($field_name, 'user_' . $user_arr['id']);
error_log(var_export( $user_arr, true ));
error_log($field_name);
//return $grad;
return $field_name . ';' . $grad . ';' . $user_arr['id'];
}
// $field_value is the value of the ACF custom field; i.e. in this case, it's `grad`.
// $user_obj is an `object` of the user data; e.g. `id`, `username`, and `name`.
function update_user_acf_fields( $field_value, $user_obj, $field_name ) {
// Get the value of the 'grad' custom field.
$grad = get_field($field_name, 'user_' . $user_obj->ID);
if ( $grad !== $field_value ) {
update_field( $field_name, $field_value, 'user_' . $user_obj->ID );
}
error_log($field_value);
error_log($field_name);
error_log(var_export( $user_obj, true ));
return true;
}
And here's the HTML and JS/AJAX I used for testing the PHP code above.

$_GET, $_POST and UPDATE are different things to the rest API. Since update isn't necessarily a construct of the language it just means that when a request usually a $_POST comes in with a specific "Update" header that it should call your update function.

Related

I can't access wp url parameter

I'm trying to add an url parameter to a custom wp rest api endpoint, but when I access the endpoint using this url: mysite.com/wp-json/booking/pris?name=carl, I get this error:
SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data
Here is my code:
add_action( 'rest_api_init', 'my_register_route_for_price' );
function my_register_route_for_price() {
global $wp;
$wp->add_query_var( 'name' );
register_rest_route( 'booking', 'pris', array(
'methods' => 'GET',
'callback' => 'get_price_callback',
)
);
}
function get_price_callback() {
$param = $request->get_url_params( 'name' );
return rest_ensure_response($param);
}
That's a WordPress security feature.
Undefined WordPress query variable outside the scope of the WP_Query class need to be defined first.
WP::add_query_var( string $qv )
Adds a query variable to the list of public query variables.
Use Case Scenario
<?php
add_action( 'init','wp67408336' );
function wp67408336() {
global $wp;
$wp->add_query_var( 'name' );
};
In any case
<?php
$name = htmlspecialchars( $_GET["name"] );

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.

How to submit contact form 7 programmatically

I want to submit contact form by custom function
The code below is getting the instance of form but when submitted. It submit the form but not the fields which I wanted.
$item = wpcf7_contact_form( $formId );
$result = $item->submit();
Here where I can pass the fields I define in admin panel like "textarea-123" & "email-234" ?
I did not get exact answer for what I look but I found the alternate solution.
function cf7Submit($formId , $args) {
$url = 'http://example.com/wp-json/contact-form-7/v1/contact-forms/'.$formId.'/feedback';
$response = wp_remote_post( $url, array(
'method' => 'POST',
'body' => $args
)
);
if ( is_wp_error( $response ) ) {
$error_message = $response->get_error_message();
echo "Something went wrong: $error_message";
} else {
echo 'Response:<pre>';
print_r( $response );
echo '</pre>';
}
}
I can call this function like this:
cf7Submit(128, array(
'textarea-123' => 'test email',
'email-234' => 'asd#asd.com'));
#daraptoor has found a good solution, but as #davevsdave noticed in the comment, it does not work properly in CF7 5.6.
Error 415 is caused by added to API check for content type passed into a request header:
// part of create_feedback() from CF7's rest-api.php
if ( ! str_starts_with( $content_type, 'multipart/form-data' ) ) {
To figure it out, just add the expected content type into a request header:
$response = wp_remote_post( $url, array(
'method' => 'POST',
'headers' => array(
'Content-Type' => 'multipart/form-data'
),
'body' => $args
)
);
UPD
Faced with an issue, that wp_remote_post() send data in body and not in POST, so CF7 API does not get any fields. It is caused because the WP's function uses http_build_query() (read more here).
I have used cURL request as a workaround:
// Same user agent as in regular wp_remote_post().
$userAgent = 'WordPress/' . get_bloginfo('version') . '; ' . get_bloginfo('url');
// Note that Content-Type wrote in a bit different way.
$header = ['Content-Type: multipart/form-data'];
// Same array with fields to pass, not changed.
$body = ['foo' => 'bar'];
$curlOpts = [
// Send as POST
CURLOPT_POST => 1,
// Get a response data instead of true
CURLOPT_RETURNTRANSFER => 1,
// CF7 will reject your request as spam without it.
CURLOPT_USERAGENT => $userAgent,
CURLOPT_HTTPHEADER => $header,
CURLOPT_POSTFIELDS => $body,
];
$ch = curl_init($apiUrl); // Create a new cURL resource.
curl_setopt_array($ch, $curlOpts); // Set options.
$response = curl_exec($ch); // Grab response.
if (!$response) {
// Do something if an error occurred.
} else {
$response = json_decode($response);
// Do something with the response data.
}
// Close cURL resource, and free up system resources.
curl_close($ch);
Hope it saves someones time :)
You can add a piece of JS code, like:
$("form.wpcf7").submit()

Wordpress REST response shows taxonomy categories only with ID's

I'm using WP as a headless CMS with ACF en ACF-2-REST plugins. I've added categories to a post-type and when I make a GET call, it shows me all the information of a particular post including the categories, but only the ID's. If I want to match it, I have to do another call to Categories to get the information of that categories (name, parent etc).
How can I show that information instead of just the ID in a post call?
How the JSON looks now at the /activities call:
{
"id":111,
"date":"2020-01-18T15:39:27",
"date_gmt":"2020-01-18T15:39:27",
"guid":{"rendered":"https:\/\/url.be\/?post_type=activities&p=111"},
"modified":"2020-01-18T15:39:27",
"modified_gmt":"2020-01-18T15:39:27",
"slug":"walking-on-wood",
"status":"publish",
"type":"activities",
"link":"https:\/\/url.be\/activities\/walking-on-wood\/",
"title":{"rendered":"Walking on wood"},
"template":"",
"categories":[14,25,13,2,18,21,6,24],
"acf":{...}
}
What I want to show in the "categories" instead of just the numbers (from the categories call)
{
"id":3,
"count":1,
"description":"",
"link":"https:\/\/url.be\/category\/duration\/lower-than-30-min\/",
"name":"< 30 min.",
"slug":"lower-than-30-min",
"taxonomy":"category",
"parent":2,"meta":[],
"acf":[],
"_links":{"self":[{"href":"https:\/\/url.be\/wp-json\/wp\/v2\/categories\/3"}],
"collection":[{"href":"https:\/\/url.be\/wp-json\/wp\/v2\/categories"}],
"about":[{"href":"https:\/\/url.be\/wp-json\/wp\/v2\/taxonomies\/category"}],
"up":[{"embeddable":true,"href":"https:\/\/url.be\/wp-json\/wp\/v2\/categories\/2"}],
"wp:post_type":[{"href":"https:\/\/url.be\/wp-json\/wp\/v2\/posts?categories=3"},{"href":"https:\/\/url.be\/wp-json\/wp\/v2\/activities?categories=3"}],
"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}
}
Can't find any solution on the internet how I could manipulate the structure of that JSON with a custom function, would appreciate it a lot if someone point me to the right direction. Thanks!
As discussed in the comments section, a solution to this question is to use a custom endpoint method for the WP REST API and perform extra queries in there to get the data you need. This way, you can do all the data manipulation to get the perfect response, resulting in one REST call.
As taken from the official developer docs
Define an endpoint method and add some extra data
<?php
add_action( 'rest_api_init', function () {
register_rest_route( 'myplugin/v1', '/author/(?P<id>\d+)', array(
'methods' => 'GET',
'callback' => 'my_awesome_func', //note that this is the method it will fire
'args' => array(
'id' => array(
'validate_callback' => function($param, $request, $key) {
return is_numeric( $param );
}
),
),
) );
/**
* Grab latest post by an author along with its category!
*
* #param array $data Options for the function.
* #return array Post,
*/
function my_awesome_func( $data ) {
$posts = get_posts( array(
'author' => $data['id'],
) );
if(!empty($posts)) {
//Example of appending extra data
foreach($posts as $post) {
$category = wp_get_post_terms($post->ID, 'category');
$post['category'] = $category;
}
return $posts;
} else {
return new WP_Error( 'no_author', 'Invalid author', array( 'status' => 404 ) );
}
}

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 );
}

Resources