您是否正在寻找一些可在您的网站上使用的 WordPress 代码片段?通过向 WordPress 网站添加代码片段,您可以构建主题和插件无法实现的独特设计和功能。片段还可以提高网站的安全性,并使管理仪表板更加用户友好。在本文中,我们将与您分享对初学者最有用的 WordPress 代码片段列表。
![对初学者有用的 WordPress 代码片段 对初学者有用的 WordPress 代码片段](https://img.itaoda.cn/wp-content/uploads/2024/02/WordPress-code-snippets-for-beginners-OG.png?imageMogr2/auto-orient/format/webp/blur/1x0/quality/60|watermark/2/text/5rqQ56CB56S-/font/5b6u6L2v6ZuF6buR/fontsize/1200/fill/IzAwMDAwMA==/dissolve/6/gravity/Center/dx/10/dy/10|imageslim)
为什么在 WordPress 中添加代码片段?
如果您有WordPress 网站,那么向您的主题文件或代码片段插件添加一些有用的代码片段可以帮助您解锁无限的自定义并使您的网站脱颖而出。
您可以使用自定义代码来定制网站上的某些特定元素。例如,您可以通过添加简单的 CSS 代码片段来更改 WordPress 中的文本选择颜色。
作为初学者,添加一些有用的代码片段还可以通过减少对多个插件的需求来提高网站的性能和速度。
除此之外,代码片段可以帮助您扩展编码技能并使用 WordPress 社区免费共享的庞大代码片段库。
1.允许SVG文件上传
SVG(可缩放矢量图形)是一种使用 XML 标记语言定义矢量图形的文件格式。这种格式允许您放大图像而不损失任何质量。
![SVG 图像质量损失 SVG 图像质量损失](https://img.itaoda.cn/wp-content/uploads/2024/02/svgnoqualityloss.jpg?imageMogr2/auto-orient/format/webp/blur/1x0/quality/60|watermark/2/text/5rqQ56CB56S-/font/5b6u6L2v6ZuF6buR/fontsize/1200/fill/IzAwMDAwMA==/dissolve/6/gravity/Center/dx/10/dy/10|imageslim)
这些文件比 JPEG 或 PNG 更小、更轻,可帮助您提高网站速度。
但是,WordPress 默认情况下不允许上传 SVG 文件,因为 SVG 可能包含危害站点安全的恶意代码。
考虑到这一点,如果您仍然想在网站上上传 SVG 文件,那么您可以将以下代码片段添加到您的网站:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 号
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
/** * Allow SVG uploads for administrator users. * * @param array $upload_mimes Allowed mime types. * * @return mixed */ add_filter( 'upload_mimes' , function ( $upload_mimes ) { // By default, only administrator users are allowed to add SVGs. // To enable more user types edit or comment the lines below but beware of // the security risks if you allow any user to upload SVG files. if ( ! current_user_can( 'administrator' ) ) { return $upload_mimes ; } $upload_mimes [ 'svg' ] = 'image/svg+xml' ; $upload_mimes [ 'svgz' ] = 'image/svg+xml' ; return $upload_mimes ; } ); /** * Add SVG files mime check. * * @param array $wp_check_filetype_and_ext Values for the extension, mime type, and corrected filename. * @param string $file Full path to the file. * @param string $filename The name of the file (may differ from $file due to $file being in a tmp directory). * @param string[] $mimes Array of mime types keyed by their file extension regex. * @param string|false $real_mime The actual mime type or false if the type cannot be determined. */ add_filter( 'wp_check_filetype_and_ext' , function ( $wp_check_filetype_and_ext , $file , $filename , $mimes , $real_mime ) { if ( ! $wp_check_filetype_and_ext [ 'type' ] ) { $check_filetype = wp_check_filetype( $filename , $mimes ); $ext = $check_filetype [ 'ext' ]; $type = $check_filetype [ 'type' ]; $proper_filename = $filename ; if ( $type && 0 === strpos ( $type , 'image/' ) && 'svg' !== $ext ) { $ext = false; $type = false; } $wp_check_filetype_and_ext = compact( 'ext' , 'type' , 'proper_filename' ); } return $wp_check_filetype_and_ext ; }, 10, 5 ); |
您可以将此代码添加到主题的functions.php文件中或使用代码片段插件(例如WPCode )。在本文后面,我们将向您展示具体如何执行此操作。
2.禁用WP管理栏
默认情况下,WordPress 会在网站顶部向所有登录用户(例如订阅者、作者、编辑者和任何其他用户角色)显示一个管理栏。
此管理栏可以将他们引导至您的 WordPress 仪表板,他们可以在其中根据其用户权限对您的网站进行任何更改。
然而,当您查看网站的前端时,它可能会有点分散您的注意力,因为它有时可能与标题等设计元素重叠。
![WordPress 管理栏 WordPress 管理栏](https://img.itaoda.cn/wp-content/uploads/2024/02/wordpress-admin-bar.png?imageMogr2/auto-orient/format/webp/blur/1x0/quality/60|watermark/2/text/5rqQ56CB56S-/font/5b6u6L2v6ZuF6buR/fontsize/1200/fill/IzAwMDAwMA==/dissolve/6/gravity/Center/dx/10/dy/10|imageslim)
要禁用 WP 管理栏,只需将以下 PHP 代码片段添加到您的 WordPress 网站:
1
2
|
/* Disable WordPress Admin Bar for all users */ add_filter( 'show_admin_bar' , '__return_false' ); |
代码执行后,管理栏不会显示在网站的前端。
3.删除WordPress版本号
WordPress 在您的网站上显示当前的 WordPress 版本号以供跟踪。
![删除 WordPress 版本号 删除 WordPress 版本号](https://img.itaoda.cn/wp-content/uploads/2024/02/remove-wordpress-version-number.jpg?imageMogr2/auto-orient/format/webp/blur/1x0/quality/60|watermark/2/text/5rqQ56CB56S-/font/5b6u6L2v6ZuF6buR/fontsize/1200/fill/IzAwMDAwMA==/dissolve/6/gravity/Center/dx/10/dy/10|imageslim)
然而,有时,这种足迹可能会告诉黑客正在使用的 WordPress 版本,从而导致安全漏洞。然后,黑客可以针对特定版本中的已知漏洞。
要删除版本号,请将以下代码片段添加到您的网站:
1
|
add_filter( 'the_generator' , '__return_empty_string' ); |
一旦你这样做了,黑客将无法通过自动扫描仪和其他不太复杂的尝试来猜测你的 WordPress 版本。
4. 将特色图像添加到 RSS 源
RSS 源允许用户通过 Feedly 等源阅读器接收有关您的WordPress 博客的定期更新。
这可以帮助推广您的内容并为您的网站带来更多流量。通过在 RSS 源中的帖子旁边添加特色图像或缩略图,您可以使源在视觉上更具吸引力并进一步改善用户体验。
![带有帖子缩略图预览的 Feed 带有帖子缩略图预览的 Feed](https://img.itaoda.cn/wp-content/uploads/2024/02/feed-with-post-thumbnails-preview.jpg?imageMogr2/auto-orient/format/webp/blur/1x0/quality/60|watermark/2/text/5rqQ56CB56S-/font/5b6u6L2v6ZuF6buR/fontsize/1200/fill/IzAwMDAwMA==/dissolve/6/gravity/Center/dx/10/dy/10|imageslim)
通过添加以下有用的 WordPress 代码片段,您可以轻松地在 RSS 源中显示帖子缩略图:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 号
18
|
/** * Add the post thumbnail, if available, before the content in feeds. * * @param string $content The post content. * * @return string */ function wpcode_snippet_rss_post_thumbnail( $content ) { global $post ; if ( has_post_thumbnail( $post ->ID ) ) { $content = '<p>' . get_the_post_thumbnail( $post ->ID ) . '</p>' . $content ; } return $content ; } add_filter( 'the_excerpt_rss' , 'wpcode_snippet_rss_post_thumbnail' ); add_filter( 'the_content_feed' , 'wpcode_snippet_rss_post_thumbnail' ); |
这可以使您的 Feed 更具吸引力,并吸引访客回访您的网站。
5.禁用自动更新电子邮件
默认情况下,WordPress 每次自动更新任何插件、主题或核心本身时都会向您发送电子邮件通知。
如果您有多个 WordPress 网站并且在打开电子邮件帐户时不断看到这些通知,这可能会变得非常烦人。
![自动更新后的电子邮件通知预览 自动更新后的电子邮件通知预览](https://img.itaoda.cn/wp-content/uploads/2024/02/disableupdatesnotificationemail.png?imageMogr2/auto-orient/format/webp/blur/1x0/quality/60|watermark/2/text/5rqQ56CB56S-/font/5b6u6L2v6ZuF6buR/fontsize/1200/fill/IzAwMDAwMA==/dissolve/6/gravity/Center/dx/10/dy/10|imageslim)
在这种情况下,您可以通过将以下 PHP 代码片段添加到您的网站来轻松禁用自动更新电子邮件:
1
2
3
4
5
6
7
8
|
// Disable auto-update emails. add_filter( 'auto_core_update_send_email' , '__return_false' ); // Disable auto-update emails for plugins. add_filter( 'auto_plugin_update_send_email' , '__return_false' ); // Disable auto-update emails for themes. add_filter( 'auto_theme_update_send_email' , '__return_false' ); |
完成此操作后,您将不会收到任何有关插件或主题自动更新的通知。
6. 更改管理栏中的“管理员您好”
当您登录 WordPress 仪表板时,您会在屏幕右上角看到一句“你好”,后面跟着您的显示名称。
这种问候语对您来说可能听起来不自然或看起来过时,甚至有点烦人。
![在管理栏中更改 Howdy 在管理栏中更改 Howdy](https://img.itaoda.cn/wp-content/uploads/2024/02/change-howdy-in-admin-bar.png?imageMogr2/auto-orient/format/webp/blur/1x0/quality/60|watermark/2/text/5rqQ56CB56S-/font/5b6u6L2v6ZuF6buR/fontsize/1200/fill/IzAwMDAwMA==/dissolve/6/gravity/Center/dx/10/dy/10|imageslim)
您可以通过将以下代码片段添加到您的 WordPress 网站来轻松更改管理栏中的问候语:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
function wpcode_snippet_replace_howdy( $wp_admin_bar ) { // Edit the line below to set what you want the admin bar to display intead of "Howdy,". $new_howdy = 'Welcome,' ; $my_account = $wp_admin_bar ->get_node( 'my-account' ); $wp_admin_bar ->add_node( array ( 'id' => 'my-account' , 'title' => str_replace ( 'Howdy,' , $new_howdy , $my_account ->title ), ) ); } add_filter( 'admin_bar_menu' , 'wpcode_snippet_replace_howdy' , 25 ); |
添加代码后,您还必须在$new_howdy =
代码中的行旁边添加您喜欢的问候语。
7.禁用XML-RPC
XML-RPC 是 WordPress 的核心API。它允许用户通过第三方服务连接到他们的网站。
例如,如果您想使用Uncanny Automator等自动化工具或移动应用程序来管理您的网站,则需要启用 XML-RPC 。
但是,如果您不想使用任何这些功能,那么我们建议禁用 XML-RPC 以防止黑客访问您的网站。
黑客可以利用这些漏洞找到您的登录凭据或发起DDoS 攻击。
要禁用 XML-RPC,您可以在网站上使用以下代码片段:
1
|
add_filter( 'xmlrpc_enabled' , '__return_false' ); |
8.禁用自动垃圾清空
WordPress 会删除垃圾箱中超过 30 天的所有内容,包括帖子、页面和媒体文件。
但是,某些用户可能不希望自动清空垃圾箱,以便他们可以随时恢复已删除的文件。
![查看已删除的帖子 查看已删除的帖子](https://img.itaoda.cn/wp-content/uploads/2024/02/view-trashed-posts-1.png?imageMogr2/auto-orient/format/webp/blur/1x0/quality/60|watermark/2/text/5rqQ56CB56S-/font/5b6u6L2v6ZuF6buR/fontsize/1200/fill/IzAwMDAwMA==/dissolve/6/gravity/Center/dx/10/dy/10|imageslim)
在这种情况下,您可以将以下代码片段添加到您的 WordPress 网站:
1
2
3
|
add_action( 'init' , function () { remove_action( 'wp_scheduled_delete' , 'wp_scheduled_delete' ); } ); |
添加此代码后,您现在必须手动清空垃圾箱。
9.更改摘录长度
摘录是您的博客文章的前几行,显示在您的 WordPress 主页、博客或档案页面的文章标题下。
您可能需要缩短摘录长度,以引起用户的兴趣,并鼓励他们点击帖子以了解更多信息。同样,您还可以增加长度,向读者提供更多背景信息和关键信息,而无需点击帖子。
![更改摘录长度 更改摘录长度](https://img.itaoda.cn/wp-content/uploads/2024/02/change-excerpt-length.jpg?imageMogr2/auto-orient/format/webp/blur/1x0/quality/60|watermark/2/text/5rqQ56CB56S-/font/5b6u6L2v6ZuF6buR/fontsize/1200/fill/IzAwMDAwMA==/dissolve/6/gravity/Center/dx/10/dy/10|imageslim)
要更改摘录长度,只需将以下代码片段添加到您的网站:
1
2
3
4
5
6
7
8
|
add_filter( 'excerpt_length' , function ( $length ) { // Number of words to display in the excerpt. return 40; }, 500 ); |
默认情况下,此代码段会将摘录限制为 40 个单词,但您可以将第 5 行的数字调整为最适合您的博客的数字。
10.禁用站点管理员电子邮件验证
默认情况下,WordPress 每隔几个月向站点管理员发送一封管理员验证电子邮件,以验证他们使用的电子邮件是否仍然正确。
然而,有时此通知可能会比必要的频率更频繁地发送给您,这可能会很烦人。
![管理员电子邮件验证通知 管理电子邮件验证](https://img.itaoda.cn/wp-content/uploads/2024/02/admin-email-verification-notice.png?imageMogr2/auto-orient/format/webp/blur/1x0/quality/60|watermark/2/text/5rqQ56CB56S-/font/5b6u6L2v6ZuF6buR/fontsize/1200/fill/IzAwMDAwMA==/dissolve/6/gravity/Center/dx/10/dy/10|imageslim)
幸运的是,您可以通过将以下代码片段添加到您的 WordPress 网站来禁用管理员电子邮件验证通知:
1
|
add_filter( 'admin_email_check_interval' , '__return_false' ); |
11.禁用自动更新
WordPress 自动更新其核心软件、插件或主题,以减少安全威胁、恶意软件感染、网站泄露和数据盗窃。
但是,自动更新有时会引入兼容性问题,或在极少数情况下破坏您的网站。
在这种情况下,您可以使用以下代码片段来禁用自动更新:
1
2
3
4
5
6
|
// Disable core auto-updates add_filter( 'auto_update_core' , '__return_false' ); // Disable auto-updates for plugins. add_filter( 'auto_update_plugin' , '__return_false' ); // Disable auto-updates for themes. add_filter( 'auto_update_theme' , '__return_false' ); |
这将禁用核心软件、主题和插件的所有 WordPress 自动更新。
如何在 WordPress 中添加代码片段(简单方法)
现在您已经了解了对初学者最有用的 WordPress 代码片段,您可以轻松地将它们添加到主题的样式表或functions.php 文件中。
但是,请记住,键入代码时的最小错误都可能会破坏您的网站并使其无法访问。另外,如果您切换到不同的主题,那么所有自定义代码都将丢失,您将必须重新添加它。
这就是为什么我们总是推荐使用WPCode。
![WPCode - 最佳 WordPress 代码片段插件 WPCode - 最佳 WordPress 代码片段插件](https://img.itaoda.cn/wp-content/uploads/2024/02/wpcode.jpg?imageMogr2/auto-orient/format/webp/blur/1x0/quality/60|watermark/2/text/5rqQ56CB56S-/font/5b6u6L2v6ZuF6buR/fontsize/1200/fill/IzAwMDAwMA==/dissolve/6/gravity/Center/dx/10/dy/10|imageslim)
它是市场上最好的 WordPress 代码片段插件,使向您的网站添加自定义代码变得超级安全且轻松。
另外,该插件还附带了一个包含 900 多个代码片段的库,包括我们上面提到的所有代码片段。有关更多信息,请参阅我们完整的WPCode 评论。
![WPCode 中的代码片段 WPCode 中的代码片段](https://img.itaoda.cn/wp-content/uploads/2024/02/code-snippets-in-wpcode.png?imageMogr2/auto-orient/format/webp/blur/1x0/quality/60|watermark/2/text/5rqQ56CB56S-/font/5b6u6L2v6ZuF6buR/fontsize/1200/fill/IzAwMDAwMA==/dissolve/6/gravity/Center/dx/10/dy/10|imageslim)
首先,您需要安装并激活WPCode插件。
注意:还有一个免费的 WPCode 插件可供您使用。但是,升级到高级插件将使您可以访问基于云的片段库、代码修订等。
激活后,从 WordPress 仪表板访问代码片段 » + 添加片段页面。
这将带您进入代码段库,您可以在其中单击“添加自定义代码(新代码段)”选项下的“使用代码段”按钮,将自定义代码添加到您的网站。
但是,如果您想使用预制代码片段,则只需单击该选项下的“使用代码片段”按钮即可。
![单击预制代码片段下的“使用片段”按钮 单击预制代码片段下的“使用片段”按钮](https://img.itaoda.cn/wp-content/uploads/2024/02/click-use-snippet-under-a-code-snippet.png?imageMogr2/auto-orient/format/webp/blur/1x0/quality/60|watermark/2/text/5rqQ56CB56S-/font/5b6u6L2v6ZuF6buR/fontsize/1200/fill/IzAwMDAwMA==/dissolve/6/gravity/Center/dx/10/dy/10|imageslim)
如果您要添加自定义代码片段,则只需将其粘贴到“代码预览”框中即可。
然后,向下滚动到“插入”部分并选择“自动插入”模式。代码段激活后,代码将在您的网站上自动执行。
![选择插入方法 选择插入方法](https://img.itaoda.cn/wp-content/uploads/2024/02/choose-an-insertion-method-1.png?imageMogr2/auto-orient/format/webp/blur/1x0/quality/60|watermark/2/text/5rqQ56CB56S-/font/5b6u6L2v6ZuF6buR/fontsize/1200/fill/IzAwMDAwMA==/dissolve/6/gravity/Center/dx/10/dy/10|imageslim)
最后,访问页面顶部并将非活动开关切换为活动状态。之后,只需单击“更新”按钮即可存储您的设置。
您现在已成功将代码片段添加到您的 WordPress 网站。
![激活并更新代码片段 激活并更新代码片段](https://img.itaoda.cn/wp-content/uploads/2024/02/update-code-snippet.png?imageMogr2/auto-orient/format/webp/blur/1x0/quality/60|watermark/2/text/5rqQ56CB56S-/font/5b6u6L2v6ZuF6buR/fontsize/1200/fill/IzAwMDAwMA==/dissolve/6/gravity/Center/dx/10/dy/10|imageslim)
关于 WordPress 代码片段的常见问题
以下是我们的读者经常询问的有关在 WordPress 中使用自定义代码和代码片段的一些问题的列表。
如何在我的 WordPress 网站上显示代码?
如果您撰写有关技术主题的博客文章,那么向您的帖子添加代码片段可能会很有用。为此,您必须打开要显示代码片段的页面/帖子,然后单击添加块“+”按钮。
完成此操作后,只需从块菜单插入代码块,然后将自定义代码添加到块本身中。
![在 WordPress 中添加代码块 在 WordPress 中添加代码块](https://img.itaoda.cn/wp-content/uploads/2024/02/add-code-block.png?imageMogr2/auto-orient/format/webp/blur/1x0/quality/60|watermark/2/text/5rqQ56CB56S-/font/5b6u6L2v6ZuF6buR/fontsize/1200/fill/IzAwMDAwMA==/dissolve/6/gravity/Center/dx/10/dy/10|imageslim)
最后,单击顶部的“发布”或“更新”按钮来存储您的更改。
代码片段现在将显示在您的 WordPress 网站上。
如何从头开始创建 WordPress 网站而无需编码?
如果您想从头开始创建一个网站而不使用任何代码,那么您可以使用SeedProd。
它是市场上最好的 WordPress 页面构建器,让您无需任何编码即可创建自定义主题和登陆页面。
![SeedProd 网站和主题生成器 SeedProd 网站和主题生成器](https://img.itaoda.cn/wp-content/uploads/2024/02/seedprod.png?imageMogr2/auto-orient/format/webp/blur/1x0/quality/60|watermark/2/text/5rqQ56CB56S-/font/5b6u6L2v6ZuF6buR/fontsize/1200/fill/IzAwMDAwMA==/dissolve/6/gravity/Center/dx/10/dy/10|imageslim)
该插件附带 300 多个预制模板、拖放构建器和众多高级块,让您只需点击几下即可构建有吸引力的网站。
在哪里可以获取 WordPress 代码片段?
您可以使用 WPCode 的库访问 900 多个代码片段,并将它们轻松添加到您的网站。
但是,如果您不使用WPCode,那么您还可以从 Stack Overflow、CodePen 或GenerateWP 等网站获取预先编写的代码片段。
我们希望本文能帮助您找到对初学者最有用的 WordPress 代码片段。您可能还想查看有关最佳 WordPress 主题构建器。
原文链接:https://www.itaoda.cn/blog/14073.html,转载请注明出处。
评论0