Update User Meta with spaces using REST API - WordPress - wordpress

I'm trying to update the user meta with spaces registered on my website. I am using register_meta hook to register user meta. And for updating users using WordPress REST API I use wp_remote_post but the problem is when I tried to update a user meta with spaces it will add as array in response then it will add as new user meta.
For example the value of user meta is - community test
Sample Response:
[meta] => Array
(
[agent_community] => Array
(
[0] => community
[1] => test
)
)
Sample Code for registering user meta.
register_meta('user', 'agent_community', array(
"type" => "string",
"show_in_rest" => true
));
Update using wp_remote_post
$update_data = array('email'=> 'test#mail.com', 'meta[agent_community]' => 'community test');
$args = array(
'headers' => array(
'Authorization' => 'Basic ' . base64_encode( 'username:password' ),
),
'body' => array_filter($update_data));
$update_url = 'https://test-url.com/wp-json/wp/v2/users/17';
$response = wp_remote_post( $update_url, $args);
Database:
Is there a way that can merge the array on user meta?

implode() might help you. And you can change spaces with HTML entities. Or use function like urledncode() / urlendecode() or something like this.

Related

Resolved: Wordpress API Post request not updating

I'm tring to write a WordPress post request (API) and although it reports a success message, the data is not updated. I temporarily created two extra columns in the WordPress user table (yes, not best practice). But the data is not saved. The jwt-auth plugin is used.
Current rest route:
register_rest_route( 'jwt-auth/v1', 'base_coords_post', array(
'methods' => 'POST',
'callback' => 'base_coords_post',
));
And the callback
function base_coords_post() {
global $wpdb;
$userId = get_current_user_id();
$data = array(
'lat' => $_POST['lat'],
'lng' => $_POST['lng']
);
$data_where = array(
'ID' => $userId
);
$wpdb->update($wpdb->users , $data, $data_where);
}
How is it that the columns are not being updated? I checked the post values, they exist, the userID also exists.
EDIT
The fix was i tried to add varchar longer than the database field can hold. Changing the DB fixed the issue.

POST method in WordPress REST API

I want to use POST methon from REST API tu update a user profile from a backend website to FrontEnd WordPress. The next code is suppose to update the name, description and the slug.
if (isset($_POST['update_frontend'])) {
$url = $setting['front_end_host_wp'].'users/'.$item_id.'/';
$data1 = array(
'description' => $data['content'],
'name' => $data['nume'],
'slug' => $data['slug'],
'parent' => 0,
'meta' => array()
);
$result = $df->runGuzzle('POST', $url, $data1);
printpre($result);
}
I want to update the user_status(Approved, Pending or Denied) and other info like, name, email etc. But the code above is not working.

Custom Post Type & Author not associating, user post count is 0, api doesn't return author in post objects

When Users create custom post types, the author field does not seem to be passed back to WP posts functions.
As the title says, I'm creating a user account programatically, and having that user log-in and write a post of custom post_type = 'job'.
This all LOOKS good in the database - the user, and post are inserted. The post_author is indeed the same as the ID of the user who created it.
But, in the User Accounts panel, that user post count is 0, and the API data object omits the author. I've configured the API for the additional custom post type and the endpoint works to return all post data EXCEPT author.
I've tried creating a post from the CMS with these users as well, and likewise, even if I give them Administrator permissions, they have a 0 post count - the posts are attributed to their author ID. I've also tried to force and update using wp_update_post();
Here's the code to create the user, and then the post:
// Generate the password and create the user
$password = wp_generate_password( 12, false );
$user_id = wp_create_user( $email_address, $password, $email_address );
//login
wp_clear_auth_cookie();
wp_set_current_user ( $user_id );
wp_set_auth_cookie ( $user_id );
// Set the role
$user = new WP_User( $user_id );
$user->set_role( 'subscriber' );
//Ready data for linked post type creation
$new_post = array(
'post_content' => $email_address,
'post_status' => 'publish',
'post_date' => date('Y-m-d H:i:s'),
'post_author' => $user_id,
'post_title' => $post_name,
'post_name' => $post_name,
'post_type' => $user_type
);
//Create the post
$account_link_post_id = wp_insert_post($new_post);
There was an associated post to this one that shed light on this answer. When registering the post type, I was not setting enough fields in the 'supports' array of the register_post_type. Adding author, whatdayaknow, gives me the datapoint I'm looking for.
register_post_type(self::POST_TYPE,
array(
'labels' => $labels,
'public' => true,
'has_archive' => true,
'description' => __(""),
'supports' => array(
'title', 'author'
),
)
)
NOTE: Post count in the User Accounts page is still 0 - but, the API is returning the data I want/need.

