Snippets

Code Snippet Category

JetEngine Gallery – WP All Import

function gallery_ids_in_string($post_id, $att_id, $filepath, $is_keep_existing_images = '') {
    // The custom field used by gallery.
    $key = '_gallery';

    // The separator to use between each ID.
    $separator = ",";

    // Retrieve the current values in the gallery field.
    $gallery = get_post_meta($post_id, $key, true);

    // Ensure gallery is valid.
    if (is_string($gallery) || is_empty($gallery) || ($gallery == false)) {

        // Split value into array.
        $gallery = explode($separator, $gallery);

        // Add image if it's not in the gallery.
        if (!in_array($att_id, $gallery)) {

            // Ensure array doesn't start with empty value.
            if ($gallery[0] == '') unset($gallery[0]);

            // Add image ID to array.
            $gallery[] = $att_id;

            // Save updated gallery field.
            update_post_meta($post_id, $key, implode($separator, $gallery));

        }
    }
}
add_action('pmxi_gallery_image', 'gallery_ids_in_string', 10, 4);

Keywords: import, jetengine, gallery

Blog Manager Access

/**
 * Create a custom Blog Manager role with limited permissions
 * Add this to your theme's functions.php or a custom plugin
 */
function create_blog_manager_role() {
    // Remove the role if it exists
    if (get_role('blog_manager')) {
        remove_role('blog_manager');
    }
    
    // Create new role with minimal capabilities
    $role = add_role(
        'blog_manager',
        'Blog Manager',
        array(
            'read'             => true,  // Basic reading capability
            
            // Post capabilities
            'edit_posts'       => true,  // Edit their own posts
            'edit_others_posts' => true, // Edit posts of other users
            'edit_published_posts' => true, // Edit already published posts
            'publish_posts'    => true,  // Publish posts
            'delete_posts'     => true,  // Delete their own posts
            'delete_published_posts' => true, // Delete published posts
            'delete_others_posts' => true, // Allow deleting others' posts
            
            // Category capabilities
            'manage_categories' => true, // Manage post categories
            
            // Media capabilities
            'upload_files'     => true,  // Upload media files
            
            // Explicitly deny access to pages and custom post types
            'edit_pages'       => false,
            'edit_published_pages' => false,
            'edit_private_posts' => false,
            'edit_others_pages' => false,
            'edit_published_pages' => false,
            'edit_private_pages' => false,
            'delete_pages'     => false,
            'delete_private_posts' => false,
            'delete_others_pages' => false,
            'delete_published_pages' => false,
            'delete_private_pages' => false,
        )
    );
    
    // Optional: Log if role creation was successful
    if (!is_wp_error($role)) {
        error_log('Blog Manager role created successfully.');
    } else {
        error_log('Error creating Blog Manager role: ' . $role->get_error_message());
    }
}

/**
 * Restrict access to custom post types for Blog Manager role
 */
function restrict_cpt_access_for_blog_manager() {
    $user = wp_get_current_user();
    
    // Only apply to Blog Manager role
    if (in_array('blog_manager', (array) $user->roles)) {
        global $pagenow;
        
        // Allow admin-ajax.php for post duplication
        if ($pagenow == 'admin-ajax.php') {
            return;
        }
        
        // If trying to access edit.php with a CPT
        if ($pagenow == 'edit.php' && isset($_GET['post_type']) && $_GET['post_type'] != 'post') {
            // Redirect to posts instead
            wp_redirect(admin_url('edit.php'));
            exit;
        }
        
        // If trying to edit a CPT
        if ($pagenow == 'post.php' && isset($_GET['post'])) {
            $post_type = get_post_type($_GET['post']);
            if ($post_type && $post_type != 'post') {
                wp_redirect(admin_url('edit.php'));
                exit;
            }
        }
    }
}

/**
 * Hide specific admin menu items for Blog Manager role
 */
function hide_menu_items_for_blog_manager() {
    $user = wp_get_current_user();
    
    // Only apply to Blog Manager role
    if (in_array('blog_manager', (array) $user->roles)) {
        // Hide Comments menu
        remove_menu_page('edit-comments.php');
        
        // Hide Media Mentions CPT - Fixed slug
        remove_menu_page('edit.php?post_type=media-mentions');
        
        // Hide Elementor Templates
        remove_menu_page('edit.php?post_type=elementor_library');
        
        // Hide Jet Popup CPT
        remove_menu_page('edit.php?post_type=jet-popup');
        
        // Hide WordPress Tools
        remove_menu_page('tools.php');
        
        // Hide other menu items that shouldn't be accessed
        remove_menu_page('edit.php?post_type=page');
        remove_menu_page('plugins.php');
        remove_menu_page('themes.php');
        remove_menu_page('users.php');
        remove_menu_page('options-general.php');
        
        // Hide Elementor menu if it exists
        remove_menu_page('elementor');
    }
}

/**
 * Add CSS to hide menu items that might not be properly removed by remove_menu_page
 */
function hide_menu_items_css_for_blog_manager() {
    $user = wp_get_current_user();
    
    // Only apply to Blog Manager role
    if (in_array('blog_manager', (array) $user->roles)) {
        echo '
            #adminmenu a[href="edit-comments.php"],
            #adminmenu a[href*="post_type=media-mentions"],
            #adminmenu a[href*="post_type=elementor_library"],
            #adminmenu a[href*="post_type=jet-popup"],
            #adminmenu a[href="tools.php"],
            #commentstatusdiv,
            #dashboard_right_now .comment-count,
            #latest-comments,
            #menu-comments,
            .aioseo-review-plugin-cta,
            #gutenkit-template-library,
            #elementor-switch-mode,
            #elementor-editor {
                display: none !important;
            }
        ';
    }
}

