Skip to content

Instantly share code, notes, and snippets.

View alexwcoleman's full-sized avatar

Alex Coleman alexwcoleman

View GitHub Profile
@alexwcoleman
alexwcoleman / menu.php
Last active November 7, 2016 23:44
Combining two WordPress menus into one
<?php
// two WordPress menus combined into one.
// first menu.
$menu = wp_nav_menu( array(
'theme_location'=> 'secondary', // or whatever location
'fallback_cb' => false,
'container' => '',
'items_wrap' => '%3$s',
@alexwcoleman
alexwcoleman / class.cpt.php
Last active September 19, 2017 19:44 — forked from jpurpleman/class.cpt.php
Custom Post Type Class
<?php
class MyCPTs {
public function __construct() {
add_action( 'init', array ( $this, 'load_cpts'), 0 );
}
/**
@alexwcoleman
alexwcoleman / remove-wordpress-text-editor.php
Created May 18, 2017 17:56
Remove the WordPress text editor on pages.
/**
* Hide editor on specific pages.
*
*/
// admin_init fires too late (I think...), so admin_head it is
add_action( 'admin_head', 'hide_text_editor' );
function hide_text_editor() {
$post_id = get_the_ID();
// some admin pages have no ID. Bugger off if there isn't an ID.
@alexwcoleman
alexwcoleman / functions.php
Created June 17, 2017 17:50
Disable WordPress Events and News Dashboard Widget
// Disable WordPress Events and News Dashboard Widget
function awc_remove() {
remove_meta_box( 'dashboard_primary', get_current_screen(), 'side' );
}
add_action( 'wp_network_dashboard_setup', 'awc_remove', 20 );
add_action( 'wp_user_dashboard_setup', 'awc_remove', 20 );
add_action( 'wp_dashboard_setup', 'awc_remove', 20 );
@alexwcoleman
alexwcoleman / the-events-calendar-filter.php
Last active September 9, 2017 16:25
Using Modern Tribe's "The Event Calendar". Just a simple <div> wrapper for a span. Nothing more, nothing less.
function wrap_event_spans($span){
// The original spit out a <span> with the Month/Year at the top of each month, but...
// there also seemed to be empty spans after each event
// This screwed up the Foundation XY Grid I wanted to use in List View
// So this is 2 birds one stone. Wrap the span, get rid of empty ones.
if( !$span ){
return;
}
$op = '<div class="whatever classes are needed">' . $span . '</div>';
return $op;
@alexwcoleman
alexwcoleman / functions.php
Created September 9, 2017 18:07
WordPress: Redirect single custom post type to archive page.
// redirect single posts to the archive page, scrolled to the current member.
// * this is for a post type of "board_member"
add_action( 'template_redirect', function() {
if ( is_singular('board_member') ) {
// I used the two variables to put the member's name as the ID of the article
// so the redirect would automatically scroll. This can be removed.
$title = get_the_title();
$url_append = str_replace( ' ', '-', strtolower($title) );
global $post;
$redirectLink = get_post_type_archive_link( 'board_member' ) . "#" . $url_append;
@alexwcoleman
alexwcoleman / functions.php
Created September 12, 2017 18:36
Character Counter in Custom Post Type excerpts
function CPT_excerpt_counter(){
// make sure to change all occurrences of the counter limit.
if ('YOUR_POST_TYPE' == get_post_type()) {
echo '<script>jQuery(document).ready(function(){
jQuery("#postexcerpt .handlediv").after("<div style=\"position:absolute;top:12px;right:34px;color:#666;\"><small>Excerpt length: </small><span id=\"excerpt_counter\"></span><span style=\"font-weight:bold; padding-left:7px;\">/ 100</span><small><span style=\"font-weight:bold; padding-left:7px;\">character(s).</span></small></div>");
jQuery("span#excerpt_counter").text(jQuery("#excerpt").val().length);
jQuery("#excerpt").keyup( function() {
if(jQuery(this).val().length > 100){
jQuery(this).val(jQuery(this).val().substr(0, 100));
@alexwcoleman
alexwcoleman / functions.php
Created September 14, 2017 21:02
Remove WooCommerce Roles (or any other WordPress roles)
// this runs on init. Just add or remove whatever user role you want to delete.
function awc_remove_role() {
remove_role( 'customer' );
remove_role( 'shop_manager' );
}
add_action( 'init', 'awc_remove_role' );
@alexwcoleman
alexwcoleman / functions.php
Created September 19, 2017 18:38
Add description to Featured Image Metabox in WordPress
// HTML is whatever you like
function awc_post_thumbnail_add_description( $content, $post_id ){
$post = get_post( $post_id );
$post_type = $post->post_type;
if ( $post_type = 'post') {
$content .= "<div style='background-color: #eee;padding: 1rem;'>
<h3><label for=\"html\"><strong>Best size: <br>900px wide by 600px tall</strong></label></h3>
</div>";
return $content;
return $post_id;
@alexwcoleman
alexwcoleman / logo-in-menu.php
Created March 9, 2018 15:48
Add A Logo/Site Title/Whatever to WordPress Navigation
class logo_in_nav_Walker extends Walker_Nav_Menu {
// from https://wordpress.stackexchange.com/questions/256868/walker-nav-menu-put-custom-code-between-a-particular-li
public $site_title;
public $site_url;
function __construct() {
$this->menu_location = 'primary';
$this->site_title = get_bloginfo( 'name' );
$this->site_url = get_bloginfo( 'url' );
}