WP-REST API excluding category doesn't work

I used this semi-official plugin to enable API call.
But I can't find a way to exclude category. For example if I have mysite.com/wp-json/wp/v2/posts?filter[category__not_in]=10, it will ignores the filter and show all latest posts.
Is there a way to exclude category from the API call?
With the Rest API some filters are available only for logged in users:
In addition, the following are available when authenticated as a user
with edit_posts:
[...]
category__not_in
This is taken from the Rest API 1.0 documentation.
What that mean is you need to logged in as an editor (user with edit_posts capability). For doing this in a clean way, I suggest create a minimal role for REST user with only the capabilities you need, something like this:
add_role(
'rest_user',
__( 'REST User' ),
array(
'read' => true,
'edit_posts' => true,
'delete_posts' => false
)
);
Then do your query as authenticated user with the following:
$response = wp_remote_request('http://example.com/wp-json/wp/v2/posts?filter[category__not_in]=10', array(
'method' => 'GET',
'headers' => array(
'Authorization' => 'Basic ' . base64_encode('login:password')
)
));
Thanks to this article you shared, it seems you need to declare the filter name as query var, so in this case:
add_filter('query_vars', function($query_vars) {
$query_vars[] = 'category__not_in';
return $query_vars;
});
Which make sense because Wordpress is droping any GET parameter that isn't declare, though I don't understand why all the filters are not declared as query vars inside the WP Core...
For WP API V2 use a minus sign, for example:
http://yourwpwebsite/wp-json/wp/v2/posts/&filter[cat]=1,2,-3
will retrieve all posts in categories with id= 1 and id = 2 but excluding any posts in category with id= 3

How to add a custom event item in Buddypress Activity Stream?

Does anybody know the basic steps on how to add a custom event item to the Buddypress activity stream? I've got a custom post type of "activity" that my members can click on in the post and it records in a database table that that user has "completed" that activity. More or less like a "to do" list. I'm trying to figure out how I can integrate this with the buddypress activity stream so when a user clicks the button, it inserts something like, "John Doe has completed Permalinked Post Title Here." I've looked through the Buddypress codex & have Googled quite a bit, but I can't seem to find any good tutorials on this. If anybody can point me in the right direction or give me a simple plain English step by step I would appreciate it. Please know that I'm not looking for any code written, just a general idea of the steps involved in making this happen.
For anyone who is searching, this is what I wrote in my php file that was called in the ajax request for the button that "completes" the activity...
// Begin Code for Custom Activity Stream
global $wp, $wp_query;
$this_activity_object = get_post( $activity_post_id_complete );
$title = $this_activity_object->post_title;
$activity_content = $title;
$this_user_profile_url = bp_core_get_user_domain($wp_user_id);
$action_String = "#" . bp_core_get_username($wp_user_id)." just completed a custom activity!";
global $bp;
bp_activity_add( array(
'user_id' => $wp_user_id,
'item_id' => $activity_db_id_complete,
'action' => $action_String,
'content' => $activity_content,
'component' => 'activity',
'primary_link' => '' . $title . '',
'type' => 'custom_activity_update',
'hide_sitewide' => false
));
You can use bp_activity_add(). For example, you'd do it like this:
// record an activity item to the activity table
bp_activity_add( array(
'id' => $id
'user_id' => $user_id,
'item_id' => $item_id,
'action' => $action,
'content' => $content,
'component' => $component,
'primary_link' => $link,
'type' => $type,
'secondary_item_id' => $secondary_item_id,
'recorded_time' => $time,
'hide_sitewide' => false,
'is_spam' => $spam
));
Ref explaining what the parameters mean:
http://codex.buddypress.org/developer/function-examples/bp_activity_add/

Resources