Complete website in Rs. 5,000 with Free Hosting & Domain. Offer ends in  00:00:00
Back to Blog

WordPress Code Snippets

Apr 3, 2022 Updated: Apr 3, 2022

Articles

  1. https://wordpress.stackexchange.com/questions/189985/how-to-properly-dequeue-scripts-and-styles-in-child-theme

Custom 2 Level Menu

$menu_locations = get_nav_menu_locations();
if ( $navbar_items = wp_get_nav_menu_items($menu_locations['primary']) ):
    $child_items  = array();
    // pull all child menu items into separate object
    foreach ($navbar_items as $key => $item) {
        if ($item->menu_item_parent) {
            array_push($child_items, $item);
            unset($navbar_items[$key]);
        }
    }
    // push child items into their parent item in the original object
    foreach ($navbar_items as $item) {
        foreach ($child_items as $key => $child) {
            if ($child->menu_item_parent == $item->ID) {
                if (!$item->child_items) {
                    $item->child_items = [];
                }
                array_push($item->child_items, $child);
                unset($child_items[$key]);
            }
        }
    }
    <ul class="navbar-nav ms-auto">
    <?php
    foreach ( $navbar_items as $menu_item ):
        $current = ( $menu_item->object_id == get_queried_object_id() ) ? 'active' : '';
        if($menu_item->child_items){
            echo '<li class="nav-item dropdown"><a href="' . $menu_item->url . '" class="nav-link dropdown-toggle ' . $current . '" role="button" data-bs-toggle="dropdown" aria-expanded="false">' . $menu_item->title . '</a>';
                echo '<ul class="dropdown-menu" aria-labelledby="navbarDropdown">';
                foreach($menu_item->child_items as $item){
                    $currentChild = ( $item->object_id == get_queried_object_id() ) ? 'active' : '';
                    echo '<li><a class="dropdown-item ' . $currentChild . '" href="' . $item->url . '">' . $item->title . '</a></li>';
                }
                echo '</ul>';
            echo '</li>';
        } else {
            echo '<li class="nav-item"><a href="' . $menu_item->url . '" class="nav-link ' . $current . '">' . $menu_item->title . '</a></li>';
        }
    endforeach;
    ?>
    </ul>
<?php endif; ?>

PHP Sessions

add_action('init', function(){
    if (!session_id()) { session_start(); }
});

Convert a WordPress file URL to it’s Absolute path

/**
 * Get the wordpress file absolute path from its url
 * @param string $url the file url to get its absolute path
 * @return bool|string It returns the absolute path of file
 **/
function file_url_to_path( $url ) {
  $parsed_url = parse_url( $url );
  if( empty($parsed_url['path']) ) return false;
  $file = ABSPATH . ltrim( $parsed_url['path'], '/');
  if ( file_exists( $file) ) return $file;
  return false;
}
add_filter('admin_footer_text', function(){
    $my_theme = wp_get_theme(); ?>
    <span id="footer-thankyou"><?php esc_html_e('Crafted by', 'theme-prefix'); ?> <a href="<?php echo esc_url($my_theme->get('AuthorURI')); ?>" title="<?php echo esc_attr($my_theme->get('Description')); ?>" target="_blank" rel="noopener noreferrer"><?php echo esc_html($my_theme->get('Author')); ?></a></span>
<?php });

Escape functions

echo __('Hello', 'domain'); // Return the translated string
_e('Hello', 'domain'); // echo the translated string
echo esc_html__('Hello', 'domain'); // Escapes & return the translation string use in HTML output
esc_html_e('Hello', 'domain'); // Escapes & echo the translation string use in HTML output
echo esc_attr__('Hello', 'domain'); // Escapes & return the translation string use in an attribute
esc_attr_e('Hello', 'domain'); // Escapes & echo the translation string use in an attribute
_n(); // Retrieve the plural or single form based on the amount

Excerpt Filter

function lite_excerpt_more( $more ) {
    return '...';
}
add_filter( 'excerpt_more', 'lite_excerpt_more' );

Excerpt Lenght

function lite_excerpt_length( $length ) {
    return 35;
}
add_filter( 'excerpt_length', 'lite_excerpt_length' );
// Function to change email address
function wpb_sender_email( $original_email_address ) {
    return 'tim.smith@example.com';
}
 
// Function to change sender name
function wpb_sender_name( $original_email_from ) {
    return 'Tim Smith';
}
 
// Hooking up our functions to WordPress filters 
add_filter( 'wp_mail_from', 'wpb_sender_email' );
add_filter( 'wp_mail_from_name', 'wpb_sender_name' );

Disable Auto Update Plugin and Theme Emails

//Disable plugin auto-update email notification
add_filter('auto_plugin_update_send_email', '__return_false');
 
//Disable theme auto-update email notification
add_filter('auto_theme_update_send_email', '__return_false');

Disable Auto Update Core Emails

//Disable automatic "Your Site Has Been Updated..." emails
add_filter( 'auto_core_update_send_email', 'smartwp_disable_core_update_emails', 10, 4 );
function smartwp_disable_core_update_emails( $send, $type, $core_update, $result ) {
  if ( !empty($type) && $type == 'success' ) {
    return false;
  }
  
  return true;
}

ACF Plugin

// ACF Option page
if( function_exists('acf_add_options_page') ){
    acf_add_options_page();
}
// Repeater Field
if( have_rows('repeater_field_name') ):
    while( have_rows('repeater_field_name') ) : the_row();

        echo get_row_index(); // index (starts with 1)
        $sub_value = get_sub_field('sub_field');

    endwhile;
else :
    echo 'Empty rows.';
endif;
Contact

Got A Question For Us?

Feel free to ask anything directly on call or fill the form and we will contact back within few hours.