Skip to content

Instantly share code, notes, and snippets.

View mennwebs's full-sized avatar

Menn mennwebs

View GitHub Profile
@mennwebs
mennwebs / functions.php
Last active September 25, 2024 03:17
Add Blog related field to REST API
<?php
// Add Blog related field to REST API /wp-json/wp/v2/blog
function blog_add_related_posts()
{
register_rest_field('blog', 'related', [
'get_callback' => function ($post) {
$categories = wp_get_post_categories($post['id']);
$related = get_posts([
'post_type' => 'blog',
@mennwebs
mennwebs / functions.php
Last active September 25, 2024 03:17
Add Blog CPT to category archive
<?php
// Add Blog CPT to category archive
add_action('pre_get_posts', 'blog_set_category');
function blog_set_category($query)
{
if (is_category() && !is_admin() && $query->is_main_query()) {
$query->set('post_type', 'blog');
}
}
@mennwebs
mennwebs / function.php
Last active July 2, 2024 07:00 — forked from mattclements/function.php
Wordpress Disable Comments (add to function.php)
<?php
add_action('admin_init', function () {
// Redirect any user trying to access comments page
global $pagenow;
if ($pagenow === 'edit-comments.php') {
wp_redirect(admin_url());
exit;
}
@mennwebs
mennwebs / functions.php
Last active May 17, 2024 03:13
WordPress - Event Post Type Slug from date
<?php /* EVENT SLUG FROM DATE*/
function event_slug($post_id, $post, $update)
{
if (get_field('date', $post_id)) {
$date = get_field('date', $post_id);
$slug = date('ymd', strtotime($date));
if ($post->post_name != $slug) {
remove_action('save_post_event', 'event_slug', 10, 3);
wp_update_post([
@mennwebs
mennwebs / slack-files-downloader.sh
Last active July 1, 2024 03:34 — forked from greird/slack-files-downloader.sh
Download all files from a Slack workspace export folder.
#!/bin/bash
#
# This script will browse a Slack export folder and download all files in a new /export folder
#
# HOW TO:
# 1. As a Workspace admin, download an export of your Slack history (https://www.slack.com/services/export)
# 2. Make sure you have jq installed (https://stedolan.github.io/jq/)
# 3. Place this file at the root of your Slack export folder, next to channels.json
# 4. Run `bash slack-files-downloader.sh` in your terminal
#
@mennwebs
mennwebs / functions.php
Last active May 17, 2024 03:12
WordPress - Random Slug
<?php
add_filter('wp_unique_post_slug', 'seed_unique_slug', 10, 4);
function seed_unique_slug($slug, $post_id, $post_status, $post_type)
{
if (in_array($post_type, ['post', 'product'])) {
$post = get_post($post_id);
if (empty($post->post_name) || $slug != $post->post_name) {
$slug = randomSlug();
}
}
@mennwebs
mennwebs / deploy-sftp.yml
Created January 10, 2024 01:53
GitHub Action: Push to Deploy sFTP
on: [push]
jobs:
deploy_job:
runs-on: ubuntu-latest
name: deploy
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Install Dependencies
@mennwebs
mennwebs / th-address.json
Created November 20, 2023 02:16
Thai Address from Postal Code - JSON
This file has been truncated, but you can view the full file.
[
{
"zipCode": "10100",
"subDistrictList": [
{
"subDistrictId": "100801",
"districtId": "1008",
"provinceId": "10",
"subDistrictName": "ป้อมปราบ"
},
@mennwebs
mennwebs / functions.php
Created October 18, 2023 12:00
WP - Running No. Slug
<?php
// Running No. Slug
add_action('wp_insert_post', 'change_slug');
function change_slug($post_id, $force = false)
{
$post_types = ['news', 'faq'];
$post_type = $_POST['post_type'];
if (!in_array($post_type, $post_types) || get_field('no', $post_id)) {
return;
@mennwebs
mennwebs / style.css
Created February 26, 2023 08:51
Class _th and _en (show only Thai, English language)
._th:not(:lang(th)),
._en:not(:lang(en-US)) {
display: none !important;
}