/**
 * Filter the list of posts to only show blog posts (post type 'post')
 */
function filter_posts_for_blog_manager($query) {
    if (!is_admin()) {
        return $query;
    }
    
    $user = wp_get_current_user();
    
    // Only apply to Blog Manager role
    if (in_array('blog_manager', (array) $user->roles)) {
        global $pagenow, $typenow;
        
        // When viewing the posts list
        if ($pagenow == 'edit.php' && empty($typenow)) {
            $query->set('post_type', 'post');
        }
    }
    
    return $query;
}

/**
 * Allow Blog Manager role to use post duplication feature
 */
function allow_duplicate_post_for_blog_manager() {
    $user = wp_get_current_user();
    
    // Only apply to Blog Manager role
    if (in_array('blog_manager', (array) $user->roles)) {
        // Check if this is the duplicate post action
        if (isset($_GET['action']) && $_GET['action'] == 'dt_duplicate_post_as_draft') {
            // Allow the action to proceed
            return;
        }
    }
}

/**
 * Fix map_meta_cap filter to allow blog managers to edit/publish posts
 */
function fix_blog_manager_capabilities($caps, $cap, $user_id, $args) {
    // Check if we're dealing with a blog manager
    $user = get_userdata($user_id);
    if (!$user || !in_array('blog_manager', (array) $user->roles)) {
        return $caps;
    }
    
    // Check if we're working with a post
    if (!empty($args[0])) {
        $post_id = $args[0];
        $post = get_post($post_id);
        
        if ($post && $post->post_type === 'post') {
            // For specific capabilities related to posts
            if ($cap === 'edit_post' || $cap === 'edit_others_post' || 
                $cap === 'publish_post' || $cap === 'edit_published_post') {
                
                // Set to a capability the blog manager has
                return array('edit_posts');
            }
            
            if ($cap === 'delete_post' || $cap === 'delete_others_post' ||
                $cap === 'delete_published_post') {
                
                // Set to a capability the blog manager has
                return array('delete_posts');
            }
        }
    }
    
    return $caps;
}

// Hook to create the role when the plugin is activated
add_action('init', 'create_blog_manager_role');

// Hook to restrict access to CPTs
add_action('admin_init', 'restrict_cpt_access_for_blog_manager');

// Hook to hide menu items
add_action('admin_menu', 'hide_menu_items_for_blog_manager', 999);

// Hook to add CSS to hide menu items
add_action('admin_head', 'hide_menu_items_css_for_blog_manager');

// Hook to filter posts
add_action('pre_get_posts', 'filter_posts_for_blog_manager');

// Hook to allow post duplication
add_action('admin_init', 'allow_duplicate_post_for_blog_manager');

// Fix capabilities mapping
add_filter('map_meta_cap', 'fix_blog_manager_capabilities', 10, 4);
Keywords: blog only, content, user role,

Crocoblock – Close Popup On Form Submit

jQuery(document).ready(function($) {
    // Listen for the JetFormBuilder submit success event
    $(document).on('jet-form-builder/ajax/on-success', function() {
        // Wait for 2 seconds after the form is successfully submitted
        setTimeout(function() {
            // Simulate a click on the popup close button
            $('.jet-popup__close-button').trigger('click');
        }, 2000); // Time in milliseconds (2 seconds)
    });
});
Keywords: Crocoblock, jetpopup, jetformbuilder

js close popup on menu click and scroll

jQuery( document ).ready( function( jQuery ) {
    jQuery( document ).on( 'click', '#elementor-popup-modal-28 .elementor-icon-list-item a', function( event ) {
        elementorProFrontend.modules.popup.closePopup( {}, event );
    } );

jQuery( document ).on( 'scroll', function( event ) {
        jQuery('.elementor-element-7d10a963 .elementor-widget-container .elementor-icon-wrapper a').trigger('click');
    } );
      
} );
Keywords: Popup, Elementor, Scroll

Disable Checkout Fields

// Do NOT include the opening php tag.
// Place in your theme's functions.php file

add_filter( 'cfw_get_billing_checkout_fields', 'remove_checkout_fields', 100 );

function remove_checkout_fields( $fields ) {
unset( $fields['billing_company'] );
unset( $fields['billing_postcode'] );
unset( $fields['billing_state'] );
unset( $fields['billing_address_1'] );
unset( $fields['billing_address_2'] );
return $fields;
}

// Set billing address fields to not required
add_filter( 'woocommerce_checkout_fields', 'unrequire_checkout_fields' );

function unrequire_checkout_fields( $fields ) {
$fields['billing']['billing_company']['required']   = false;
$fields['billing']['billing_city']['required']      = false;
$fields['billing']['billing_postcode']['required']  = false;
$fields['billing']['billing_country']['required']   = false;
$fields['billing']['billing_state']['required']     = false;
$fields['billing']['billing_address_1']['required'] = false;
$fields['billing']['billing_address_2']['required'] = false;
return $fields;
}
Keywords: Ecommerce, checkout, billing field

Override Outgoing Email

/**
 * Force all WordPress emails to go to a specific email address
 */
add_filter('wp_mail', 'redirect_all_emails_to_specific_address');

function redirect_all_emails_to_specific_address($args) {
    // The email address where you want to receive all emails
    $specific_email = '[email protected]';
    
    // Uncomment the line below if you want to keep a record of the original recipient
    // $args['message'] .= "nnOriginally intended for: " . implode(', ', (array)$args['to']);
    
    // Replace the recipient(s) with your specific email
    $args['to'] = $specific_email;
    
    return $args;
}
Keywords: Outgoing email, override
Login / email
Password
Remember me