• Home
    ->
    Code snippets



    Custom WordPress code snippets
    Put in functions.php

    Breadcrumb links on pages:

    function breadcrumb_links($content) {
        $parents = get_post_ancestors( $post->ID );
        $title2 = get_the_title($post->ID);
        $page_link2 = get_page_link($post->ID);
        $url = get_bloginfo('url');
        $title3 = get_the_title($url);
        foreach ($parents as $value) {
            $title = get_the_title($value);
            $page_link = get_page_link($value);
            $page_url = '<a href=' . $page_link .'>' . $title . '</a>';
            $item_output1 = $page_url .'<div class="sep3"> -> </div>'. $item_output1;
        }
        $page_url2 = '<a href=' . $page_link2 .'>' . $title2 . '</a>';
        $page_url3 = '<a href=' . $url .'>' . 'Home' . '</a>';
        $beforecontent = '<h2>' . $page_url3 .'<div class="sep3"> -> </div>'. $item_output1 . $title2 . '</h2>';
        $aftercontent = '';
        $fullcontent = $beforecontent .'<br>' . '<br>' . $content . $aftercontent;
        if ( is_page()  and !is_front_page() ) {
            return $fullcontent;
        } else {
            return $content;
        }
    }
    add_filter('the_content', 'breadcrumb_links');

    Gallery Index page shortcode

    Put

    shortcode in any page and it will grab all child pages with featured images and their title and create a grid gallery right in the page can be easily used to create galleries with sub galleries.
    Example: https://www.machinecode.org/short-test

    function n0x5_gallery() {
        $pages = get_pages(array('parent'  => get_the_id(), 'post_status' => array('publish', 'private')));
        $result = wp_list_pluck( $pages, 'ID' );
        foreach ($result as $thumb) {
            $wp_query2 = new WP_Query(array(
                'post_parent' => $thumb,
                'post_type' => array('attachment', 'page'),
                'post_status' => 'any'
                ));
            $count = $wp_query2->found_posts;
            $perma = get_permalink($thumb);
            $title = get_the_title($thumb);
            $thumbnail = get_the_post_thumbnail($thumb, 'thumbnail');
            $sizes = wp_get_registered_image_subsizes();
            $img_width = $sizes['thumbnail']['width'];
            if (!empty(get_the_post_thumbnail($thumb, 'thumbnail') )) {
              $thumb = '<div class="nox-item"><div class="n0x-lnk"><a href="'.$perma.'">'.$thumbnail.'</a></div><div class="n0x-title"><a href="'.$perma.'">'.$title . ' <br>(' . $count .  ' items)</a></div></div>';
                $lnks = $lnks . $thumb;
    			
            }
            else {
                $thumb = '<a href="'.$perma.'">'.$title.'</a><br>';
                $lnks = $lnks . $thumb;
            }
        }
        return '<div class="stuf">' . $lnks . '</div>';
    }
    add_shortcode('noxindex', 'n0x5_gallery');

    Image width+size in gutenberg image block

    function add_img_s1ze( $block_content = '', $block = [] ) {
      if ( isset( $block['blockName'] ) && 'core/image' === $block['blockName'] ) {
        $args2 = wp_parse_args( $block);
        $innr = $args2['attrs']['id'];
        $metadata = wp_get_attachment_metadata($innr);
        $width = $metadata['width'];
        $height = $metadata['height'];
        $lr5nfo = esc_url(wp_get_attachment_url($innr));
        $uploaded = get_the_time('', $innr);
        $date5 = get_the_date('M d, Y', $innr);
        $url3 = explode("/", $lr5nfo);
        $url4 = end($url3);
        $html2 = str_replace('</figure>', '<div class="dimensions">' . $url4 . '<br>---<br>' . $width . 'x' . $height . '</div></figure>', $block_content);
        //$html = str_replace('<a href=', '<a data-fancybox="gallery" href=', $block_content);
        return $html2;
    }
      return $block_content;
    }
    add_filter( 'render_block', 'add_img_s1ze', 10, 2 );

    Old gallery code

    Older code to put image meta data in galleries for Classic WP themes, mostly replaced with the grid gallery above.

    add_filter( 'use_default_gallery_style', '__return_false' );
    remove_shortcode('gallery', 'gallery_shortcode');
    add_shortcode('gallery', 'custom_gallery');
    function custom_gallery($attr) {
        $post = get_post();
        static $instance = 0;
        $instance++;
        $attr['columns'] = 1;
        $attr['size'] = 'full';
        $attr['link'] = 'none';
        $attr['orderby'] = 'post__in';
        $attr['include'] = $attr['ids'];
        $output = apply_filters('post_gallery', '', $attr);
        if ( $output != '' )
            return $output;
        # We're trusting author input, so let's at least make sure it looks like a valid orderby statement
        if ( isset( $attr['orderby'] ) ) {
            $attr['orderby'] = sanitize_sql_orderby( $attr['orderby'] );
            if ( !$attr['orderby'] )
            unset( $attr['orderby'] );
        }
        extract(shortcode_atts(array(
            'order'      => 'DESC',
            'orderby'    => 'menu_order ID',
            'id'         => $post->ID,
            'itemtag'    => 'div',
            'icontag'    => 'div',
            'captiontag' => 'p',
            'columns'    => 1,
            'size'       => 'medium',
            'include'    => '',
            'exclude'    => ''
        ), $attr));
        $id = intval($id);
        if ( 'RAND' == $order )
            $orderby = 'none';
        if ( !empty($include) ) {
            $_attachments = get_posts( array('include' => $include, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby) );
            $attachments = array();
            foreach ( $_attachments as $key => $val ) {
                $attachments[$val->ID] = $_attachments[$key];
            }
        } elseif ( !empty($exclude) ) {
            $attachments = get_children( array('post_parent' => $id, 'exclude' => $exclude, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby) );
        } else {
            $attachments = get_children( array('post_parent' => $id, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby) );
        }
        if ( empty($attachments) )
            return '';
        $gallery_style = $gallery_div = '';
        if ( apply_filters( 'use_default_gallery_style', true ) )
            $gallery_style = "<!-- see gallery_shortcode() in functions.php -->";
        $gallery_div = "<div class='gallery gallery-columns-1 gallery-size-full'>";
        $output = apply_filters( 'gallery_style', $gallery_style . "\n\t\t" . $gallery_div );
        foreach ( $attachments as $id => $attachment ) {
                     $lr3nfo = wp_get_attachment_metadata($id);
                     $cam = $lr3nfo[image_meta][camera];
                     $lr2nfo = "$lr3nfo[width] x $lr3nfo[height]";
                     $lr5nfo = $lr3nfo[file];
                     $lr6nfo = substr($lr5nfo, strpos($lr5nfo, "/") + 1);
                     $lr4nfo = substr($lr6nfo, strpos($lr6nfo, "/") + 1);
                     $trunc_nfo = $string = substr($lr4nfo,0,45);
                     $date5 = get_the_date('M d, Y', $attachment);
            $link = wp_get_attachment_link($id, 'medium', false, false);
            $output .= "
                             <div class='imgc'>
                    $link <div class=\"dimensions\">$lr2nfo
                    <div class=\"dlc\"> <div title=\"$lr4nfo\" class=\"filename1\">$trunc_nfo</div></div>
                         
                          <div class=\"uploaded\">Uploaded: $date5</div>
                </div></div>";
            $output .= "";
        }
        $output .= "</div>\n";
        return $output;
    }