pods-framework / pods

The Pods Framework is a Content Development Framework for WordPress - It lets you create and extend content types that can be used for any project. Add fields of various types we've built in, or add your own with custom inputs, you have total control.
https://pods.io/
GNU General Public License v2.0
1.07k stars 264 forks source link

POST method not working on Pods fields with REST API #3412

Open cmjn14 opened 8 years ago

cmjn14 commented 8 years ago

Hi,

I have followed this tutorial: http://pods.io/docs/build/extending-core-wordpress-rest-api-routes-with-pods/

But I cannot update my Pods fields with Ajax POST method:

Here is my ajax request:

$.ajax({
    url     :   myUrl,
    method  :   'POST',
    data        :   {myField: "my new value"} ,
    dataType    :   'json',
    beforeSend  :   function(req) {
                        req.setRequestHeader('Authorization', 'Bearer ' + myToken));
                    }
});

Many thanks for any help you may provide :-)

cmjn14 commented 8 years ago

PS : I've also tested with Postman and the behaviour is the same.

quasel commented 8 years ago

Very Strange to be honest i didn't use the new version yet - maybe @Shelob9 can help Can you share an example pods configuration and the corresponding data submitted?

Pods Admin -> Migrate Packages -> export And maybe please add Pods Admin -> Settings -> Debug Information

cmjn14 commented 8 years ago

Here is my current config:

podconfig.txt

cmjn14 commented 8 years ago

And the debug info:

Debug Information

WordPress Version: 4.4.1

PHP Version: 5.4.12

MySQL Version: 5.6.12

Server Software: Apache/2.4.4 (Win64) PHP/5.4.12

Your User Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.109 Safari/537.36

Session Save Path: c:/wamp/tmp

Session Save Path Exists: Yes

Session Save Path Writeable: Yes

Session Max Lifetime: 1440

Opcode Cache:

Apc: No
Memcached: No
Redis: No
Object Cache:

Apc: No
Apcu: No
Memcache: No
Memcached: No
Redis: No
WPDB Prefix: wp_

WP Multisite Mode: No

WP Memory Limit: 40M

Pods Network-Wide Activated: No

Pods Install Location: C:\wamp\www\mysite\wp-content\plugins\pods/

Pods Tableless Mode Activated: No

Pods Light Mode Activated: No

Currently Active Theme: Twenty Sixteen

Currently Active Plugins:

JWT Authentication for WP-API: 1.0.0
Pods - Custom Content Types and Fields: 2.6.1
WordPress Importer: 0.6.1
WP REST API: 2.0-beta12
saulopaiva commented 8 years ago

Hi,

Adding to discution, i have been taking the same problem. Using Pods 2.6.1 and WP REST API 2.0 beta 12.

I found that:

In PodsRESTHandlers::write_handler() the variables $pod_name and $id were not being set properly, and were empty. The pods_v() function seems to not work properly.

Another problem found was that the self::get_pod( $pod_name, $id ); even if the parameters was hardcoded, did not let the field be saved. Changing to pods( $pod_name, $id, true ); works well.

I've made some other changes in this function, that solves the problem. As I not deeply know the plugin architecture, it's important a better review of my code before a pull request. Especialy if the bug is in the function pods_v().

public function write_handler( $value, $object, $field_name, $request, $object_type ) {

    $pod_name = $object_type;

    $id = $object->ID;

    $pod = pods( $pod_name, $id, true );

    if ( $pod && PodsRESTFields::field_allowed_to_extend( $field_name, $pod, 'write' ) ) {
        $pod->save( $field_name, $value, $id );

        return $pod->field( $field_name );
    }

    return false;

}
quasel commented 8 years ago

Related to #3362 - same solution could apply here ...

huangc28 commented 8 years ago

is there an further fixation on this issue? i've tried @okrg's method all the way through, but pods custom field is still not changing. I have enabled all rest read/write api support on both the field and field type, but only WP's original field is being updated, all other fields are left unchanged @@. please help!

mdkass commented 8 years ago

Has any one find a solution for this issue, I can't write pods custom fields too using the REST api.

ghost commented 8 years ago

Any update on this? Still persistent with the latest WP release of Pods and WP-Rest v2

nickbreen commented 7 years ago

Still occurring with:

Debug Information

WordPress Version: 4.6.1

PHP Version: 7.0.8-0ubuntu0.16.04.3

MySQL Version: 5.6.34

Server Software: Apache/2.4.18 (Ubuntu)

Your User Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/53.0.2785.143 Chrome/53.0.2785.143 Safari/537.36

Session Save Path: /var/lib/php/sessions

Session Save Path Exists: Yes

Session Save Path Writeable: Yes

Session Max Lifetime: 1440

Opcode Cache:

Apc: No
Memcached: No
Redis: No
Object Cache:

Apc: No
Apcu: No
Memcache: No
Memcached: No
Redis: No
WPDB Prefix: wp_8_

WP Multisite Mode: Yes

WP Memory Limit: 128M

Pods Network-Wide Activated: No

Pods Install Location: /var/www/wp-content/plugins/pods/

Pods Tableless Mode Activated: No

Pods Light Mode Activated: No

Currently Active Theme: Salient

Currently Active Plugins:

Advanced Custom Fields PRO: 5.4.7
Amazon Web Services: 1.0
Contact Form 7: 4.5.1
Google XML Sitemaps: 4.0.8
Go – Responsive Portfolio for WP: 1.6.2
Gravity Forms: 2.0.7
Gravity Forms DPS PxPay: 1.8.0
Jetpack by WordPress.com: 4.4
Loushou: ACF for WooCommerce: 1.1.0
Multisite Enhancements: 1.3.7
Multisite Plugin Manager: 3.1.5
Payment Express Gateway for WooCommerce: 1.3.1
Pods - Custom Content Types and Fields: 2.6.7
Really Simple CAPTCHA: 1.9
REST API Console: 2.0.1
TinyMCE Advanced: 4.4.1
User Role Editor: 4.29
WooCommerce: 2.6.8
WooCommerce - Store Exporter: 1.8.6
WooCommerce Bookings: 1.9.12
WooCommerce Bookings Weekly Calendar: 0.2.0
WooCommerce Email ACF Location: 1.0
WooCommerce Helper: 1.7.2
Woo Preview Emails: 1.2.3
WP Migrate DB Pro: 1.7
WP Migrate DB Pro CLI: 1.3
WP Migrate DB Pro Media Files: 1.4.6
WP Migrate DB Pro Multisite Tools: 1.1.4
WP Offload S3: 1.2.2
WP Offload S3 - Assets Addon: 1.2.1
WP Offload S3 - Enable Media Replace Addon: 1.0.2
WP Offload S3 - WooCommerce Addon: 1.0.5
WP REST API: 2.0-beta15
WP REST API Controller: 1.2.1
WP SES: 0.4.8
nickbreen commented 7 years ago

Gaa, I am using jQuery.ajax and neglected to JSON.stringify({...}) the data first.

This does work:

$.ajax("https://example.com/wp-json/wp/v2/my-pod/1819", {
    method: 'PATCH',
    contentType: 'application/json; charset=UTF-8',
    processData: false,
    data: JSON.stringify({
        my-field: [1, 2, 3]
    }),
})

This does not as it form-encodes the data:

$.ajax("https://example.com/wp-json/wp/v2/my-pod/1819", {
    method: 'PATCH',
    contentType: 'application/json; charset=UTF-8',
    processData: false,
    data: {
        my-field: [1, 2, 3]
    },
})