PHP Code Snippets I Use (Wordpress/ClassicPress)

noellarkin

Senior Member
Joined
Mar 14, 2021
Messages
1,006
Reaction score
1,492
Setting up some shared hosting PBNs at the moment, wanted to share some php code snippets (inserted in a functions.php file inside a child theme). Using these will probably help you cut down on the number of plugins you need.
Note: none of these scripts were written by me, I sourced them from different places on the net, most of them are from stackexchange.

DISABLE MULTIPLE IMAGE SIZES
PHP:
function shapeSpace_disable_image_sizes($sizes) {
    
    unset($sizes['thumbnail']);    // disable thumbnail size
    unset($sizes['medium']);       // disable medium size
    unset($sizes['large']);        // disable large size
    unset($sizes['medium_large']); // disable medium-large size
    unset($sizes['1536x1536']);    // disable 2x medium-large size
    unset($sizes['2048x2048']);    // disable 2x large size
    
    return $sizes;
    
}
add_action('intermediate_image_sizes_advanced', 'shapeSpace_disable_image_sizes');

// disable scaled image size
add_filter('big_image_size_threshold', '__return_false');

// disable other image sizes
function shapeSpace_disable_other_image_sizes() {
    
    remove_image_size('post-thumbnail'); // disable images added via set_post_thumbnail_size()
    remove_image_size('another-size');   // disable any other added image sizes
    
}
add_action('init', 'shapeSpace_disable_other_image_sizes');

DISABLE SEARCH FUNCTIONALITY
PHP:
function ifb_filter_query($query)
{
    if (is_search()) {
        $query->is_search = false;
        $query->query_vars['s'] = false;
        $query->query['s'] = false;
    }
}
add_action('parse_query', 'ifb_filter_query', 1, 1);

add_filter('get_search_form', function ($a) {
    return '';
});

function ifb_remove_search_widget()
{
    unregister_widget('WP_Widget_Search');
}
add_action('widgets_init', 'ifb_remove_search_widget');

DISABLE COMMENTS FEATURE
PHP:
/**
 * For example, copy the following to "wp-content/mu-plugins/disable-comments.php"
 * to use it as a "must-use plugin" (always-on and before "normals" plugins)
 */

add_action('admin_init', function () {
    // Redirect any user trying to access comments page
    global $pagenow;
    
    if ($pagenow === 'edit-comments.php' || $pagenow === 'options-discussion.php') {
        wp_redirect(admin_url());
        exit;
    }

    // Remove comments metabox from dashboard
    remove_meta_box('dashboard_recent_comments', 'dashboard', 'normal');

    // Disable support for comments and trackbacks in post types
    foreach (get_post_types() as $post_type) {
        if (post_type_supports($post_type, 'comments')) {
            remove_post_type_support($post_type, 'comments');
            remove_post_type_support($post_type, 'trackbacks');
        }
    }
});

// Close comments on the front-end
add_filter('comments_open', '__return_false', 20, 2);
add_filter('pings_open', '__return_false', 20, 2);

// Hide existing comments
add_filter('comments_array', '__return_empty_array', 10, 2);

// Remove comments page and option page in menu
add_action('admin_menu', function () {
    remove_menu_page('edit-comments.php');
    remove_submenu_page('options-general.php', 'options-discussion.php');
});

// Remove comments links from admin bar
add_action('init', function () {
    if (is_admin_bar_showing()) {
        remove_action('admin_bar_menu', 'wp_admin_bar_comments_menu', 60);
    }
});

DISABLE ALL DASHBOARD WIDGETS
PHP:
function remove_dashboard_widgets() {
    global $wp_meta_boxes;
    
    unset( $wp_meta_boxes['dashboard']['side']['core']['dashboard_quick_press'] );
    unset( $wp_meta_boxes['dashboard']['normal']['core']['dashboard_incoming_links'] );
    unset( $wp_meta_boxes['dashboard']['normal']['core']['dashboard_right_now'] );
    unset( $wp_meta_boxes['dashboard']['normal']['core']['dashboard_plugins'] );
    unset( $wp_meta_boxes['dashboard']['normal']['core']['dashboard_recent_drafts'] );
    unset( $wp_meta_boxes['dashboard']['normal']['core']['dashboard_recent_comments'] );
    unset( $wp_meta_boxes['dashboard']['side']['core']['dashboard_primary'] );
    unset( $wp_meta_boxes['dashboard']['side']['core']['dashboard_secondary'] );

    remove_meta_box( 'dashboard_activity', 'dashboard', 'normal' );
}
add_action( 'wp_dashboard_setup', 'remove_dashboard_widgets' );


REDIRECT ATTACHMENT PAGES TO MEDIA URLs
PHP:
function wpseo_change_attachment_link($url, $id) {
    $attachment_url = wp_get_attachment_url($id);
    if ($attachment_url) {
        return $attachment_url;
    }
    return $url;
}

add_filter('attachment_link', 'wpseo_change_attachment_link', 10, 2);

FEATURED IMAGES AS POST COLUMN IN ADMIN
PHP:
// show featured images in dashboard
add_image_size( 'haizdesign-admin-post-featured-image', 120, 120, false );

// Add the posts and pages columns filter. They both use the same function.
add_filter('manage_posts_columns', 'haizdesign_add_post_admin_thumbnail_column', 2);
add_filter('manage_pages_columns', 'haizdesign_add_post_admin_thumbnail_column', 2);

// Add the column
function haizdesign_add_post_admin_thumbnail_column($haizdesign_columns){
    $haizdesign_columns['haizdesign_thumb'] = __('Featured Image');
    return $haizdesign_columns;
}

// Manage Post and Page Admin Panel Columns
add_action('manage_posts_custom_column', 'haizdesign_show_post_thumbnail_column', 5, 2);
add_action('manage_pages_custom_column', 'haizdesign_show_post_thumbnail_column', 5, 2);

// Get featured-thumbnail size post thumbnail and display it
function haizdesign_show_post_thumbnail_column($haizdesign_columns, $haizdesign_id){
    switch($haizdesign_columns){
        case 'haizdesign_thumb':
        if( function_exists('the_post_thumbnail') ) {
            echo the_post_thumbnail( 'haizdesign-admin-post-featured-image' );
        }
        else
            echo 'hmm… your theme doesn\'t support featured image…';
        break;
    }
}

POST FEATURED IMAGE IN RSS FEED
PHP:
function rss_post_thumbnail($content) {
global $post;
if(has_post_thumbnail($post->ID)) {
$content = '<p>' . get_the_post_thumbnail($post->ID) .
'</p>' . get_the_content();
}
return $content;
}
add_filter('the_excerpt_rss', 'rss_post_thumbnail');
add_filter('the_content_feed', 'rss_post_thumbnail');
 
I'm just going to be learning in depth wordpress development next year! Thanks for sharing, these seem easy enough to implement.
 
Thanks great share for those building a pbn.

What use is there for: POST FEATURED IMAGE IN RSS FEED?
 
I'm just going to be learning in depth wordpress development next year! Thanks for sharing, these seem easy enough to implement.
yep, I'm starting on that too, I've heard Jeff Starr is an OG WP dev, probably getting his book on the subject.
 
He is cool, he just the other day released a new revision of his book to, checkout his plugins to.
yep that's the one. I'm already using his 7G firewall .htaccess rules :)
 
Back
Top