Skip to content

Instantly share code, notes, and snippets.

View sabrina-zeidan's full-sized avatar

Sabrina Zeidan sabrina-zeidan

View GitHub Profile
@sabrina-zeidan
sabrina-zeidan / acf_get_user_fields.php
Created November 13, 2020 20:54
Get all ACF's assigned to user profile
// get all field groups that have location User Add/Edit Form
$field_groups = acf_get_field_groups(array( 'user_form' => 'edit'));
$all_fields = array();
foreach ($field_groups as $field_group){
$fields = acf_get_fields($field_group['key']);
$all_fields = array_merge($all_fields, $fields); //get all user's fields together
}
@sabrina-zeidan
sabrina-zeidan / acf_in_shortcode.php
Created October 15, 2020 16:11
ACF form inside a shortcode (buffering) otherwise broken XML sitemaps, headers already sent etc
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 September 18, 2020 08:31
Hide child if parent has (not) another child with class [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 September 18, 2020 08:27
Filter Elementor query by ACF [WordPress]
//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 September 6, 2020 19:41
Html wrapper for WP_Query (insert text before or after the loop)
<?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 July 20, 2020 12:36
Get post/term slug from any type of URL
<?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 July 6, 2020 12:37
Converts links to https://gist.github.com in Gutenberg into embeds
//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 11:33
Outfunnel.com nginx settings
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 December 7, 2018 17:25
Let users apart from Superadmin insert raw code in text editor [WordPress Multisite]
//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 October 16, 2018 11:01
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
//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