Skip to content

Instantly share code, notes, and snippets.

View thisbit's full-sized avatar

thisbit thisbit

View GitHub Profile
@thisbit
thisbit / custom_hook_shortcode.php
Created February 1, 2022 15:19
Custom Hook in Shortcode
<?php
// place [content_here] shortcode where you want to place your hooked element
function apuri_hero_hook_function( $atts, $content = null ) {
ob_start();
do_action( 'prefix_content_here' );
return ob_get_clean();
}
@thisbit
thisbit / post_object_controled_loop_in_shortcode.php
Created February 2, 2022 11:53
Custom Hook in ACF post object based loop in Shortcode
<?php
// Based on https://gist.github.com/diggeddy/b71bf07aa55eecb0c34191f9fe05d224
// justom some styles to display as 4col grid
add_action( 'wp_head', function () { ?>
<style>
@media (min-width: 600px) {
.wp-block-post-template.is-flex-container.is-flex-container.columns-4>div, .wp-block-query-loop.is-flex-container.is-flex-container.columns-4>div {
width: calc(25% - 0.9375em);
}
@thisbit
thisbit / create_and_insert_custom_hook_for_mobile_only.php
Created February 7, 2022 10:19
Create a custom hook for mobile version at the bottom of the site, to insert a block element into there.
// not sure if this is ok, but it works on my local generatepress site
function prefix_mobile_quicklinks() {
if ( wp_is_mobile() && function_exists( 'generate_after_footer' ) ) :
do_action( 'mobile_quicklinks' );
endif;
}
add_action( 'generate_after_footer', 'prefix_mobile_quicklinks' );
@thisbit
thisbit / svg_support.php
Created February 17, 2022 15:06
svg support for wordpress media upload
<?php
function add_svg_to_upload_mimes( $upload_mimes ) {
$upload_mimes['svg'] = 'image/svg+xml';
$upload_mimes['svgz'] = 'image/svg+xml';
return $upload_mimes;
}
add_filter( 'upload_mimes', 'add_svg_to_upload_mimes', 10, 1 );
@thisbit
thisbit / modify_role_capabilities.php
Last active February 21, 2022 13:53
Modify wordpress user role capabilities function
<?php
/**
* Add or remove capabilities from user roles
* @link https://kinsta.com/blog/wordpress-user-roles/
*//
function modify_role_caps() {
// Get roles
$author = get_role( 'author' );
$editor = get_role( 'editor' );
@thisbit
thisbit / generateblocks_accordion.php
Last active February 25, 2022 01:16
A snippet to be used with GenerateBlocks WordPress plugin to build your own Accordions
<?php
// Design your own Accordions
// remove the <?php part at the begining of the file when including in your child theme
// this snippet contains basic functionality at the moment I will develop it further, so drop by again in a while
function thisbit_accordion_script() { ?>
<script id="toggler">
// Reusable function for togglig one ore more elements in a page.
// Add the class click-me to elements you want to triger the clicking.
@thisbit
thisbit / custom-home-page-query.php
Last active April 18, 2022 06:18
Query once and ouput multiple loops in vearious sections of the page to achieve a more performant homepage, Work in progress
<?php
/**
* Template name: Apuri home
* Description: Single Query Multiple Loops for a Performant Sectioned Homepage With GeneratePress
* @package WordPress
* @subpackage GeneratePress
* @link https://code.tutsplus.com/tutorials/how-to-code-multiple-loops-while-only-querying-the-database-once--cms-25703
* @link https://wpmudev.com/blog/how-to-use-one-query-to-run-multiple-loops/
* @todo Add transients in the query to serve from there instead of DB where applicable
* Author: @thisbit
@thisbit
thisbit / custom-search-and-filter.php
Created February 25, 2022 19:12
Custom search and filter accross two loops with one filter/search form
<?php
/**
* Template name: Custom search
* Description: Search and filter accross two loops of two different CPTS
* @todo create two loops over same cpt with different taxonomies
* @todo create radiobuttons instead of select
* @todo maybe in time add ajax
* @todo maybe rather custom fields rather then taxonomies
* Warn: this is a work in progress
@thisbit
thisbit / typography_front_and_editor.php
Last active March 13, 2023 16:29
Enquing files to have same typesetting in both front and editor
<?php
/**
* This is a gist that goes with the following tutorial
* @link https://thisbit.medium.com/animated-design-system-with-generatepresss-and-generateblocks-responsive-fluid-typography-292e02eeeb15
*/
function prefix_load_assets( ) {
wp_enqueue_style( 'prefix-type', get_stylesheet_directory_uri() . '/assets/css/prefix-typography.css', false, '1.0.0', 'all');
}
@thisbit
thisbit / conditionals_users.php
Last active March 4, 2022 16:59
user condition snippet
<?php
/**
* User role condition template, I prefer to use capabilities but here goes
**/
function apuri_allowed_block_types_per_usergroup() {
$user = wp_get_current_user(); // if used in theme
$allowed_roles = array(
'contributor',