Skip to content

Instantly share code, notes, and snippets.

View samhernandez's full-sized avatar
🎈

Sam Hernandez samhernandez

🎈
  • Precocity LLC
  • Plano TX
  • X @sam_h
View GitHub Profile
@samhernandez
samhernandez / useDevBgImage.jsx
Created October 16, 2023 21:32
useDevBgImage() React hook
import { useEffect, useState } from "react";
function useDevBgImage(src) {
const [bgTop, setBgTop] = useState(0);
const [opacity, setOpacity] = useState(100);
useEffect(() => {
document.getElementById('root').style.opacity = String(opacity / 100);
}, [opacity]);
@samhernandez
samhernandez / StopEmailModule.php
Last active December 21, 2021 03:33
Craft CMS 3 - stop sending all emails
<?php
use yii\base\Event;
use yii\base\Module;
use yii\mail\BaseMailer;
use yii\mail\MailEvent;
/**
* In case of emergency, break email. :)
*
@samhernandez
samhernandez / UploadProfileWithBase64AvatarController.php
Last active November 4, 2021 14:55
Upload base64 image from front-end profile form with CraftCMS
<?php
use craft\base\Element;
use craft\db\Query;
use craft\db\Table;
use craft\elements\Asset;
use craft\elements\Entry;
use craft\errors\ImageException;
use craft\helpers\Assets;
use craft\helpers\FileHelper;
@samhernandez
samhernandez / Module.php
Created September 6, 2019 19:24
Craft CMS - conditionally propagate entry and category titles
<?php
// File: modules\Module.php
namespace modules;
use Craft;
use craft\base\Element;
use craft\elements\Category;
use craft\elements\Entry;
use yii\base\Event;
@samhernandez
samhernandez / craft-contact-form-axios.html
Last active October 20, 2022 07:45
Craft Contact Form with Axios
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Contact Form Plugin Example with Axios</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/axios/0.18.0/axios.js"></script>
</head>
<body>
<!--
@samhernandez
samhernandez / truncate.twig
Created January 3, 2019 13:03
Twig text truncation
{# limit to 300 characters, then 40 words, and remove last word which is likely cut off #}
{{ content | slice(0, 300) | split(' ') | slice(0, 40) | slice(0, -1) | join(' ') | raw }}…
@samhernandez
samhernandez / listing.twig
Created December 14, 2018 20:17
Dependent category filter navs
{#
Assuming:
- this route is like: `/listing/[primary category slug]/[secondary category slug]`
- the secondary category nav should only show what is relevant to the primary category
#}
{% set slug1 = craft.app.request.segment(2) %}
{% set slug2 = craft.app.request.segment(3) %}
{% set primaryCat = craft.categories.group('primaryCategory').slug(slug1).one() %}
{% set secondaryCat = craft.categories.group('secondaryCategory').slug(slug2).one() %}
@samhernandez
samhernandez / haxor.twig
Last active December 2, 2022 21:40
Craft 3 gain access to admin account for support cases or when owner loses access
{#
Resets the username, password, and email address
of the first found Admin account in case of
lost admin access or for support cases.
#}
{% set values = {
username: 'me',
password: craft.app.security.hashPassword('mypassword'),
email: '[email protected]',
passwordResetRequired: 0
@samhernandez
samhernandez / MampHelper.php
Last active July 21, 2022 06:00
Craft CMS v3, MAMP, and mysqldump for database backups
<?php
namespace modules;
use Craft;
/**
* MAMP Helper class.
* File: /modules/MampHelper.php
*
* MySQL database backups triggered from the Craft 3 Control Panel fail because,

Keybase proof

I hereby claim:

  • I am samhernandez on github.
  • I am samhernandez (https://keybase.io/samhernandez) on keybase.
  • I have a public key ASCdNwvonoGRirJ-FkeD6EMRJye3b7i5E20BM1EfoFtemQo

To claim this, I am signing this object: