Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create custom endpoints #29

Open
str opened this issue Sep 2, 2020 · 8 comments
Open

Create custom endpoints #29

str opened this issue Sep 2, 2020 · 8 comments

Comments

@str
Copy link

str commented Sep 2, 2020

I'm trying to create my custom end point but it looks like the endpoints are hardcoded to be classess form the Vnn\WpApiClient\Endpoint namespace;

What's the easiest way to define a custom endpoint?

@jhhazelaar
Copy link

Did you manage to add a custom end-point? We need it to add custom post types to WP

@jhhazelaar
Copy link

I think we need this #20

@morban
Copy link

morban commented May 28, 2021

Yes it will be very interesting.

@zack-carlson
Copy link

zack-carlson commented Feb 6, 2023

Hello.

Go into /vendor/vnn/wordpress-rest-api/src/Endpoints and copy "Posts" class to your main app directory and rename the class to CustomPostType and update namespace appropriately. Then when you call, you'll need to do it slightly different..

So in your "GetWPApiData" class (for example) .. in a method you need to get custom endpoint data, include your class CustomPostType in the "use" area, make sure it implements "AbstractWpEndpoint" ..

protected function getEndpoint()
    {
        return '/wp-json/wp/v2/mycustomendpoint';
    }

Then initiate

$custom_post_type_data = new CustomPostType($this->client);

return $custom_post_type_data->get();

Works great, don't have to fork or override private properties.

@pekka
Copy link

pekka commented Mar 4, 2023

Works great, don't have to fork or override private properties.

Lovely - much better than hacking away in the package, then losing changes on updating... thank you!

@angelxmoreno
Copy link

Any ideas why the PR was not merged? it seems like a reasonable feature. @zack-carlson could you add the feature to the readme? something like this: https://wprestclient.readthedocs.io/en/latest/extending/api-prefix/

@zack-carlson
Copy link

zack-carlson commented Jun 12, 2023

@angelxmoreno I'm not sure the approach is what the maintainer wished for, but would be happy to copy the comment / explanation

<?php

namespace App\WpApiClient;

use Vnn\WpApiClient\Endpoint\AbstractWpEndpoint;

/**
 * Class Posts
 * @package Vnn\WpApiClient\Endpoint
 */
class Promotion extends AbstractWpEndpoint
{
    /**
     * {@inheritdoc}
     */
    protected function getEndpoint()
    {
        return '/wp-json/wp/v2/promotion';
    }
}

@angelxmoreno
Copy link

That's cool @zack-carlson

BTW, I ended up creating my own, WPRestClient because there was no license usage. The docs I linked were from my lib. I just wanted to promote the contribution of this lib since it served as an inspiration for creating WPRestClient

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants