ad网络粘贴_从网络将片段粘贴到WordPress的初学者指南

ad网络粘贴

At WPBeginner, we often publish tutorials asking users to add code snippets into WordPress. Copying and pasting code in WordPress theme files may seem easy to experienced users, but it is quite tedious and intimidating for beginners. Due to unfamiliarity with coding, beginners may end up making a mistake that could crash their website. In this step by step guide, we will show you how to copy and paste code snippets from web into WordPress.

在WPBeginner,我们经常发布要求用户向WordPress添加代码段的教程。 对于经验丰富的用户而言,将代码复制和粘贴到WordPress主题文件中似乎很容易,但是对于初学者而言,这是相当繁琐且令人生畏的。 由于不熟悉编码,因此初学者可能会犯一个错误,从而使他们的网站崩溃。 在本分步指南中,我们将向您展示如何从Web复制代码片段并将其粘贴到WordPress。

How to safely copy and paste code snippets in WordPress
为什么要向您的WordPress网站添加代码段? (Why Add Code Snippets to Your WordPress Site?)

WordPress is the best website builder in the world due to the flexibility it offers. It is super easy to add new features and functionality to your WordPress website.

由于WordPress提供的灵活性,它是世界上最好的网站构建器 。 向您的WordPress网站添加新功能非常容易。

WordPress resource sites, like WPBeginner share tips and hacks that you can add to improve your website. While adding custom code snippets to your website is optional, and you can often find plugins to do the same task, sometimes a simple snippet is more efficient.

WordPress资源站点(例如WPBeginner)共享提示和技巧 ,您可以添加这些技巧和技巧来改善您的网站。 虽然将自定义代码段添加到您的网站是可选的,并且您通常可以找到执行相同任务的插件,但有时使用简单的代码段会更有效率。

Some code snippets can be very handy and can help you fix many common WordPress errors, improve WordPress security, and even add new features to your website.

一些代码片段非常方便,可以帮助您修复许多常见的WordPress错误 ,提高WordPress安全性 ,甚至为您的网站添加新功能。

That being said, let’s take a look at how to safely add code snippets into your WordPress website.

话虽如此,让我们看一下如何安全地将代码段添加到WordPress网站中。

什么是在WordPress中添加代码段的最佳方法 (What are The Best Ways to Add Code Snippets in WordPress)

First thing you must do on every WordPress website is install a WordPress backup plugin. This keeps your WordPress site safe, and you can always restore it from backup in case anything goes wrong.

在每个WordPress网站上必须做的第一件事是安装WordPress备份插件 。 这样可以确保您的WordPress网站安全,并且万一出了问题,您始终可以从备份中恢复它。

Sometimes you may find instructions to add code snippets in WordPress theme templates like index.php, single.php, header.php, and more. These code snippets are useful only in those particular templates, so you will have to add them directly to your theme files or create a child theme.

有时,您可能会发现在WordPress主题模板(例如index.php,single.php,header.php等)中添加代码段的说明。 这些代码段仅在特定模板中有用,因此您必须将它们直接添加到主题文件中或创建子主题

However, most custom code snippets are added into your WordPress theme’s functions.php file. While the tutorial may recommend you to add it in the theme’s functions.php file, there is a much better alternative that works the same way as a functions file.

但是,大多数自定义代码段都添加到了WordPress主题的functions.php文件中 。 尽管本教程可能建议您将其添加到主题的functions.php文件中,但是还有更好的替代方法,其工作方式与功能文件相同。

Let’s take a look at some of the ways you can add custom code snippets in WordPress.

让我们看一下在WordPress中添加自定义代码段的一些方法。

1. Use The Code Snippets WordPress Plugin

1.使用代码片段WordPress插件

Code Snippets plugin

This is the safest and most recommended method for beginners. Code Snippets is a WordPress plugin which allows you to easily add and manage custom code snippets on your website.

对于初学者来说,这是最安全,最推荐的方法。 代码段是一个WordPress插件,可让您轻松地在网站上添加和管理自定义代码段。

It comes with a fail-safe switch which immediately deactivates a code snippet if it causes an error. This protects you from losing access to your website when adding a custom code snippet.

它带有故障安全开关,如果它引起错误,则该开关立即将代码段停用。 这样可以防止您在添加自定义代码段时失去对网站的访问权限。

For detailed instructions, see our guide on how to easily add custom code snippets in WordPress.

有关详细说明,请参见有关如何轻松在WordPress中添加自定义代码段的指南。

Note: Code snippets method is useful for snippets that need to be added in a functions file. If you are asked to add a code snippet in other theme files, then this method will not work.

