Skip to content

Instantly share code, notes, and snippets.

View brandonkramer's full-sized avatar
🏠
Working from home

Brandon Kramer brandonkramer

🏠
Working from home
View GitHub Profile
@brandonkramer
brandonkramer / vite-js-retain-input-path-as-output-path.js
Created April 17, 2022 23:10
Makes sure you retain the same input path structure as output file path structure in ViteJS
const rootPath = 'your-project/root-path';
export default defineConfig() => {
build: {
rollupOptions: {
output: {
entryFileNames: (assetInfo) => assetInfo.facadeModuleId.replace( path.resolve( __dirname, rootPath ) + '/', '' ),
}
}
}
@brandonkramer
brandonkramer / vite-js-retain-css-file-path.js
Last active April 17, 2022 23:06
This ViteJS plugin will make sure compiled CSS files will have the same path structure as JS files which allows you to retain the same input path structure as output path
import path from 'path'
export default (cssBundleNameIndex = []) => ({
/**
* @see https://rollupjs.org/guide/en/#renderchunk
*/
renderChunk(code, chunkInfo) {
const {
fileName: jsFileName,
@brandonkramer
brandonkramer / get-internal-links-from-yoast.php
Created April 13, 2022 17:36
Get the number of internal links linking to a post and the number of outgoing internal links from Yoast indexable using their models.
@brandonkramer
brandonkramer / wrap-google-doc-iframes.php
Last active April 12, 2022 23:58
Get all (preg_match_all) Google docs embed iframes and wrap it with a div with PHP
<?php
preg_match_all( '@<iframe[^>]+?src="((?:https?:)?//(?:docs\.)?google\.com/([^"\'/]++)[^"\']*?)"[^>]*+>.*</iframe>@iUs', $content, $matches );
if ( isset( $matches[0] ) && isset( $matches[0][0] ) ) {
foreach ( $matches[0] as $googleDocIframe ) {
$content = str_replace( $googleDocIframe, '<div class="google-doc-iframe">' . $googleDocIframe . '</div>', $content );
}
}
@brandonkramer
brandonkramer / wrap-elements-and-their-siblings-with-vanilla-javascript.js
Last active April 12, 2022 23:57
Wrap elements and their previous and next siblings with vanilla javascript
// Get all the elements
document.querySelectorAll( '.my-element' ).forEach( (el) => {
var prevEl = el.previousElementSibling, nextEl = el.nextElementSibling, elArr = [], html = '';
if ( prevEl === null || nextEl === null ) return;
// In case you want to include the two elements before the previous sibling
[prevEl.previousElementSibling, prevEl.previousElementSibling.previousElementSibling].forEach((topEl) => {
if ( topEl !== null ) elArr.push(topEl);
})
@brandonkramer
brandonkramer / wp-change-attachment-file-path.php
Last active April 9, 2022 22:49
Programmatically change the file path of a single attachment or multiple attachments in WordPress. Can be used in a loop or any other use cases in which you have the attachment_id in hand.
<?php
$attachment_id = $your_way_of_getting_the_id;
$file_path = '2022/02/my-image.jpg';
// Update the path inside the post meta
update_post_meta( $attachment_id, '_wp_attached_file', $attachment_id );
$old_metadata = wp_get_attachment_metadata( $attachment_id );
$old_metadata['file'] = $attachment_id;
@brandonkramer
brandonkramer / exclude_post_types_from_yoast_sitemap.php
Last active February 10, 2022 21:03
A short snippet using the Yoast filter to exclude post types from the Yoast Sitemap.
<?php
add_filter( 'wpseo_sitemap_exclude_post_type', 'your_prefix_exclude_cpt_from_sitemap', 10, 2 );
function your_prefix_exclude_cpt_from_sitemap( $value, $post_type ) {
$post_types_to_exclude = [
'post_type_one',
'post_type_two'
];
if ( in_array($post_type, $post_types_to_exclude) ) {
@brandonkramer
brandonkramer / notion.php
Last active October 17, 2023 21:18
(Notion.so) Notion API Integration (PHP) - A simple snippet which tells you, how to add a new page to a Notion database with PHP using Curl and their public API. You need a secret token and the ID of a "Shared" database. In the example below the database needs to have a text property called "Description" and the default "Name" property.
<?php
// Options
$secret_token = 'YOUR_SECRET_TOKEN';
$database_id = 'YOUR_DATABASE_ID';
$notion_version = '2021-08-16';
// Post url
$post_url = 'https://api.notion.com/v1/pages';
@brandonkramer
brandonkramer / instagram-curl.php
Last active May 30, 2021 17:12
Instagram oEmbed / embed request with PHP / Curl
<?php
// Access token with App Id
$appId = "";
$accessToken = "";
function fetchData( $url ) {
$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, $url );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt( $ch, CURLOPT_TIMEOUT, 20 );
@brandonkramer
brandonkramer / woocommerce-single-category-redirect.php
Last active September 1, 2021 16:47
WooCommerce: If a parent category has only one subcategory then it will automatically redirect to that subcategory.
<?php
add_action( 'template_redirect', function () {
global $wp_query;
if ( is_archive() ) {
$queried_object = get_queried_object();
if (
isset( $queried_object->taxonomy ) &&
isset( $queried_object->term_id ) &&
$queried_object->taxonomy === 'product_cat' &&
count( get_term_children( $queried_object->term_id, 'product_cat' ) ) === 1 ) {