Last active
April 28, 2019 15:26
-
-
Save debabratakarfa/c956191803f2d0c6de9e8090a471e8bc to your computer and use it in GitHub Desktop.
WooCommerce Custom Rest API Endpoint v3 [creating for https://domain.tld/wc-api/v3/my-custom-endpoint]
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
add_action( 'woocommerce_api_loaded', 'my_plugin_load_api' ); | |
add_filter( 'woocommerce_api_classes', 'my_plugin_add_api' ); | |
function my_plugin_load_api() { | |
include_once 'your-api-endpoint-class.php'; | |
} | |
function my_plugin_add_api( $apis ) { | |
$apis[] = 'YOUR_API_ENDPOINT_CLASS_NAME'; | |
return $apis; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class YOUR_API_ENDPOINT_CLASS_NAME extends WC_API_Resource { | |
protected $base = '/my-custom-endpoint'; | |
public function register_routes( $routes ) { | |
$routes[ $this->base ] = array( | |
array( array( $this, 'get_custom' ), WC_API_Server::READABLE ) | |
); | |
return $routes; | |
} | |
public function get_custom() { | |
return array( 'custom' => 'Data' ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment