Class WPApiHandler

Constructors

  • Creates a new instance of the WPApiHandler class.

    Parameters

    • server_address: string

      The address of the WordPress site.

    • headers: Headers

      The headers to be used for the requests.

    Returns WPApiHandler

    Example

    const wpa = new WPApiHandler(
    'https://example.com',
    {
    "Content-Type": "application/json",
    "Authorization": "Basic YOURACCESSTOKEN"
    }
    );

Properties

headers: AxiosRequestConfig<any>
server_address: string

Methods

  • Asynchronously posts a post to the WordPress site.

    Parameters

    • Optional new_post: Post

      The post to be posted to the WordPress site.

    Returns Promise<Post>

    A promise that resolves to the post that was posted.

    Async

    Example

    const wpa = new WPApiHandler(
    'https://example.com',
    {
    "Content-Type": "application/json",
    "Authorization": "Basic YOURACCESSTOKEN"
    }
    );

    const new_post = {
    id: 1910,
    title: 'New Post',
    content: 'This is a new post.',
    status: 'publish',
    tags: ['test'],
    };

    const result = await wpa.add_post(new_post);
    console.log(result);
  • Asynchronously updates a post on the WordPress site.

    Returns Promise<boolean>

    A promise that resolves to the post that was updated.

    Async

    Throws

    AuthenticationError If the authentication failed.

    Example

    const wpa = new WPApiHandler(
    'https://example.com',
    {
    "Content-Type": "application/json",
    "Authorization": "Basic YOURACCESSTOKEN"
    }
    );

    const updated_post = {
    id: 1910,
    title: 'Updated Post',
    content: 'This is an updated post.',
    status: 'publish',
    tags: [1, 2, 3],
    };

    const result = await wpa.update_post(updated_post);
    console.log(result);
  • Asynchronously retrieves the events from the WordPress site.

    Parameters

    • Optional event_id: number

      The ID of a specific event to be retrieved.

    Returns Promise<Event[]>

    A promise that resolves to an array of events.

    Async

    Example

    const wpa = new WPApiHandler(
    'https://example.com',
    {
    "Content-Type": "application/json",
    "Authorization": "Basic YOURACCESSTOKEN"
    }
    );

    const events = await wpa.get_events();
    console.log(events);

    const events = await wpa.get_events(1);
    console.log(events);
  • Asynchronously retrieves the partners from the WordPress site.

    Parameters

    • Optional project: string

      The project of the partners to be retrieved.

    Returns Promise<Partner[]>

    A promise that resolves to an array of partners.

    Async

    Example

    const wpa = new WPApiHandler(
    'https://example.com',
    {
    "Content-Type": "application/json",
    "Authorization": "Basic YOURACCESSTOKEN"
    }
    );

    const partners = await wpa.get_partners();
    console.log(partners);

    const partners = await wpa.get_partners('Test');
    console.log(partners);
  • Asynchronously retrieves the personnel from the WordPress site.

    Parameters

    • Optional search: string

      The search term to filter the personnel.

    Returns Promise<Personnel[]>

    A promise that resolves to an array of personnel.

    Async

    Example

    const wpa = new WPApiHandler(
    'https://example.com',
    {
    "Content-Type": "application/json",
    "Authorization": "Basic YOURACCESSTOKEN"
    }
    );

    const personnel = await wpa.get_personnel();
    console.log(personnel);

    const personnel = await wpa.get_personnel('John Doe');
    console.log(personnel);
  • Asynchronously retrieves posts from the WordPress site.

    Parameters

    • Optional id: number

      The ID of the post to be retrieved.

    • Optional tags: string[]

      The tags of the posts to be retrieved.

    Returns Promise<Post[]>

    A promise that resolves to an array of posts.

    Async

    Throws

    PostNotFoundError If the post does not exist.

    Example

    const wpa = new WPApiHandler(
    'https://example.com',
    {
    "Content-Type": "application/json",
    "Authorization": "Basic YOURACCESSTOKEN"
    }
    );

    const posts = await wpa.get_posts();
    console.log(posts);

    const post = await wpa.get_posts(1910);
    console.log(post);

    const posts = await wpa.get_posts(undefined, ['test']);
    console.log(posts);
  • Asynchronously retrieves the tag ID by its slug.

    Parameters

    • tag: string

      The slug of the tag to be retrieved.

    • Optional createIfNotExists: boolean = false

      Whether to create the tag if it does not exist.

    Returns Promise<number>

    A promise that resolves to the ID of the tag.

    Async

    Throws

    If the tag does not exist and createIfNotExists is false.

    Example

    const wpa = new WPApiHandler(
    'https://example.com',
    {
    "Content-Type": "application/json",
    "Authorization": "Basic YOURACCESSTOKEN"
    }
    );

    const tag_id = await wpa.get_tag_slug('test');
    console.log(tag_id);
  • Asynchronously retrieves the tags by their IDs.

    Parameters

    • tag_ids: number[]

      The IDs of the tags to be retrieved.

    Returns Promise<string[]>

    A promise that resolves to an array of tags.

    Async

    Example

    const wpa = new WPApiHandler(
    'https://example.com',
    {
    "Content-Type": "application/json",
    "Authorization": "Basic YOURACCESSTOKEN"
    }
    );

    const tag_ids = [1, 2, 3];
    const tags = await wpa.get_tags(tag_ids);
    console.log(tags);
  • Asynchronously retrieves the total number of posts on the WordPress site.

    Returns Promise<number>

    A promise that resolves to the total number of posts.

    Async

    Example

    const wpa = new WPApiHandler(
    'https://example.com',
    {
    "Content-Type": "application/json",
    "Authorization": "Basic YOURACCESSTOKEN"
    }
    );

    const total_posts = await wpa.post_len();
    console.log(total_posts);
  • Asynchronously removes a post from the WordPress site.

    Parameters

    • Optional id: number

      The ID of the post to be removed from the WordPress site.

    Returns Promise<void>

    A promise that resolves to void.

    Async

    Example

    const wpa = new WPApiHandler(
    'https://example.com',
    {
    "Content-Type": "application/json",
    "Authorization": "Basic YOURACCESSTOKEN"
    }
    );

    await wpa.remove_post(1910);

Generated using TypeDoc