注意:代码段方法对于需要在功能文件中添加的代码段很有用。 如果要求您在其他主题文件中添加代码段,则此方法将不起作用。

2. Create a Child Theme to Save Custom Code

2.创建一个子主题以保存自定义代码

Using child theme to add custom code snippets in WordPress

The second approach is to create a child theme. Using child theme will keep your changes intact when you update your theme. It will also allow you to add code to other theme files if needed without worrying about updates undoing your changes.

第二种方法是创建一个子主题。 当您更新主题时,使用子主题将使您的更改保持原样。 它还将允许您在需要时将代码添加到其他主题文件,而不必担心更新会撤消更改。

For more details, see our guide on how to create a child theme in WordPress.

有关更多详细信息,请参见有关如何在WordPress中创建子主题的指南。

Note: This method will also work for code snippets that are needed to be added in other theme templates.

注意:此方法也适用于需要在其他主题模板中添加的代码段。

3. Use a Site-Specific Plugin for Custom Code

3.使用特定于站点的插件来定制代码

Another flexible option is to use a site-specific WordPress plugin. This is a custom plugin that you can create for your own website and use it to save all your custom code.

另一个灵活的选择是使用特定于站点的WordPress插件。 这是一个自定义插件,您可以为自己的网站创建该插件,并使用它保存所有自定义代码。

The advantage of this method is that your code is not dependent on your theme, and it will remain active even when you change themes. It is also not affected by any WordPress updates on your website.

这种方法的优点是您的代码不依赖主题,并且即使您更改主题也将保持活动状态。 它也不受您网站上任何WordPress更新的影响。

See our guide on how to make a site-specific plugin for your website for detailed instructions.

有关详细说明,请参见有关如何为您的网站制作特定于站点的插件的指南。

Note: This method is only applicable for code snippets that need to be added in functions file.

注意:此方法仅适用于需要在功能文件中添加的代码段。

4. Add Code Directly To Functions File

4.直接将代码添加到函数文件

It is OK to add code snippets in your theme’s functions.php file. However, there are some downsides to it.

可以在主题的functions.php文件中添加代码段。 但是,它也有一些缺点。

  • Your changes will be wiped off when you update your WordPress theme

    当您更新WordPress主题时,您的更改将被删除
  • Your code will only work if you are using that particular theme

    您的代码仅在使用该特定主题时才有效

That being said, let’s take a look at how to properly copy and paste code snippets and avoid breaking your website.

话虽如此,让我们看一下如何正确地复制和粘贴代码片段,并避免破坏您的网站。

如何编辑WordPress文件? (How to Edit WordPress Files?)

There are different ways to edit WordPress files depending on which method you choose to add custom code snippets on your website.

根据您选择在网站上添加自定义代码段的方法,有多种编辑WordPress文件的方法。

1. Adding Custom Code in Code Snippets Plugin

1.在代码片段插件中添加自定义代码

If you are using the Code Snippets plugin, then you can easily add code snippets from WordPress admin area. Simply go to Snippets » Add New page to add your custom code.

如果您使用的是代码片段插件,则可以轻松地从WordPress管理区域添加代码片段。 只需转到片段»添加新页面即可添加您的自定义代码。

Adding a new snippet in Code Snippets plugin

2. Adding Custom Code in Site-Specific WordPress Plugin

2.在特定于站点的WordPress插件中添加自定义代码

If you are adding custom code in a site-specific plugin, then you can use the built-in WordPress plugin editor to add custom code.

如果要在特定于站点的插件中添加自定义代码,则可以使用内置的WordPress插件编辑器来添加自定义代码。

Editing a site specific plugin using the WordPress plugin editor

First, select your site-specific plugin from the drop down menu labeled ‘Select plugin to edit’. The editor will load your plugin file, and you will be able to add code snippets in it.

首先,从标记为“选择要编辑的插件”的下拉菜单中选择特定于站点的插件。 编辑器将加载您的插件文件,并且您将能够在其中添加代码段。

Once you are done, click on the ‘Update File’ button to save your changes.

完成后,单击“更新文件”按钮以保存您的更改。

If there is something missing in your code or it has potential to break your website, then the plugin editor will automatically undo your changes.

如果您的代码中缺少某些内容或可能破坏您的网站,则插件编辑器将自动撤消您的更改。

Another method to add custom code in a site-specific plugin is by using FTP.

在特定于站点的插件中添加自定义代码的另一种方法是使用FTP。

Simply go to the plugin folder using your FTP client. Right click on the plugin file and then select View/Edit file.

只需使用您的FTP客户端转到插件文件夹。 右键单击插件文件,然后选择查看/编辑文件。

Edit site-specific WordPress plugin using FTP

3. Adding Custom Code to functions.php or Other Theme Templates

