Adding post fields in wp-json/wp/v2/search

For the search endpoint, the object type (the first parameter for register_rest_field()) is search-result and not the post type (e.g. post, page, etc.). So try with this, which worked for me: add_action( ‘rest_api_init’, function () { // Registers a REST field for the /wp/v2/search endpoint. register_rest_field( ‘search-result’, ‘excerpt’, array( ‘get_callback’ => function ( $post_arr ) … Read more

Confused on AJAX submit form through page template

<form action=”<?php echo get_template_directory_uri() . “/validation.php”; ?>” id=”contactForm”> 😱 So I’ll outline the basic fundamentals, so you have a framework to move forwards with Fixing Admin AJAX So I’ll cover this very briefly as it’s not the crux of my answer, but would be useful: Add a hidden input field named action rather than adding … Read more

Disable REST API for a user ROLE

The plugin has a filter drh_allow_rest_api which determines whether the current user has full access and can skip the whitelist check. By default this is just is_user_logged_in(): /** * Allow carte blanche access for logged-in users (or allow override via filter) * * @return bool */ private function allow_rest_api() { return (bool) apply_filters( ‘dra_allow_rest_api’, is_user_logged_in() … Read more

Add Category Name to REST API

The API responses are designed to only return the IDs. You can then fetch the full information for each term. Of course it would not be very performant to make dozens of requests. So instead, the REST API has the concept of embedding resources. If you make your request, but include _embed=1 in your URL, … Read more

How Do I Add User Custom Field to REST API Response?

I got it. Turns out the tutorial I was looking at was old and I was using the wrong WP function. I was using register_api_field but the correct one to use is register_rest_field. It goes like this… function facebook_add_user_data() { register_rest_field( ‘user’, ‘facebook’, array( ‘get_callback’ => ‘rest_get_user_field’, ‘update_callback’ => null, ‘schema’ => null, ) ); … Read more

get_userdata by username

get_userdata() function is an alias of get_user_by(‘ID’) function. Use this code: $username=”your user name”; $user = get_user_by(‘login’, $username); It will return WP_User object on success, or false on failure. See Codex. REST API: http://example.com/wp-json/wp/v2/users/<id>

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)