Skip to content

Instantly share code, notes, and snippets.

Avatar

Sabrina Zeidan sabrina-zeidan

View GitHub Profile
@sabrina-zeidan
sabrina-zeidan / acf_sync_with_user_profile.php
Last active Nov 15, 2020
When you have ACF User fields like first name and last name and you need to keep that in sync with what is entered in WordPress user profile (works both ways)
View acf_sync_with_user_profile.php
//If both changed at the same time - custom field value will be applied as it fires later, set 30 to change it add_action( 'profile_update', array($this, 'update_acf_fields'), 30, 2 );
class Sync_ACF_with_User_Profile_Class {
static public $sync_pair = array(
array( 'acf' => 'member_name', 'profile_field' => 'first_name'), //add acf field name according to your setup
array( 'acf' => 'member_lastname', 'profile_field' => 'last_name')// add more rows to sync any other fields as well
);
public function __construct() {
add_action( 'profile_update', array($this, 'update_acf_fields'), 10, 2 ); //when profile is updated -> update ACF
add_action('updated_user_meta', array($this, 'update_user_profile_fields'),10,4); // when ACF is updated -> update profile
add_action('added_user_meta', array($this, 'update_user_profile_fields'),10,4); // when ACF is added -> update profile
@sabrina-zeidan
sabrina-zeidan / acf_get_user_fields.php
Created Nov 13, 2020
Get all ACF's assigned to user profile
View acf_get_user_fields.php
// 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 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 / delete_empty_terms.php
Last active Jul 21, 2020
Bulk delete empty taxonomy terms [Wordpress]
View delete_empty_terms.php
function delete_empty_terms(){
$taxonomy_name = 'city';
$terms = get_terms( array(
'taxonomy' => $taxonomy_name,
'hide_empty' => false
) );
foreach ( $terms as $term ) {
$term_count = $term->count;
if ($term_count < 1){ wp_delete_term($term->term_id, $taxonomy_name);
}
@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 / bulk_delete_comments.php
Last active May 27, 2020
Bulk delete all comments [Wordpress]
View bulk_delete_comments.php
function bulk_delete_comments(){
$args = array('fields' => 'ids','number' => '','status' => 'all');
$comments = get_comments( $args );
foreach ($comments as $comment) {
wp_delete_comment( $comment, true);
}
}
add_action( 'wp_head', 'bulk_delete_comments' );
You can’t perform that action at this time.