3.将自定义代码添加到functions.php或其他主题模板

If you are adding code snippets to your theme’s functions.php file or any other template, then you can add code directly by visiting Appearance » Editor. You can select the file where you need to add the code from the right column.

如果要将代码段添加到主题的functions.php文件或任何其他模板中,则可以通过访问Appearance»Editor直接添加代码。 您可以从右列中选择要在其中添加代码的文件。

Theme editor

The instructions you are following will tell you where to paste the code, but if they don’t, then you need to add the code at the bottom of the file.

您遵循的说明将告诉您粘贴代码的位置,但是如果没有,则需要在文件底部添加代码。

A better alternative way would be to use FTP to add custom code in theme files. Simply connect your FTP client to your website and go to /wp-content/themes/your-theme-folder/ and right click on the file that needs editing.

更好的替代方法是使用FTP在主题文件中添加自定义代码。 只需将FTP客户端连接到您的网站,然后转到/ wp-content / themes / your-theme-folder /,然后右键单击需要编辑的文件。

Edit theme files via FTP

Select View/Edit file option to open the file in the text editor.


选择查看/编辑文件选项,以在文本编辑器中打开文件。

添加自定义代码时对PHP错误进行故障排除 (Troubleshooting PHP Errors when Adding Custom Code)

There are some common mistakes that beginners make when adding custom code snippets to their websites. Let’s take a look at some of them and how to avoid them

初学者在向其网站添加自定义代码段时会犯一些常见的错误。 让我们看看其中的一些以及如何避免它们

1. Incorrect Usage of PHP Begin and End Tags

1.错误使用PHP的开始和结束标记

WordPress is written mainly in the PHP programming language which has a specific syntax that tells your server that the following code needs to be processed by PHP. Here is how a typical PHP code snippet looks like:

WordPress主要是用PHP编程语言编写的,该语言具有特定的语法,该语法告诉您的服务器以下代码需要由PHP处理。 以下是典型PHP代码段的样子:


// PHP Begin Tag
<?php

// Rest of the code goes here

// PHP End Tag
?>

All your PHP code needs to be inside the <?php and ?> tags.

您所有PHP代码都必须位于<?php?>标记内。

The PHP end tag becomes more important in files that switch back and forth in HTML. This includes most WordPress themes files which use PHP tags alongside HTML.

PHP结束标记在以HTML来回切换的文件中变得更加重要。 这包括大多数使用PHP标签和HTML的WordPress主题文件。

You need to make sure that if you are pasting your code at a location where PHP start tag is not closed then you need to add your code without the starting PHP tag.

您需要确保如果将代码粘贴到未关闭PHP开始标签的位置,则需要添加没有开始PHP标签的代码。


<?php 
// Some pre-existing code

// your custom code

?> 

If you are pasting your custom code outside or after the PHP end tag, then you need to add the PHP begin tag as well.

如果要将自定义代码粘贴到PHP结束标记之外或之后,则还需要添加PHP开始标记。


<?php 
// Some pre-existing code 
?> 

// Your custom code snippet
<?php 

?>

Almost 90% of all errors are caused by incorrect placement of PHP end or start tags. Studying the code will allow you to understand whether or not you need to add the PHP begin or end tags in your custom code snippet.

将近90%的错误是由于PHP结束标记或开始标记的放置不正确引起的。 通过研究代码,您可以了解是否需要在自定义代码段中添加PHP开始或结束标记。

Many WordPress theme files particularly functions.php file may not have a PHP end tag at all. This means that you can add your code at the bottom of the file without the start or end tags.

许多WordPress主题文件,尤其是functions.php文件,可能根本没有PHP结束标签。 这意味着您可以将代码添加到文件底部,而无需使用开始或结束标签。


<?php
// Lots of code in your theme's functions file
//
//
// Your custom code
function custom_loginlogo() {
echo '<style type="text/css">
h1 a {background-image: url('.get_bloginfo('template_directory').'/images/login_logo.png) !important; }
</style>';
}
add_action('login_head', 'custom_loginlogo');

Remember that tutorial writers may sometime assume that you already know how to use PHP start and end tags. They may simply show you a code snippet without those tags.

请记住,教程编写者有时可能会假设您已经知道如何使用PHP的开始和结束标记。 他们可能只是向您显示没有这些标签的代码段。


function custom_loginlogo() {
echo '<style type="text/css">
h1 a {background-image: url('.get_bloginfo('template_directory').'/images/login_logo.png) !important; }
</style>';
}
add_action('login_head', 'custom_loginlogo');

Since the tutorial author doesn’t know where you will be adding this code, they have skipped the PHP end and start tags. Now when you are adding such a code snippet in your theme files, you need to make sure that it is inside the PHP tags.

