Skip to content

Instantly share code, notes, and snippets.

Avatar

Sabrina Zeidan sabrina-zeidan

View GitHub Profile
@sabrina-zeidan
sabrina-zeidan / acf_in_shortcode.php
Created Oct 15, 2020
ACF form inside a shortcode (buffering) otherwise broken XML sitemaps, headers already sent etc
View acf_in_shortcode.php
add_shortcode( 'acf_registration', 'acf_registration' );
function acf_registration() {
$args = array(); //arguments here
ob_start();
acf_form( $args );
$registration_form = ob_get_contents();
ob_end_clean();
return $registration_form;
@sabrina-zeidan
sabrina-zeidan / hide-child-if-parent-has-another.js
Created Sep 18, 2020
Hide child if parent has (not) another child with class [JS]
View hide-child-if-parent-has-another.js
//<div class="elementor-column"><div class="dce-acf-repater-"><div class="repeater-item"></div></div><div class="elementor-widget-button"></div></div>
(function( $ ){
$(".elementor-column .dce-acf-repater-").each(function() {
if (!$(this).has(".repeater-item").length) {
hide = $(this).closest('.elementor-column').find(".elementor-widget-button").hide();
}
});
})(jQuery);
@sabrina-zeidan
sabrina-zeidan / filter_elementor_query_by_acf.php
Created Sep 18, 2020
Filter Elementor query by ACF [WordPress]
View filter_elementor_query_by_acf.php
//custom-query-name is set in Elementor as query ID
add_action( 'elementor/query/custom-query-name', function( $query ) {
$meta_query = (array)$query->get('meta_query');
$meta_query[] = array(
'key' => 'custom_field',
'value' => '',
'compare' => '!=',
);
$query->set('meta_query',$meta_query);
} );
@sabrina-zeidan
sabrina-zeidan / wp_query_wrapper.php
Created Sep 6, 2020
Html wrapper for WP_Query (insert text before or after the loop)
View wp_query_wrapper.php
<?php
//Handy when CPT previews are used in different templates across the site
add_action( 'loop_start', 'recipes_loop_start' );
function recipes_loop_start( $query ){
if ( !$query->is_main_query() && $query->get('post_type')== 'recipe' && !is_admin()) echo "<ul class='recipes-preview'>";
}
add_action( 'loop_end', 'recipes_loop_end' );
function recipes_loop_end( $query ){
if ( !$query->is_main_query() && $query->get('post_type')== 'recipe' && !is_admin()) echo "</ul>";
@sabrina-zeidan
sabrina-zeidan / get_slug.php
Last active Jul 20, 2020
Get post/term slug from any type of URL
View get_slug.php
<?php
//Examples of input
$input = "http://www.example.com/cat/cat-child/slug/";
$input = "http://www.example.com/cat/slug";
$input = "http://www.example.com/cat/slug/";
$input = "http://www.example.com/cat/slug/?dsss&efsdc=dc";
//This line will always return "slug"
$slug = basename(strtok(str_replace(' ', '', htmlspecialchars($input )), '?')).PHP_EOL;
echo $slug;
//Useful for further use with get_term_by('slug', $slug, $tax);
@sabrina-zeidan
sabrina-zeidan / gists_in_gutenberg.php
Created Jul 6, 2020
Converts links to https://gist.github.com in Gutenberg into embeds
View gists_in_gutenberg.php
//Gist embeds in Gutenberg, quick fix
//Works if you add https://gist.github.com links as regular links
add_filter( 'the_content', 'gist_in_gutenberg' );
function gist_in_gutenberg( $content ) {
$doc = new DOMDocument();
$doc->loadHTML($content);
foreach ($doc->getElementsByTagName('a') as $link){
$href = $link->getAttribute('href');
if (strpos($href, 'gist.github.com') !== false) {
$element = $doc->createElement('script');
@sabrina-zeidan
sabrina-zeidan / outfunnel.com.conf
Created May 3, 2020
Outfunnel.com nginx settings
View outfunnel.com.conf
server {
listen 80;
listen [::]:80;
server_name outfunnel.com www.outfunnel.com;
root /var/www/outfunnel.com-production;
index index.php;
@sabrina-zeidan
sabrina-zeidan / wordpress_multisite_unfiltered_html.php
Created Dec 7, 2018
Let users apart from Superadmin insert raw code in text editor [WordPress Multisite]
View wordpress_multisite_unfiltered_html.php
//Let any user who can edit page insert unfiltered html in text editor
add_action( 'init', 'reply_unfiltered_html' );
function reply_unfiltered_html() {
$user = wp_get_current_user();
if ( current_user_can('edit_pages') )
kses_remove_filters();
}
//Let users with certain roles insert unfiltered html in text editor
add_action( 'init', 'reply_unfiltered_html_for_roles' );
function reply_unfiltered_html_for_roles() {
@sabrina-zeidan
sabrina-zeidan / wordpress_multisite_compatible_site_options_actions.php
Created Oct 16, 2018
To use one single function for stand-alone WordPress install and Multisite (in case options are network wide). Get/Update/Delete options with the same function with no need to check whether it's Multisite every time
View wordpress_multisite_compatible_site_options_actions.php
//Check if plugin is Network activated
if (!function_exists('is_plugin_active_for_network')) require_once( ABSPATH . '/wp-admin/includes/plugin.php');
$plugins = get_plugins();
foreach( $plugins as $plugin_file => $plugin_info ) {
if ( $plugin_info['TextDomain'] === $this->plugin_name ){
$plugin = $plugin_file;
break;
}
}
//And set constant
@sabrina-zeidan
sabrina-zeidan / wordpress_multisite_global_menu_anywhere.php
Created Sep 10, 2018
Get shared menu from the main site displayed anywhere in the WordPress Multisite network
View wordpress_multisite_global_menu_anywhere.php
//Get $blog_id to remember where we currently are
global $blog_id;
$current_blog_id = $blog_id;
//switch to the main site which has id of 1
switch_to_blog(1);
//Check if menu exists (regardless location)
if (wp_nav_menu(array('menu' => 'social-links'))):
//output the WordPress navigation menu, use slug/name/id
wp_nav_menu( array( 'menu' => 'social-links') );
endif;
You can’t perform that action at this time.