Blog

Display image with srcet from ACF image field

function acfimg( $img ) {
$img_srcset = wp_get_attachment_image_srcset( $img[‘ID’] );
$img_sizes = wp_get_attachment_image_sizes( $img[‘ID’] );
echo ‘'.$img['alt'].'‘;
}


Hide Yoast Meta Boxes

function hide_yoast_seo_meta_box_for_cpt() {
remove_meta_box( ‘wpseo_meta’, ‘testimonials’, ‘normal’ );
remove_meta_box( ‘wpseo_meta’, ‘faqs’, ‘normal’ );
remove_meta_box( ‘wpseo_meta’, ‘links’, ‘normal’ );
remove_meta_box( ‘wpseo_meta’, ‘team’, ‘normal’ );
remove_meta_box( ‘wpseo_meta’, ‘events’, ‘normal’ );
remove_meta_box( ‘wpseo_meta’, ‘volunteer_events’, ‘normal’ );
}
add_action( ‘add_meta_boxes’, ‘hide_yoast_seo_meta_box_for_cpt’, 99 );


Center image vertically in DIV

<div class="container d-flex align-items-center justify-content-center min-vh-100">
<h1>Centered Element</h1>
</div>

Remove not required menu items for Editors

function remove_menus() {
if ( !current_user_can( ‘administrator’ ) ) {
remove_menu_page(‘edit.php’);
remove_menu_page(‘edit-comments.php’);
remove_menu_page(‘tools.php’);
remove_menu_page(‘wpseo_workouts’);
remove_submenu_page(‘wpseo_workouts’, ‘wpseo_redirects’);
remove_submenu_page(‘wpseo_workouts’, ‘wpseo_workouts’);
}
}
add_action( ‘admin_menu’, ‘remove_menus’ );


Allow Editor to edit Privacy Policy Page

add_filter( ‘map_meta_cap’, ‘manage_privacy_options’, 1, 4 );
function manage_privacy_options( $caps, $cap, $user_id, $args ) {
if ( !is_user_logged_in() ) return $caps;

if ( 'manage_privacy_options' === $cap ) {
    $manage_name = is_multisite() ? 'manage_network' : 'manage_options';
    $caps = array_diff( $caps, [ $manage_name ] );
}
return $caps;

}


Bootstrap 5 Menu Drop Down and Navigate


// The drop down menu needs to allow for navigation to parent as well as drop down.
// This javascript will find all instances of .dropdown-toggle (parent menu items) and if there is
// a .show class (ie; the submenu is showing), it will change to default behaviour (href navigation).
// If no .show class, bootstrap behaviour should be activated. // Target all parent menu items with a dropdown-toggle
const dropdownToggles = document.querySelectorAll('.dropdown-toggle');
dropdownToggles.forEach(function(dropdownToggle) {
const dropdownMenu = dropdownToggle.nextElementSibling; // Get the corresponding dropdown menu
const dropdown = dropdownToggle.closest('.dropdown'); // Get the closest dropdown container // Manually track the dropdown visibility state let isDropdownVisible = false; // Attach the click event to each parent link dropdownToggle.addEventListener('click', function(e) { e.preventDefault(); // Prevent Bootstrap's default behavior of toggling the dropdown if (isDropdownVisible) { // If the dropdown is open, navigate to the link window.location.href = dropdownToggle.href; // Navigate to the href } else { // If the dropdown is not open, show it and prevent navigation const bsDropdown = new bootstrap.Dropdown(dropdownToggle); bsDropdown.show(); // Explicitly show the dropdown without navigating // Update our manually tracked state isDropdownVisible = true; } });

});


Stay logged into WordPress

    function stay_logged_in($expires) {
    return 172800; // default 48 hours
    // return YEAR_IN_SECONDS;
    }
    add_filter(‘auth_cookie_expiration’, ‘stay_logged_in’);


    WordPress Admin Stylesheet

    To include styling for Administrators:

    function admin_style() {
    wp_enqueue_style(‘admin-styles’, get_template_directory_uri().’/admin.css’);
    }
    add_action(‘admin_enqueue_scripts’, ‘admin_style’);


    WordPress Categories Template

    If WordPress category template file is not working, add the following in category.php, before the loop:

    $cat_id = get_query_var(‘cat’);
    query_posts(“post_type=&cat=$cat_id”);


    Page Navigation by Category

    $terms = get_the_terms( $post->ID, ‘mediaitem’ );
    $args = array(
    ‘in_same_term’ => true,
    ‘category’ => $term->slug,
    ‘screen_reader_text’ => ‘Post navigation’
    );
    the_post_navigation($args);