Code Snippet Library


When I first got going in WordPress, I use to save all the cool little code snippets that I came up with as well as those that I used on a regular basis. Most of these are old and even really simple, but I figured what the heck, I will share them anyway.  These are mostly PHP, but I am working to put in some cool JS snippets real soon.  I had a lot of help getting started in WordPress and did a lot of online tutorials as well as reading a lot of Stack Overflow forums and I figured that if I can help even one person with any of these snippets, then I would.  That being said, feel free to contact me if you have a question about any of them.  I am always happy to help – time permitting of course.

Remove Parent Theme Functions

Remove Parent Theme Functions - this comes in handy when working in a premium theme that you might want to update with new versions, but you want to make changes to it in a child theme.

Replace parent function

replace parent function with a function in a child theme. Overriding a parent function with a child function.

Remove unwanted Scripts and css

For when your parent theme contains scripts or css that you don't want or need...

Aqua Resizer

Resize images using aqua resizer. Aqua Resizer is a great little tool for when several images on a page must all be the same size. In a perfect world, we would just crop and resize in something like Photoshop, but the reality for a web developer is that not all of our clients have a photo editing tool and or have the skillset

query category

Pull category for each unique post using the ID and replace spaces and special characters. This comes in handy when using isotope.js to filter projects or portfolio items by category. https://isotope.metafizzy.co/. Note, if a category is more than one word or has a special character such as an '&' then see the bottom example.

Enqueue Parent Styles to Child Theme

making sure that the parent css still gets loaded into the child theme

Child Overwrite scripts and styles

add_action( 'wp_print_scripts', 'child_overwrite_scripts', 100 ); add_action( 'wp_print_styles', 'child_overwrite_styles', 100 ); function child_overwrite_scripts() { //wp_deregister_script( 'sydney-scripts' ); //wp_deregister_script( 'sydney-main' ); wp_deregister_script( 'comment-reply' ); } function child_overwrite_styles() { wp_deregister_style( 'sydney-font-awesome' ); wp_deregister_style( 'sydney-bootstrap' ); }

Remove Default Widgets

remove the default out of the box widgets that most of my clients never ever ever use

CMB2 repeatable fields groups

Using CMB2 to add repeating fields to a page or post - CMB2 has become one of my favorite plugins (even though it isn't officially known as a plugin). Using a repeatable field snippet from CMB2 is great for when you don't want to put a minimum or a maximum number of entry options for images, custom fields... I often use this in tandem with an Owl Carousel where I want my client to be able to add unlimited Testimonials, specials, gallery images, slider images...

foreach query for repeating fields group

get post meta from post_id or cpt_id and name of repeating field group. do a for each loop to pull meta data...

Querying a CPT

querying a custom post type - in this example we are querying the 'projects' post type

Displaying a CPT query

retrieving and displaying a queried cpt while loop

Add Post Thumbnail theme support to your CPTs

Add Post Thumbnail theme support to your CPTs

Complete Query with Output and endwhile

Complete project-page.php with get_header, while loop, output, and endwhile...

WP Nav Walker…

/*class My_Walker_Nav_Menu extends Walker_Nav_Menu { function start_lvl( &$output, $depth = 0, $args = array()) { $indent = str_repeat("\t", $depth); $output .= "\n$indent<ul class=\"megamenu full-width\">\n"; } } $menuSettings = array( 'container' => 'div', 'menu_class' => 'jetmenu blue', //change this to whatever classes you need for your menu 'echo' => true, 'fallback_cb' => 'wp_page_menu', 'items_wrap' => '<ul id="%1$s" class="%2$s list">%3$s</ul>', 'depth' => 3, //the number of submenus + 1 'walker' => new My_Walker_Nav_Menu() ); wp_nav_menu( $menuSettings ); //This script displays the menu. Please put it where you want it to display the menu.*/

modify nav menu args

//Add jetmenu blue class to the megamenu plugin function modify_nav_menu_args( $args ) { if( 'primary' == $args['theme_location'] ) { $args['menu_class'] = 'jetmenu blue'; } return $args; } add_filter( 'wp_nav_menu_args', 'modify_nav_menu_args', 20000 );

use-child-theme

facetwp use child theme

Set Default Images Link to NONE

set default image link location to 'None'

Always Show Kitchen Sink in WYSIWYG Editor

Always Show Kitchen Sink in WYSIWYG Editor

function to add an admin account

function to add an admin account via ftp and the functions.php file

posts carousel

<div id="blog" class="dark-version dark-section section-block"> <div class="container"> <div class="row"> <div class="mid-space"></div> <div class="col-sm-5 col-lg-8 col-lg-push-2 section-title-wrapper" style="padding:15px 60px; text-align: center;"> <h2 class="section-title-wrapper">Latest From the Blog</h2> </div> <div class="mid-space"></div> </div> <div class="row"> <!-- Latest Post 1 --> <div class="owl-carousel"> <?php $loop = new WP_Query(array('post_type' => 'post', 'posts_per_page' => -1, 'orderby'=> 'ASC')); ?> <?php while ( $loop->have_posts() ) : $loop->the_post(); ?> <div class="grid post-entry" style="width:95%;"> <?php $postTitle = get_the_title(); $post_image = the_post_thumbnail(); $source_image = $post_image; // let's assume this image has the size 100x100px $width = 500; // note, how this exceeds the original image size $height = 400; // some pixel