Skip to content

Instantly share code, notes, and snippets.

@samuelcotterall
samuelcotterall / Mark parent navigation active when on custom post type single page
Last active February 20, 2018 09:15 — forked from gerbenvandijk/Mark parent navigation active when on custom post type single page
Mark (highlight) custom post type parent as active item in Wordpress Navigation.When you visit a custom post type's single page, the parent menu item (the post type archive) isn't marked as active. This code solves it by comparing the slug of the current post type with the navigation items, and adds a class accordingly.
<?php
add_action('nav_menu_css_class', 'add_current_nav_class', 10, 2 );
function add_current_nav_class($classes, $item) {
// Getting the current post details
global $post;
// Getting the post type of the current post
@germanny
germanny / shortcode-include-file.php
Last active September 30, 2022 12:37
Shortcode to include files in WP Pages
<?php
// SHORTCODE - Include File
// http://www.amberpanther.com/knowledge-base/using-the-wordpress-shortcode-api-to-include-an-external-file-in-the-post-content/
function include_file($atts) {
//check the input and override the default filepath NULL
//if filepath was specified
extract(shortcode_atts(array('filepath' => 'NULL'), $atts));
//check if the filepath was specified and if the file exists
if ($filepath!='NULL' && file_exists(CHILD_SS_DIR.$filepath)){

To shut off all comments/trackbacks/pingbacks in the WordPress Admin, the steps for cleanup are:

@germanny
germanny / get_id_by_page_name.php
Last active August 29, 2015 13:57
why does this fail?
<?php
/**
* Get a Page's ID by post_name (slug)
*/
function get_id_by_page_name($post_name) {
global $wpdb;
$post_name_id = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_name = '".$post_name."'");
if ( ( $post_name_id !== NULL ) || ( $post_name_id !== '0' ) ) {
return $post_name_id;
}
@germanny
germanny / page-parent-id.php
Created March 4, 2014 20:59
get page parent in wp-admin
<?php
$post_id = $_GET['post'] ? $_GET['post'] : $_POST['post_ID'] ;
$parents = get_post_ancestors( $post_id );
$id = ($parents) ? $parents[count($parents)-1]: $post_id;
$parent = get_page( $id );
if ( get_id_by_page_name('states') == $parent->ID ) {
// do stuff
}
?>
<?php
$beforeTime = microtime(true);
function helloWorld() {
// Do stuff
}
$afterTime = microtime(true);
echo "Loading helloWorld: ".($afterTime - $beforeTime) . '<br />';
<?php
/**
* Delayed loading of typekit scripts vs Flicker-free loading
* Source: https://www.farbeyondcode.com/Delayed-loading-of-typekit-scripts-vs-Flicker-free-loading-5-2304.html
* Avg. Load Time: 66ms; 91ms; 113ms
*/
?>
<script type="text/javascript">
/* <![CDATA[ */
TypekitConfig = {
<?php
/**
* Add new classes to the $classes array
* http://codex.wordpress.org/Function_Reference/body_class#Add_Classes_By_Filters
*/
add_filter('body_class','my_class_names');
function my_class_names($classes) {
global $post;
if ( is_front_page() ) :
@halgatewood
halgatewood / pmpro-expire-at-end-of-year.php
Created November 20, 2013 16:09
Expire all membership levels at the end of the year. If the user registers after October (10 variable on line 8) then they'll expire in the next year.
/// EXPIRATION DATE
function my_pmpro_checkout_level($level)
{
$expiration_year = date('Y');
$current_month = date('n');
// IF OCT, NOV, DEC, EXPIRE NEXT YEAR
if($current_month >= 10) { $expiration_year++; }
$expiration_date = $expiration_year . "-12-31";