由于教程作者不知道您将在何处添加此代码,因此他们跳过了PHP end和start标签。 现在,当您在主题文件中添加这样的代码片段时,您需要确保它在PHP标记内。

2. Incorrect Nesting Errors

2.不正确的嵌套错误

PHP has a particular syntax for functions, conditional logics, and loops. This syntax depends on curly brackets which indicate when a function begins and when it ends.

PHP具有用于函数,条件逻辑和循环的特定语法。 此语法取决于大括号,大括号表示函数的开始时间和结束时间。

For example, here is a simple PHP function:

例如,这是一个简单PHP函数:


<?php 
function wpbeginner_tutorial() { 
echo "Hello World!"; 
} 
?>

Now if you want to add a custom code snippet that has nothing to do with this function, then you will need to put it outside this function like this:

现在,如果要添加与此功能无关的自定义代码段,则需要将其放置在此功能之外,如下所示:


// Pre-existing code in your theme file
<?php
function wpbeginner_tutorial() { 
echo "Hello World!"; 
} 
// Your custom code 
function custom_loginlogo() {
echo '<style type="text/css">
h1 a {background-image: url('.get_bloginfo('template_directory').'/images/login_logo.png) !important; }
</style>';
}
?>

If you miss the starting or ending curly brackets, then this will break the code, and you will end up with an error page.

如果您错过了大括号的开头或结尾,那么这将破坏代码,并最终显示错误页面。

了解WordPress中PHP错误 (Understanding PHP Errors in WordPress)

Errors caused by adding a custom code in WordPress often result in a detailed error message. Most of them are syntax error, parse errors, or fatal errors due to unexpected characters.

在WordPress中添加自定义代码导致的错误通常会导致详细的错误消息。 其中大多数是语法错误 ,解析错误或由于意外字符导致的致命错误

The good part is that these errors will tell you which line in your code caused the error.

好消息是这些错误将告诉您代码中的哪一行导致了错误。

PHP error message

You can then go to the exact line to review the code and figure out what you missed. For this purpose we recommend using a proper text editor for code editing because they have line numbers and syntax highlighting which can help you fix the issue easily.

然后,您可以转到确切的行以查看代码并找出丢失的内容。 为此,我们建议使用适当的文本编辑器进行代码编辑,因为它们具有行号和语法突出显示,可以帮助您轻松解决问题。

当您的WordPress网站无法访问时该怎么办? (What to do When Your WordPress Site is Inaccessible?)

First of all don’t panic. Simply connect to your website using a FTP client or File Manager app in cPanel. Next, locate the file where you added the code that caused the error and edit it.

首先不要惊慌。 只需使用cPanel中的FTP客户端或文件管理器应用程序连接到您的网站。 接下来,找到添加了导致错误的代码的文件,然后对其进行编辑。

You can try and fixing the issues with the code snippet. If you are unable to fix those issues, then remove the code and save your changes.

您可以尝试解决代码段中的问题。 如果您无法解决这些问题,请删除代码并保存更改。

Your site should be back to normal again. If it is still showing some error, then download a fresh copy of your WordPress theme and extract the zip file to your computer.

您的网站应再次恢复正常。 如果仍然显示错误,请下载WordPress主题的新副本,然后将zip文件提取到计算机中。

Upload the file where you made the changes from your computer to your website while overwriting the old file.

将您进行了更改的文件从计算机上载到网站,同时覆盖旧文件。

For more ways to solve these issues, see our guide on most common WordPress errors and how to fix them. If that doesn’t help then follow our WordPress troubleshooting guide to perform a step by step diagnosis.

有关解决这些问题的更多方法,请参阅有关最常见的WordPress错误以及如何修复它们的指南。 如果这样做没有帮助,请按照我们的WordPress故障排除指南进行逐步诊断。

We hope this article helped you learn how to paste code snippets from the web into WordPress. Need some code snippets that you can try on your website? See our list of extremely useful tricks for the WordPress functions file.

我们希望本文能帮助您学习如何将Web上的代码片段粘贴到WordPress中。 需要一些可在您的网站上尝试的代码段吗? 请参阅我们的WordPress函数文件极其有用的技巧列表。

If you liked this article, then please subscribe to our YouTube Channel for WordPress video tutorials. You can also find us on Twitter and Facebook.

如果您喜欢这篇文章,请订阅我们的YouTube频道 WordPress视频教程。 您也可以在TwitterFacebook上找到我们。

翻译自: https://www.wpbeginner.com/beginners-guide/beginners-guide-to-pasting-snippets-from-the-web-into-wordpress/

ad网络粘贴

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值