Our most frequently used code snippets
WORDPRESS SPECIFIC CODE SNIPPETS
Force new tab to open for all external links
function add_target_blank_to_external_links() {
?>
<script type="text/javascript">
document.addEventListener("DOMContentLoaded", function() {
var links = document.querySelectorAll('a[href^="http"]:not([href*="' + location.hostname + '"])');
links.forEach(function(link) {
link.setAttribute('target', '_blank');
link.setAttribute('rel', 'noopener noreferrer');
});
});
</script>
<?php
}
add_action('wp_footer', 'add_target_blank_to_external_links');
Restore Classic Widgets
add_filter( 'gutenberg_use_widgets_block_editor', '__return_false' ); add_filter( 'use_widgets_block_editor', '__return_false' );
Disable Comments
function __disable_feature($data) { return false; }
add_filter('comments_number', '__disable_feature');
add_filter('comments_open', '__disable_feature');
Disable Gutenberg
add_filter('use_block_editor_for_post', '__return_false', 10);
Disable XML-RPC
add_filter('xmlrpc_enabled', '__return_false');
Media Library File Size Column
add_filter( 'manage_media_columns', 'sk_media_columns_filesize' );
/**
* Filter the Media list table columns to add a File Size column.
*
* @param array $posts_columns Existing array of columns displayed in the Media list table.
* @return array Amended array of columns to be displayed in the Media list table.
*/
function sk_media_columns_filesize( $posts_columns ) {
$posts_columns['filesize'] = __( 'File size', 'my-theme-text-domain' );
return $posts_columns;
}
add_action( 'manage_media_custom_column', 'sk_media_custom_column_filesize', 10, 2 );
/**
* Display File Size custom column in the Media list table.
*
* @param string $column_name Name of the custom column.
* @param int $post_id Current Attachment ID.
*/
function sk_media_custom_column_filesize( $column_name, $post_id ) {
if ( 'filesize' !== $column_name ) {
return;
}
$bytes = filesize( get_attached_file( $post_id ) );
echo size_format( $bytes, 2 );
}
add_action( 'admin_print_styles-upload.php', 'sk_filesize_column_filesize' );
/**
* Adjust File Size column on Media Library page in WP admin
*/
function sk_filesize_column_filesize() {
echo
'<style>
.fixed .column-filesize {
width: 10%;
}
</style>';
}
// Make the filesize column sortable
add_filter( 'manage_upload_sortable_columns', 'sk_filesize_column_register_sortable' );
function sk_filesize_column_register_sortable( $columns ) {
$columns['filesize'] = 'filesize';
return $columns;
}
// Handle the sorting logic
add_action( 'pre_get_posts', 'sk_filesize_column_orderby' );
function sk_filesize_column_orderby( $query ) {
if( ! is_admin() || ! $query->is_main_query() ) {
return;
}
$orderby = $query->get( 'orderby');
if( 'filesize' == $orderby ) {
$query->set( 'meta_key', '_wp_attached_file' );
$query->set( 'orderby', 'meta_value_num' ); // This assumes the file sizes are stored numerically
}
}
Duplicate Pages & Posts
function rd_duplicate_post_as_draft(){
global $wpdb;
if (! ( isset( $_GET['post']) || isset( $_POST['post']) || ( isset($_REQUEST['action']) && 'rd_duplicate_post_as_draft' == $_REQUEST['action'] ) ) ) {
wp_die('No post to duplicate has been supplied!');
}
if ( !isset( $_GET['duplicate_nonce'] ) || !wp_verify_nonce( $_GET['duplicate_nonce'], basename( __FILE__ ) ) )
return;
$post_id = (isset($_GET['post']) ? absint( $_GET['post'] ) : absint( $_POST['post'] ) );
$post = get_post( $post_id );
$current_user = wp_get_current_user();
$new_post_author = $current_user->ID;
if (isset( $post ) && $post != null) {
$args = array(
'comment_status' => $post->comment_status,
'ping_status' => $post->ping_status,
'post_author' => $new_post_author,
'post_content' => $post->post_content,
'post_excerpt' => $post->post_excerpt,
'post_name' => $post->post_name,
'post_parent' => $post->post_parent,
'post_password' => $post->post_password,
'post_status' => 'draft',
'post_title' => $post->post_title,
'post_type' => $post->post_type,
'to_ping' => $post->to_ping,
'menu_order' => $post->menu_order
);
$new_post_id = wp_insert_post( $args );
$taxonomies = get_object_taxonomies($post->post_type); // returns array of taxonomy names for post type, ex array("category", "post_tag");
foreach ($taxonomies as $taxonomy) {
$post_terms = wp_get_object_terms($post_id, $taxonomy, array('fields' => 'slugs'));
wp_set_object_terms($new_post_id, $post_terms, $taxonomy, false);
}
$post_meta_infos = $wpdb->get_results("SELECT meta_key, meta_value FROM $wpdb->postmeta WHERE post_id=$post_id");
if (count($post_meta_infos)!=0) {
$sql_query = "INSERT INTO $wpdb->postmeta (post_id, meta_key, meta_value) ";
foreach ($post_meta_infos as $meta_info) {
$meta_key = $meta_info->meta_key;
if( $meta_key == '_wp_old_slug' ) continue;
$meta_value = addslashes($meta_info->meta_value);
$sql_query_sel[]= "SELECT $new_post_id, '$meta_key', '$meta_value'";
}
$sql_query.= implode(" UNION ALL ", $sql_query_sel);
$wpdb->query($sql_query);
}
wp_redirect( admin_url( 'post.php?action=edit&post=' . $new_post_id ) );
exit;
} else {
wp_die('Post creation failed, could not find original post: ' . $post_id);
}
}
add_action( 'admin_action_rd_duplicate_post_as_draft', 'rd_duplicate_post_as_draft' );
function rd_duplicate_post_link( $actions, $post ) {
if (current_user_can('edit_posts')) {
$actions['duplicate'] = '<a href="' . wp_nonce_url('admin.php?action=rd_duplicate_post_as_draft&post=' . $post->ID, basename(__FILE__), 'duplicate_nonce' ) . '" title="Duplicate this item" rel="permalink">Duplicate</a>';
}
return $actions;
}
add_filter( 'post_row_actions', 'rd_duplicate_post_link', 10, 2 );
add_filter( 'page_row_actions', 'rd_duplicate_post_link', 10, 2 );
.HTACCESS SPECIFIC CODE SNIPPETS
Force redirects from HTTP to HTTPS without adding the domain name
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Fix for Semrush audit issue 2 subdomains don’t support HSTS
Header add Strict-Transport-Security: "max-age=15768000;includeSubdomains"
Stop theme files from entering SERPs
# Don't show directory listings for URLs which map to a directory. Options -Indexes


