Skip to content

Instantly share code, notes, and snippets.

@stormwarning
Last active February 29, 2024 03:03
Show Gist options
  • Save stormwarning/8099753 to your computer and use it in GitHub Desktop.
Save stormwarning/8099753 to your computer and use it in GitHub Desktop.
WordPress PROTIPs
/**
* A selection of handy code snippets, plugins, and best practices for WordPress development.
*
*/
<?php
/**
* Strip `width` & `height` attributes from `img` elements.
*
* @file functions.php
*/
function remove_width_and_height_attribute( $html ) {
return preg_replace( '/(height|width)="\d*"\s/', "", $html );
}
add_filter( 'get_image_tag', 'remove_width_and_height_attribute', 10 );
add_filter( 'post_thumbnail_html', 'remove_width_and_height_attribute', 10 );
add_filter( 'image_send_to_editor', 'remove_width_and_height_attribute', 10 );
/**
* Prevent file editing via the dashboard editor.
*
* @file wp-config.php
*/
define( 'DISALLOW_FILE_EDIT', true);
?>
<?php
/**
* Display post time in relation to current time.
*
* @file page.php
*/
echo 'Posted ' . human_time_diff( get_the_time('U'), current_time('timestamp') ) . ' ago.';
/**
* Display estimated reading time, ala Medium, et al.
*
* Assumes a reading speed of 200 words per minute.
*
* @todo make this more WordPress-friendly.
*/
function read_time($text){
$words = str_word_count(strip_tags($text));
$min = floor($words / 200);
if($min === 0){
return 'min read';
}
return $min . 'min read';
}
?>
<?php
/**
* Remove unnecessary self-closing tags
*
* @file functions.php
*/
function remove_self_closing_tags($input) {
return str_replace(' />', '>', $input);
}
add_filter('get_avatar', 'remove_self_closing_tags'); // <img />
add_filter('comment_id_fields', 'remove_self_closing_tags'); // <input />
add_filter('post_thumbnail_html', 'remove_self_closing_tags'); // <img />
?>
<?php
/**
* Adjust post revisioning and autosaving.
*
* @file wp-config.php
*/
// Adjust autosave frequency, in seconds
define('AUTOSAVE_INTERVAL', 300 );
// Limit number of revisions
define('WP_POST_REVISIONS', 3);
// Or disable revisions completely
//define('WP_POST_REVISIONS', false );
?>
<?php
/**
* Display only posts which haven't "expired".
*
* @file page.php
*/
if (have_posts()) :
while (have_posts()) : the_post();
$expirationtime = get_post_custom_values('expiration'); // Get date value from custom field
if (is_array($expirationtime)) {
$expirestring = implode($expirationtime);
}
$secondsbetween = strtotime($expirestring)-time();
if ( $secondsbetween > 0 ) {
// Post content is displayed here
}
endwhile;
endif;
?>
<?php
/**
* Display estimated reading time, ala Medium, et al.
*
* Assumes a reading speed of 200 words per minute.
*
* @todo make this more WordPress-friendly.
*/
function read_time($text){
$words = str_word_count(strip_tags($text));
$min = floor($words / 200);
if($min === 0){
return 'min read';
}
return $min . 'min read';
}
?>
<?php
/**
* Display post time in relation to current time.
*
* @file page.php
*/
echo 'Posted ' . human_time_diff( get_the_time('U'), current_time('timestamp') ) . ' ago.';
?>
<?php
/**
* Create a Dashboard page with theme-specific settings controls.
*
* @file functions.php
*/
add_action( 'admin_menu', 'my_admin_menu' );
function my_admin_menu() {
add_theme_page( 'My Theme Options', 'My Theme Options', 'edit_theme_options', 'my-theme-options', 'my_theme_options' );
}
function my_theme_options() {
?>
<h2>Theme Options</h2>
<form method="post" action="options.php">
<?php wp_nonce_field( 'update-options' ); ?>
<?php settings_fields( 'my-options' ); ?>
<?php do_settings_sections( 'my-options' ); ?>
<?php submit_button(); ?>
</form>
<?php
}
add_action( 'admin_init', 'my_register_admin_settings' );
function my_register_admin_settings() {
register_setting( 'my-options', 'my-options' );
// Settings fields and sections
add_settings_section( 'section_typography', 'Typography Options', 'my_section_typography', 'my-options' );
add_settings_field( 'primary-font', 'Primary Font', 'my_field_primary_font', 'my-options', 'section_typography' );
}
function my_section_typography() {
echo 'Section description can go here.';
}
function my_field_primary_font() {
echo 'Font control will go here.';
}
?>
<?php
/**
* Custom login screen logo image
*
* Image should be 80px square, or overwrite the default height/width properties.
*
* @file functions.php
*/
function custom_login_logo() {
echo '<style type="text/css">
.login h1 a {
background-image: url(' . get_bloginfo('template_directory') . '/images/logo.png) !important;
background-image: none, url(' . get_bloginfo('template_directory') . '/images/logo.svg) !important;
}
</style>';
}
add_action('login_head', 'custom_login_logo');
?>
<?php
/**
* Allow SVG files in the media uploader
*
* @file functions.php
*/
function add_mime_types( $mimes ){
$mimes['svg'] = 'image/svg+xml';
return $mimes;
}
add_filter( 'upload_mimes', 'add_mime_types' );
?>
@link http://www.paulund.co.uk/add-facebook-open-graph-tags-to-wordpress
<?php
/**
* Adjust JPG compression quality.
*
* @file functions.php
*/
add_filter( 'jpeg_quality', function($arg) { return 100; } );
add_filter( 'wp_editor_set_quality', function($arg) { return 100; } );
?>
<?php
/**
* Add contact notice to Core Update notice
* First, remove the default notice.
* Next, replicate the original function, with our updated notice text.
*
* @todo Find a way to append to the original notice instead of duplicating
*/
add_action( 'admin_init', 'remove_default_update_nag' );
function remove_default_update_nag() {
remove_action( 'admin_notices', 'update_nag', 3 );
remove_action( 'network_admin_notices', 'update_nag', 3 );
}
add_action( 'admin_notices', 'custom_update_nag', 3 );
add_action( 'network_admin_notices', 'custom_update_nag', 3 );
function custom_update_nag();
if ( is_multisite() && !current_user_can('update_core') )
return false;
global $pagenow;
if ( 'update-core.php' == $pagenow )
return;
$cur = get_preferred_from_update_core();
if ( ! isset( $cur->response ) || $cur->response != 'upgrade' )
return false;
if ( current_user_can('update_core') ) {
$msg = sprintf( __('<a href="http://codex.wordpress.org/Version_%1$s">WordPress %1$s</a> is available! <a href="%2$s">Please update now</a>.<br>Reach out to your friends at <a href="http://overhaulmedia.com/" target="_blank">Overhaul Media</a> for assistance!'), $cur->current, network_admin_url( 'update-core.php' ) );
} else {
$msg = sprintf( __('<a href="http://codex.wordpress.org/Version_%1$s">WordPress %1$s</a> is available! Please notify the site administrator.<br>Reach out to your friends at <a href="http://overhaulmedia.com/" target="_blank">Overhaul Media</a> for assistance!'), $cur->current );
}
echo '<div class="update-nag">' . $msg . '</div>';
}
?>
<?php
/**
* Display gallery of attached images
*
* @file page.php
*/
$args = array(
'numberposts' => -1, // Using -1 loads all posts
'orderby' => 'menu_order', // This ensures images are in the order set in the page media manager
'order' => 'ASC',
'post_mime_type' => 'image', // Make sure it doesn't pull other resources, like videos
'post_parent' => $post->ID, // Important part - ensures the associated images are loaded
'post_status' => null,
'post_type' => 'attachment',
);
$images = get_children( $args );
if ( $images ) {
foreach ( $images as $image ) {
$sized_image = wp_get_attachment_image_src( $image->ID, 'medium' );
echo $sized_image[0];
echo $image->guid;
}
}
?>
<?php
/**
* Display featured image thumbnail in post admin columns
*
* @file functions.php
*/
// Setup Admin Thumbnail Size
if ( function_exists( 'add_theme_support' ) ) {
add_image_size( 'admin-thumb', 100, 999999 ); // 100 pixels wide (and unlimited height)
}
// Thumbnails to Admin Post View
add_filter( 'manage_posts_columns', 'posts_columns', 5 );
add_action( 'manage_posts_custom_column', 'posts_custom_columns', 5, 2 );
function posts_columns($defaults){
$defaults['post_thumb'] = __( 'Thumbnail' );
return $defaults;
}
function posts_custom_columns($column_name, $id){
if ( $column_name === 'post_thumb' ) {
the_post_thumbnail( 'admin-thumb' );
}
}
?>
<?php
/**
* Automagically set a Featured Image for a post using the first image in the content.
*
* @file functions.php
*/
function autoset_featured() {
global $post;
$already_has_thumb = has_post_thumbnail($post->ID);
if( ! $already_has_thumb ) {
$attached_image = get_children( "post_parent=$post->ID&post_type=attachment&post_mime_type=image&numberposts=1" );
if( $attached_image ) {
foreach ($attached_image as $attachment_id => $attachment) {
set_post_thumbnail($post->ID, $attachment_id);
}
}
}
}
add_action('the_post', 'autoset_featured');
add_action('save_post', 'autoset_featured');
add_action('draft_to_publish', 'autoset_featured');
add_action('new_to_publish', 'autoset_featured');
add_action('pending_to_publish', 'autoset_featured');
add_action('future_to_publish', 'autoset_featured');
?>
<?php
/**
* Hide the admin bar for users that can't edit posts.
*
* @file functions.php
*/
add_action('set_current_user', 'hide_the_adminbar');
function hide_the_adminbar() {
if ( ! current_user_can('edit_posts') ) {
show_admin_bar(false);
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment