在 WordPress 中,透過程式碼片段可以修改網站功能、調整外觀,甚至加入追蹤碼或驗證碼。對於 網頁設計公司 來說,這是一個靈活且高效的方法來客製化網站,而無需依賴過多的外掛。
程式碼片段是用來擴充或修改 WordPress 網站功能的小段 PHP、CSS 或 JavaScript 代碼。例如:
這些程式碼能幫助網頁設計公司更有效率地管理客戶網站,提高安全性與效能。
php
複製
add_filter(
'upload_mimes',
function ( $upload_mimes ) {
if ( ! current_user_can( 'administrator' ) ) {
return $upload_mimes;
}
$upload_mimes['svg'] = 'image/svg+xml';
$upload_mimes['svgz'] = 'image/svg+xml';
return $upload_mimes;
}
);
php
複製
add_filter('gutenberg_can_edit_post', '__return_false', 5);
add_filter('use_block_editor_for_post', '__return_false', 5);
php
複製
add_filter('the_generator', '__return_empty_string');
php
複製
add_filter( 'use_widgets_block_editor', '__return_false' );
php
複製
add_filter( 'auto_update_core', '__return_false' );
add_filter( 'auto_update_plugin', '__return_false' );
add_filter( 'auto_update_theme', '__return_false' );
php
複製
add_action(
'template_redirect',
function () {
global $post;
if ( ! is_attachment() || ! isset( $post->post_parent ) || ! is_numeric( $post->post_parent ) ) {
return;
}
if ( 0 !== $post->post_parent && 'trash' !== get_post_status( $post->post_parent ) ) {
wp_safe_redirect( get_permalink( $post->post_parent ), 301 );
} else {
wp_safe_redirect( get_bloginfo( 'wpurl' ), 302 );
}
exit;
},
1
);
php
複製
add_action('admin_init', function () {
global $pagenow;
if ($pagenow === 'edit-comments.php') {
wp_safe_redirect(admin_url());
exit;
}
remove_meta_box('dashboard_recent_comments', 'dashboard', 'normal');
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');
}
}
});
add_filter('comments_open', '__return_false', 20, 2);
add_filter('pings_open', '__return_false', 20, 2);
add_filter('comments_array', '__return_empty_array', 10, 2);
add_action('admin_menu', function () {
remove_menu_page('edit-comments.php');
});
add_action('admin_bar_menu', function () {
remove_action('admin_bar_menu', 'wp_admin_bar_comments_menu', 60);
}, 0);
在 WordPress 後台,前往 [外掛] → [安裝外掛],搜尋 FluentSnippets,點選 [立即安裝],接著點選 [啟用]。
在 WordPress 後台左側找到 FluentSnippets,點選 New Snippet,依需求輸入以下資訊:
最後,點選 Create Snippet 即可完成。
對於網頁設計公司來說,使用程式碼片段能提升網站開發效率,減少對外掛的依賴,並強化安全性。如果你希望進一步優化 WordPress 網站,建議選擇合適的工具來管理這些程式碼,確保網站效能與穩定性。