Skip to content

Instantly share code, notes, and snippets.

@anisur2805
Created June 4, 2023 18:17
Show Gist options
  • Save anisur2805/6351a6054a19ebbe60bdbb8d530227f6 to your computer and use it in GitHub Desktop.
Save anisur2805/6351a6054a19ebbe60bdbb8d530227f6 to your computer and use it in GitHub Desktop.
Create a very basic rest api
<?php
/**
* Plugin Name: Awesome API Test
* Description: Awesome Desc...
* Plugin URI: #
* Version: 1.0
* Author: #
* Author URI: http://github.com/anisur2805/
* Text Domain: test-domain
* License: GPL v2 or later
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
*/
if ( !defined( 'ABSPATH' ) ) {
exit;
}
define( 'WPPLUGIN_DIR', __DIR__ );
// function test_init(){
// $api_url = 'https://api.github.com/users';
// $args = [
// 'method' => 'GET'
// ];
// $response = wp_remote_get($api_url, $args);
// if( is_wp_error( $response ) ) {
// $error_msg = $response->get_error_message();
// return "Something went wrong: " . $error_msg;
// }
// $results = json_decode( wp_remote_retrieve_body( $response ) );
// error_log( print_r( $results, true ) );
// }
// test_init();
// add_action( 'admin_menu', 'wpplugin_admin_menu' );
// function wpplugin_admin_menu() {
// add_menu_page( 'Testing', 'Testing', 'manage_options', 'testing', 'testing_callback' );
// }
// function testing_callback() {
// $url = 'https://jsonplaceholder.typicode1.com/users';
// $args = [
// 'method' => 'GET',
// ];
// $response = wp_remote_get( $url, $args );
// if ( 200 == wp_remote_retrieve_response_code( $response ) ) {
// $file_link = WPPLUGIN_DIR . '/query-apis/data.json';
// $message = wp_remote_retrieve_body( $response );
// write_to_file( $message, $file_link );
// }
// if ( is_wp_error( $response ) ) {
// $file_link = WPPLUGIN_DIR . '/query-apis/error.txt';
// $error_msg = $response->get_error_message();
// $msg = date( 'd m Y g:i:a' ) . ' ' . wp_remote_retrieve_response_code( $response ) . ' ' . $error_msg;
// write_to_file( $msg, $file_link );
// }
// }
// function write_to_file( $message, $file_link ) {
// if ( file_exists( $file_link ) ) {
// $filling = fopen( $file_link, 'a' );
// fwrite( $filling, $message . "\n" );
// } else {
// $filling = fopen( $file_link, 'w' );
// fwrite( $filling, $message, "\n" );
// }
// fclose( $filling );
// }
// create a rest api endpoint
add_action( 'rest_api_init', 'register_rest_api_init' );
function register_rest_api_init() {
register_rest_route(
'ar/v1/',
'test-callback',
array(
array(
'methods' => 'POST',
'callback' => 'ar_post_callback',
),
array(
'methods' => 'GET',
'callback' => 'ar_get_callback',
),
)
);
register_rest_field(
'post',
'feat_img',
array(
'get_callback' => 'rest_api_feature_image_callback',
'update_callback' => null,
'schema' => null,
)
);
register_rest_field(
'post',
'author',
array(
'get_callback' => 'rest_api_author_callback',
'update_callback' => null,
'schema' => null,
)
);
register_rest_field(
'post',
'author_description',
array(
'get_callback' => 'rest_api_author_description_callback',
'update_callback' => null,
'schema' => null,
)
);
}
function rest_api_feature_image_callback( $object ) {
$image_src = wp_get_attachment_image_src( $object['featured_media'], 'medium' );
return $image_src;
}
function rest_api_author_callback() {
$author = get_the_author_meta( 'display_name' );
return $author;
}
function rest_api_author_description_callback( $object ) {
$author_description = get_the_author_meta( 'user_description' );
return $author_description;
}
function ar_post_callback( $request_params ) {
$data = array();
$params = $request_params->get_params();
$name = $params['name'];
$password = $params['password'];
$title = $params['title'];
$content = $params['content'];
if ( isset( $name ) && isset( $password ) ) {
$userData = get_user_by( 'login', $name );
if ( $userData ) {
$wp_check_password_result = wp_check_password( $password, $userData->user_pass, $userData->ID );
if ( $wp_check_password_result ) {
$data['received_data'] = array(
'name' => $name,
'password' => $password,
// 'data' => $userData
);
$post_args = array(
'post_title' => wp_strip_all_tags( $title ),
'post_content' => $content,
'post_status' => 'publish',
'post_type' => 'apiData',
);
$inserted_id = wp_insert_post( $post_args );
if ( $inserted_id ) {
$data['status'] = 'OK';
$data['message'] = __( 'Alhamdulillah, ' ) . $title . ' was created successfully.';
} else {
$data['status'] = 'Insert Failed';
$data['message'] = __( 'Innalillah, post insert failed' );
}
} else {
$data['status'] = 'OK';
$data['message'] = __( 'You are not authenticated' );
}
} else {
$data['status'] = 'OK';
$data['message'] = __( 'User dose not exits' );
}
} else {
$data['status'] = 'Failed';
$data['message'] = __( 'Innnalillah' );
}
return $data;
}
function ar_get_callback( $request_params ) {
$data = array();
$params = $request_params->get_params();
$name = $params['name'];
$password = $params['password'];
$data = [
'status' => 'OK',
'name' => $name,
'password' => $password,
];
return $data;
}
// register a custom post type
function my_register_post_type() {
$args = array(
'public' => true,
'publicly_queryable' => false,
'label' => __( 'Rest API' ),
'supports' => array( 'title', 'editor' ),
);
register_post_type( 'apiData', $args );
}
add_action( 'init', 'my_register_post_type' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment