Skip to content

Instantly share code, notes, and snippets.

View swinggraphics's full-sized avatar

Greg Perham swinggraphics

  • Swing Graphics
  • Northampton, MA
View GitHub Profile
@swinggraphics
swinggraphics / allow-links-in-excerpts.php
Created June 7, 2018 01:03
WordPress strips out tags in `wp_trim_words()`, which is called by `get_the_excerpt()`; so we have to filter 'wp_trim_words', basically copying that function with one change: replace `wp_strip_all_tags()` with `strip_tags()`. We don't want other functions that run `wp_trim_words` to be modified, so we add our filter while `get_the_excerpt()` is …
@swinggraphics
swinggraphics / broadcast-replace-site_url.php
Created May 14, 2018 23:21
Replace site_url in posts syndicated by WP Broadcast
<?
/**
* Hook into Broadcast
* Modify links in the broadcasted post content
* "Replace Site URL" checkbox appears in child post meta box to show if this was applied
*/
add_action( 'threewp_broadcast_prepare_meta_box', 'my_custom_prepare_meta_box' );
function my_custom_prepare_meta_box( $action ) {
$meta_box_data = $action->meta_box_data; // Convenience.
@swinggraphics
swinggraphics / oxygen-pro-menu-tap-sub-menu.js
Created November 2, 2020 18:38
Activate Pro Menu desktop dropdowns on tap
$('document').ready(function(){
/* Activate Pro Menu desktop dropdowns on tap */
$('.oxy-pro-menu-list .menu-item-has-children > a').on('touchstart', function(event){
var $this = $(this);
if ( $this.parents('.oxy-pro-menu-off-canvas-container, .oxy-pro-menu-open-container').length ) return;
event.preventDefault();
event.stopPropagation();
$('body').trigger('touchstart.mouseout');
$this.parent().trigger('mouseenter');
@swinggraphics
swinggraphics / avoid-duplicate-posts.php
Created December 11, 2020 16:59
Exclude posts that have been displayed already by setting `avoid_duplicates` query var
// Based on code from https://ashton.codes/avoid-showing-wordpress-post-multiple-loops/
function sg_track_displayed_posts( $permalink, $post ) {
global $sg_displayed_posts;
$sg_displayed_posts[ $post->post_type ][] = get_the_ID();
return $permalink;
}
add_filter( 'post_link', 'sg_track_displayed_posts', 10, 2 );
add_filter( 'post_type_link', 'sg_track_displayed_posts', 10, 2 );
@swinggraphics
swinggraphics / Relevanssi HTML Tag Weights
Last active February 21, 2022 15:36 — forked from msaari/functions.php
Increases the weight for specified HTML tags
<?php
function rlv_html_tag_boost( $content ) {
$weightings = [
'h1' => 5,
'h2' => 4,
'h3' => 2,
'strong' => 1,
];
foreach ( $weightings as $tag => $weight ) {
@swinggraphics
swinggraphics / default-event-recap-slug.php
Created March 26, 2022 00:05
Set CPT slug by default based on taxonomy and post_parent
/**
* Default event recap slug
* If has parent and is in category Recap, set slug to "recap".
* Can't use `wp_insert_post_data` because taxonomy data isn't included, frustratingly.
*/
// Catch changes in block editor
add_filter( 'rest_post_dispatch', 'recap_slug_rest', 10, 3 );
function recap_slug_rest( $result, $server, $request ) {
if ( 'PUT' == $request->get_method() ) {
$post_id = $request->get_param( 'id' );
@swinggraphics
swinggraphics / BB post state.php
Created June 6, 2022 18:45
Provide clear indication when Beaver Builder is active
<?php
/* Make BB status obvious */
add_filter( 'display_post_states', 'sg_bb_post_state', 10, 2 );
function sg_bb_post_state( $post_states, $post ) {
if ( FLBuilderModel::is_builder_enabled( $post->ID ) ) {
$post_states[] = 'Beaver Builder';
}
return $post_states;
}
@swinggraphics
swinggraphics / oxy-save-rendered-content.php
Last active June 15, 2022 21:56
Copy compiled Oxygen page content to post_content for search
/* Copy compiled Oxygen page content to post_content for search */
function sg_save_rendered_page_content( $meta_id, $object_id, $meta_key, $meta_value ) {
// Debugging
// $debug .= preg_replace( '#(\[/[^\]]+?\])#', "$1\n", $meta_value );
// file_put_contents( plugin_dir_path( __FILE__ ) . 'debug.html', $debug );
if ( 'ct_builder_shortcodes' != $meta_key ) return;
// Don't try to render anything with inner content
if ( false !== strpos( $meta_value, '[ct_inner_content' ) ) return;
@swinggraphics
swinggraphics / prefer-pages-over-cpt.php
Last active August 4, 2022 01:46
Prefer Pages over CPT
/* Prefer Pages over CPT */
function sg_page_before_cpt( $query ) {
$cpt_slug = 'my_cpt_slug';
$cpt_rewrite_slug = 'my_cpt_rewrite_slug';
if( isset( $query->query_vars[$cpt_slug] ) && $slug = $query->query_vars[$cpt_slug] ) {
$slug = "$cpt_rewrite_slug/$slug";
if( get_page_by_path( $slug ) ) {
@swinggraphics
swinggraphics / tec-anti-hijack.php
Created October 15, 2022 14:28
Prevent The Events Calendar from hijacking the single post template
// Let page builders like Elementor control the display of single events
function dont_let_tec_hijack( $should_hijack, $query ) {
if ( $query->is_single() ) $should_hijack = false;
return $should_hijack;
}
add_filter( 'tribe_events_views_v2_should_hijack_page_template', 'dont_let_tec_hijack', 10, 2 );