Skip to content

Instantly share code, notes, and snippets.

View anka's full-sized avatar

Andreas anka

View GitHub Profile
@anka
anka / line-items-email.html
Created February 16, 2023 19:25
Display line items data within an automated Hubspot e-mail template
<div class="data-container">
<div class="data-title">Details of your order</div>
<div class="data">
<table style="width:100%;font-size:.8rem;">
<tr>
<th style="width:2%;">Pos</th>
<th style="width:2%;">Qty</th>
<th style="width:70%;">Name</th>
<th style="width:20%;text-align:right;">Price</th>
</tr>
@anka
anka / custom-api-route.php
Created April 7, 2023 18:09
Wordpress plugin stub
<?php
/*
Plugin Name: Custom API Route
Description: A simple plugin to demonstrate how to implement a custom JSON API route in WordPress.
Version: 1.0
Author: Your Name
*/
@anka
anka / custom-api-route-2.php
Created April 7, 2023 18:11
Wordpress plugin code to register a custom route
add_action('rest_api_init', 'register_custom_route');
function register_custom_route() {
register_rest_route('custom-api-route/v1', '/submit', array(
'methods' => 'POST',
'callback' => 'process_data_submission',
'permission_callback' => '__return_true',
));
}
@anka
anka / custom-api-route-3.php
Created April 7, 2023 18:13
Example implementation of a Wordpress function to process a POST request
function process_data_submission(WP_REST_Request $request) {
$data = json_decode($request->get_body(), true);
// Perform your data processing here
$response = new WP_REST_Response(array('status' => 'success', 'message' => 'Data processed successfully'));
$response->set_status(200);
return $response;
}
@anka
anka / custom-route-script.js
Created April 7, 2023 18:17
Example JavaScript code to consume a custom JSON API in a Wordpress plugin
document.addEventListener('DOMContentLoaded', async () => {
const data = {
key1: 'data1',
key2: 'data2',
};
const response = await fetch('/wp-json/custom-api-route/v1/submit', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
@anka
anka / custom-api-route-4.php
Created April 7, 2023 18:19
Enqueue a JavaScript file in a custom Wordpress plugin
function custom_route_enqueue_scripts() {
wp_enqueue_script('custom-route-script', plugin_dir_url(__FILE__) . 'custom-route-script.js', array(), '1.0', true);
}
add_action('wp_enqueue_scripts', 'custom_route_enqueue_scripts');