Skip to content

Instantly share code, notes, and snippets.

View stephanieleary's full-sized avatar

Stephanie Leary stephanieleary

View GitHub Profile
@stephanieleary
stephanieleary / search.php
Created March 3, 2016 17:03
Include number of search results in Genesis title
<?php
add_filter( 'genesis_search_title_text', 'my_search_title_text' );
function my_search_title_text( $title ) {
global $wp_query;
$start = ( ( $wp_query->paged - 1 ) * $wp_query->posts_per_page ) + 1;
$end = $wp_query->paged * $wp_query->posts_per_page;
return sprintf( '%s (%d&mdash;%d of %d)', $title, $start, $end, $wp_query->found_posts );
}
$('li.blurb').each(function(index) {
var chapters = $('dd.chapters', this).text();
var done = chapters.split('/');
if ( done[0] == 1 && done[1] == 1 ) {
$(this).css('display', 'none');
}
else {
var words = $('dd.words', this).text();
words = words.replace(',', '');
words = parseInt(words, 10);
@stephanieleary
stephanieleary / functions.php
Last active October 8, 2018 16:11
Redirect private page 404 errors to the login screen with a message
<?php
add_action( 'wp', 'my_private_page_404' );
function my_private_page_404() {
$queried_object = get_queried_object();
if ( isset( $queried_object->post_status ) && 'private' == $queried_object->post_status && !is_user_logged_in() ) {
wp_safe_redirect( add_query_arg( 'private', '1', wp_login_url( $_SERVER['REQUEST_URI'] ) ) );
exit;
}
@stephanieleary
stephanieleary / functions.php
Last active April 26, 2016 20:35
Redirect 404 errors to posts with old path stored in custom field, if it exists
<?php
add_filter( 'redirect_canonical', 'my_404_no_guessing', 10, 2 );
function my_404_no_guessing( $redirect_url, $request_url ) {
if ( is_404() ) {
// Drupal redirects
$posts = get_posts( array(
'meta_key' => 'drupal_path', // change to your meta_key
'meta_value' => parse_url( $request_url, PHP_URL_PATH ),
@stephanieleary
stephanieleary / functions.php
Last active March 14, 2023 22:50
Specify meta boxes that should be hidden by default for new users.
<?php
add_filter( 'default_hidden_meta_boxes', 'custom_default_hidden_screen_options', 10, 2 );
function custom_default_hidden_screen_options( $hidden, $screen ) {
$hide_these = array(
'dashboard_primary',
'postcustom',
'trackbacksdiv',
@stephanieleary
stephanieleary / functions.php
Created April 27, 2016 03:05
Close some meta boxes by default instead of hiding them altogether
<?php
add_action( 'admin_init', 'my_close_meta_boxes', 99 );
function my_close_meta_boxes() {
$post_types = get_post_types( array( 'public' => true ) );
foreach ( $post_types as $type ) {
// Close Excerpt on all post types
add_filter( "postbox_classes_{$type}_postexcerpt", 'my_custom_closed_meta_boxes' );
}
@stephanieleary
stephanieleary / functions.php
Created April 27, 2016 16:11
infer post type when query var is not set
<?php
function berkeley_find_post_type() {
$type = get_query_var( 'post_type' );
if ( ( !isset( $type ) || empty( $type ) ) && is_tax() ) {
// we're on a term archive, where post type might not be set but can be inferred from taxonomy's object_type
$current_term = get_queried_object();
$tax_obj = get_taxonomy( $current_term->taxonomy );
@stephanieleary
stephanieleary / functions.php
Last active October 17, 2016 14:50
add post_type arg to term links (narrow down the query)
<?php
add_filter( 'term_link', 'taxonomy_link_for_post_type', 10, 3 );
function taxonomy_link_for_post_type( $termlink, $term, $taxonomy ) {
$tax_obj = get_taxonomy( $taxonomy );
// if this is not a shared taxonomy, bail
if ( count( $tax_obj->object_type ) == 1 )
@stephanieleary
stephanieleary / functions.php
Last active June 5, 2018 20:39
Filter Genesis "no posts found" message
<?php
add_filter( 'genesis_noposts_text', 'my_custom_404_message', 10, 2 );
function my_custom_404_message( $text ) {
$queried_object = get_queried_object();
if ( isset( $queried_object->post_status ) && 'private' == $queried_object->post_status && !is_user_logged_in() )
$text = sprintf( __( 'This page is restricted. Please <a href="%s">log in or register</a>.' ), wp_login_url( $_SERVER['REQUEST_URI'] ) );
elseif ( is_search() )
@stephanieleary
stephanieleary / functions.php
Last active August 22, 2016 21:50
Title placeholders for custom post types
<?php
// Change placeholder text for post titles
add_filter( 'enter_title_here', 'my_title_placeholders' );
function my_title_placeholders( $placeholder ){
$screen = get_current_screen();
switch ( $screen->post_type ) {
case 'people':
$placeholder = __( 'Enter full name' );