Skip to content

Instantly share code, notes, and snippets.

View morgyface's full-sized avatar

Dan morgyface

View GitHub Profile
@morgyface
morgyface / archive-page-content.php
Last active May 17, 2017 13:18
WordPress | Grab content from a page with the same name as an archive
<?php
$archivetitle = post_type_archive_title('', false); // No prefix and false to display
$thepage = get_page_by_title( $archivetitle ); // Get page object using the archive title
$content = $thepage->post_content; // Get raw page content
if ( !empty( $content ) ) {
$content = apply_filters('the_content', $content); // Apply filters to add formating
echo $content;
if ( current_user_can( 'edit_pages' ) ) {
// Let us also add an edit link for ease during development
$pageid = $thepage->ID; // Get the page ID for the edit link
@morgyface
morgyface / edit-link.php
Last active July 1, 2016 11:24
WordPress | Custom "Edit" link visible only by administrators
@morgyface
morgyface / header-excerpt-description.php
Last active January 13, 2018 12:45
WordPress | Use post/page excerpt to generate meta description for SEO
<?php
$tagline = get_bloginfo ( 'description' );
$post_object = get_post();
$excerpt = $post_object->post_excerpt; // Get the raw excerpt, warts (tags) and all.
$content = $post_object->post_content; // Get the raw content.
$char_limit = 150; // Set the character length to 150, best practice for SEO.
echo '<meta name="description" content="';
if ( !empty( $excerpt ) ) { // If there is an excerpt lets use it to generate a meta description
$excerpt_stripped = strip_tags( $excerpt ); // Remove any tags using the PHP function strip_tags.
$excerpt_length = strlen( $excerpt_stripped ); // Now lets count the characters
@morgyface
morgyface / copyright.php
Last active January 22, 2019 17:54
WordPress | Dynamic copyright with current year and site title plus tagline
@morgyface
morgyface / mobile-menu.php
Last active February 6, 2017 15:34
WordPress | Mobile Menu using Mobile_Detect
<!-- stylesheet content -->
<style type="text/css">
div#access {position:fixed; text-align:center; background-color:rgba(0, 0, 0, 0.9); height:100%; z-index:10; width:100%; color:#FFF; top:0; -webkit-transition:all 0.5s ease; -moz-transition:all 0.5s ease; transition:all 0.5s ease; opacity:1}
div#access ul {font-size:3em; list-style:none; margin:8em 0 0 0; padding:0}
div#access ul li {margin:1em 0}
div#access ul li a {display:inline-block}
div#access ul li a {color:#FFF; text-decoration:none; display:block; -webkit-transition: all 0.3s ease; -moz-transition: all 0.3s ease; transition: all 0.3s ease}
div#access a:hover {color:blue}
div#access ul li.current-menu-item a {cursor:default; border-bottom:2px solid #FFF}
div#access.right {right: -100%}
@morgyface
morgyface / functions_add-image-sizes.php
Last active June 30, 2016 10:12
WordPress | Add additional image sizes
<?php
add_action( 'after_setup_theme', 'add_image_sizes' );
function add_image_sizes() {
add_image_size( 'tiny', 80, 60, true ); // 80 pixels wide by 60 pixels tall, hard crop mode
add_image_size( 'super', 1600, 1200, true );
}
// Use them like this
if ( has_post_thumbnail() ) {
the_post_thumbnail( 'super' );
@morgyface
morgyface / cf7_contact.php
Last active June 8, 2018 01:59
WordPress | Contact Form 7 | Display any forms that match the page title
<?php
// If there is a CF7 form with the same name as the current page let us display it.
$current_page_title = get_the_title();
$cf7form = get_page_by_title( $current_page_title, OBJECT, 'wpcf7_contact_form' );
if ( $cf7form ) {
$formid = $cf7form->ID;
$formtitle = $cf7form->post_title;
echo( do_shortcode('[contact-form-7 id="' . $formid . '" title="' . $formtitle . '"]') );
}
?>
@morgyface
morgyface / cf7_custom-select.php
Last active March 27, 2024 00:10
WordPress | Contact Form 7 | Custom select from post list
<?php
// Custom contact form 7 retreat select
add_action( 'wpcf7_init', 'custom_retreat_select' );
function custom_retreat_select() {
wpcf7_add_form_tag( 'retreat_select', 'custom_retreat_handler', array( 'name-attr' => true ) );
}
function custom_retreat_handler( $tag ) {
$atts = array();
@morgyface
morgyface / list-children.php
Last active July 1, 2016 15:16
WordPress | List bottom level childless posts
<?php
$args = array(
'post_type' => 'posts',
'posts_per_page' => 99,
'orderby' => 'parent',
'order' => 'ASC'
);
$posts = get_posts( $args );
foreach ( $posts as $post ) {
$childargs = array(
@morgyface
morgyface / functions_crop-images.php
Last active January 31, 2024 10:29
WordPress | Create checkboxes on Settings > Media which crop medium and large image sizes.
// Add crop options for medium and large images to dashboard > settings > media.
function crop_settings_api_init() {
// Add the section to media settings
add_settings_section(
'crop_settings_section',
'Crop images',
'crop_settings_callback_function',
'media'
);
// Add the fields to the new section