Skip to content

Instantly share code, notes, and snippets.

@k1sul1
k1sul1 / plugin-cpt-with-templates.php
Created May 30, 2017 14:09
Way to add custom post types with template support directly from plugin
<?php
/*
Plugin Name: Plugin cpt with templates
Author: Christian Nikkanen
*/
defined("ABSPATH") or die("Wat r u doing?");
add_action("init", function() {
register_post_type("blog", [
@k1sul1
k1sul1 / WP Libre Form ohjeistus.html
Created June 6, 2017 08:42
Ohjeet lomakkeiden hallintaan.
<!-- Tämä on HTML kommentti, se ei näy sivustolla.
Input-elementeillä (<input>, <select></select>, <textarea></textarea>) on muutama attribuutti jotka vaikuttavat siihen miten elementti käyttäytyy.
Type-attribuutti nimensä mukaisesti vaikuttaa elementin tyyppiin. Yleisimmät tyypit ja mitä ne tekevät:
text: Oletus, tavallinen tekstikenttä, esimerkiksi nimeä varten.
email: Sähköpostikenttä, jos haluat vastaajan sähköpostiosoitteen, käytä tätä. Yhdistettynä required attribuutin kanssa validoi että kentässä on sähköpostiosoite ennen lomakkeen tallentamista.
checkbox: Rasti ruutuun kenttä. Jos vastaaja voi valita monta vaihtoehtoa, käytä tätä. Huomaa että tämän tyypin kanssa name-attribuutissa pitää olla arvon jälkeen [] jotta kaikki vastaukset tallentuvat.
radio: Valitse yksi kenttä. Antaa käyttäjän valita vain yhden vaihtoehdon monesta.
@k1sul1
k1sul1 / routeWatcher.js
Created January 1, 2018 12:26
node script that watches a directory and marks whether the files listed exist or not, conditional component loading
const fs = require('fs')
const watch = require('node-watch')
const routes = require('./routes.json')
const route = {
write(type, name) {
const types = ['enable', 'disable']
const state = types.indexOf(type) > -1 ? [true, false][types.indexOf(type)] : new Error('Wat?')
const fresh = require('./routes.json')
@k1sul1
k1sul1 / index.php
Created August 30, 2018 16:36
WordPress: Block plugin activation if PHP or WordPress version is too low
<?php
register_activation_hook(__FILE__, function () {
$php_version = phpversion();
$wp_version = $GLOBALS["wp_version"];
$is_56 = version_compare($php_version, 5.6, ">=");
$is_70 = version_compare($php_version, 7.0, ">=");
$php_ok = $is_56 || $is_70;
$wp_ok = version_compare($wp_version, 4.7, ">=");
$message = "";
@k1sul1
k1sul1 / README.md
Last active December 12, 2019 15:16
CircleCI pipeline for WordPress projects running on Seravo/wordpress project template

CircleCI pipeline for WordPress projects running on Seravo/wordpress project template

Builds your theme, installs composer dependencies and deploys it to production / staging.

How?

Create .circleci folder in project root, and add config.yml in it. Edit the config a bit. Use search & replace to substitute $PROD_SSH_PORT to the SSH port number and $THEMENAME to the folder name of your theme.

Login to CircleCI and add a project using the menu in the sidebar. Click "Set Up Project" on the project you want to setup CI for.

@k1sul1
k1sul1 / yolo.php
Created January 9, 2020 19:44
Get all translations of post content from a value translated with qtranslate-x
<?php
$post = $GLOBALS['post'];
$wpdb = $GLOBALS['wpdb'];
// :trollface:
// qtranslate is not a very nice tool
$id = (int) $post->ID; // No injections here
$raw = $wpdb->get_var("SELECT post_content from $wpdb->posts WHERE ID = $id");
$extractor = function($lang = 'fi') use ($raw) {
@k1sul1
k1sul1 / build-non-split.js
Created April 1, 2020 14:45
Build create-react-app to a single file. Useful for things like WordPress plugins.
const rewire = require('rewire');
const defaults = rewire('react-scripts/scripts/build.js');
let config = defaults.__get__('config');
config.optimization.splitChunks = {
cacheGroups: {
default: false,
},
};
@k1sul1
k1sul1 / wordpress.types.ts
Created April 28, 2020 15:44
Mostly autogenerated type defs for WordPress REST API
// Below: Autogenerated types from Quicktype. They have been modified by hand.
// ACF fields in standard endpoints do not exist out of the box, you have to add them yourself.
// k1sul1/k1kit does that for you automagically.
// To parse this data:
//
// import { Convert, RawStoneData, Texts, Taxonomies, PostTypes, PostStatuses } from "./file";
//
// const taxonomies = Convert.toTaxonomies(json);
// const tags = Convert.toTags(json);
@k1sul1
k1sul1 / webpack.config.babel.js
Last active December 15, 2023 18:47
Webpack configuration for building a WordPress plugin with React and Typescript
const path = require("path");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const ManifestPlugin = require("webpack-manifest-plugin");
const WriteFilePlugin = require("write-file-webpack-plugin");
const TerserJSPlugin = require("terser-webpack-plugin");
const OptimizeCSSAssetsPlugin = require("optimize-css-assets-webpack-plugin");
const BundleAnalyzerPlugin = require("webpack-bundle-analyzer")
.BundleAnalyzerPlugin;
@k1sul1
k1sul1 / mousemove.ts
Created May 11, 2020 13:30
I spent way too much time digging the correct properties. For future reference.
function onMouseMove(event, initialX: number, initialY: number) {
const { clientX, clientY, movementX, movementY } = event
const moving = {
left: movementX < 0
right: movementX > 0
up: movementY < 0
down: movementY > 0
}