Skip to content

Instantly share code, notes, and snippets.

View anka's full-sized avatar

Andreas anka

View GitHub Profile
@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');
@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-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-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.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 / 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 / slack-files-download.py
Created June 29, 2022 15:25
Downloads all files of messages of your Slack (workspace) JSON export.
#!/usr/bin/python
import argparse
import json
import os
import sys
from typing import List, Optional
from urllib.request import urlopen
"""Downloads all files of messages of your Slack (workspace) JSON export. Each channel
@anka
anka / currency_input.rb
Created June 18, 2022 19:35
Custom currency input using SimpleForm
class CurrencyInput < SimpleForm::Inputs::StringInput
def input(wrapper_options)
input_options = input_html_options.merge({data: {autonumeric: true}})
merged_input_options = merge_wrapper_options(input_options, wrapper_options)
template.content_tag(:div, class: 'input-group') do
template.concat span_currency_sign
template.concat @builder.text_field(attribute_name, merged_input_options)
end
end
@anka
anka / azure-py-func-setup.sh
Last active August 2, 2022 07:49
Workaround for running a Python Azure Function locally on an M1
# Install Python
brew install python@3.9
# Install NVM
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash
# Update/check your source files (.zshrc, .bashrc, etc.)
export PATH="~/.nvm/current/bin:$PATH"
# Install specific Node version
@anka
anka / custom.js
Created June 1, 2022 08:52
Hubspot custom workflow function to send transactional email with custom data
const hubspot = require('@hubspot/api-client');
exports.main = async (event, callback) => {
// initialize the hubspot api client with your api secret
const hubspotClient = new hubspot.Client({
apiKey: process.env.API_KEY
});
// the ID of the email template
const EMAIL_TEMPLATE_ID = 82546305923;