城市简码_在两分钟内为WordPress创建自定义简码

本文介绍了如何在WordPress中创建自定义简码,以方便快速实现自定义功能。通过创建PHP代码,保存在独立文件中,然后在functions.php中引入,定义简码并添加到页面或帖子中,只需几分钟即可实现。简码是解决WordPress安全限制的一种解决方案,允许插入任何所需PHP代码。
摘要由CSDN通过智能技术生成

城市简码

I love shortcodes. If there’s a time to drop the childish quip, “Then why don’t you marry it?” — now would be the right time. That’s how much I love shortcodes in WordPress.

我喜欢简码。 如果有时间放下幼稚的玩笑,“那你为什么不嫁给它呢?” -现在是正确的时间。 这就是我非常喜欢WordPress中的短代码。

WordPress’s biggest strength can also be its biggest weakness. While it does have all kinds of powerful tools and security built in, making custom functions and features can be a huge pain.

WordPress的最大优势也可能是其最大的劣势。 尽管它确实内置了各种强大的工具和安全性,但是自定义功能和特性的创建可能会非常麻烦。

But that’s where shortcodes come in. Once you’re comfortable with creating a shortcode, you can create custom functionality in just minutes — your imagination is the only limit.

但这就是简码的来源。一旦您对创建简码感到满意,就可以在短短几分钟内创建自定义功能-想象力是唯一的限制。

摘要供将来参考 (Summary for Future Reference)

Because I hope you’ll bookmark this page and reference later, here’s a summary of what it takes to get a shortcode functioning:

因为我希望您可以将此页面添加为书签并在以后引用,所以下面是获得简码功能所需的摘要:

  1. Create the code you want

    创建所需的代码
  2. Save the code (usually in some place like the wp-content folder)

    保存代码(通常在wp-content文件夹等位置)
  3. Include the new file you saved within the functions.php file

    将您保存的新文件包括在functions.php文件中
  4. Reference your shortcode using add_shortcode(‘shortcode’, ‘function’)

    使用add_shortcode('shortcode','function')引用您的简码
  5. Place your shortcode wherever you want it

    将您的简码放在任何需要的地方

为何所有简码都喜欢? (Why All the Shortcode Love, Exactly?)

If you’ve ever had to do any kind of WordPress development, you’ve run into the situation where you need some custom functionality or a specialized design element. For security reasons, WordPress strips a lot of HTML tags and all PHP code out of posts and pages.

如果您曾经必须进行任何类型的WordPress开发,那么您会遇到需要一些自定义功能或专门设计元素的情况。 出于安全原因,WordPress从帖子和页面中剥离了许多HTML标记和所有 PHP代码。

This is usually for our own good, but it makes for a nightmare when you just want a simple design modification.

通常这是为了我们自己的利益,但是当您只想进行简单的设计修改时,这就会成为噩梦。

Shortcodes are the stop-gap. They allow you to inject any PHP or other code right where you want it. Once you’re comfortable creating the shortcode markup, you’ll never go back.

简码是权宜之计。 它们使您可以在所需的位置注入任何PHP或其他代码。 一旦您舒适地创建了简码标记,就永远不会回头。

So, let’s jump into creating a custom shortcode and build out a process that you can reference in the future when you’re ready to make your own.

因此,让我们跳入创建自定义的简码,并构建一个流程,供以后准备自己创建时参考。

步骤1:建立程式码 (Step 1: Create the Code)

This is where you need to create your functionality. I don’t know what you want to accomplish, but here’s a very simple example of a shortcode that allows me to inject a custom message into a post. This is just for demonstration purpose, but it should get your imagination fired up:

这是您需要创建功能的地方。 我不知道您想完成什么,但是这是一个简短的短代码示例,允许我将自定义消息注入到帖子中。 这只是出于演示目的,但它应该激发您的想象力:

[sourcecode language=”php”] <?php function some_random_code(){

[源代码语言=“ php”] <?php函数some_random_code(){

echo ‘Thanks for reading my posts!’; }// End some_random_code()

回声“谢谢阅读我的帖子!”; } //结尾some_random_code()

?> [/sourcecode]

?> [/源代码]

So we have a very simple little PHP function. You could literally put anything in this function that you want to execute on your site — a custom form, special HTML markup, or calls to an external database.

因此,我们有一个非常简单PHP小功能。 您可以从字面上将要在站点上执行的任何内容放入自定义表单,特殊HTML标记或对外部数据库的调用中。

步骤2:保存代码(但不保存在Functions.php文件中) (Step 2: Save the Code (But NOT in the Functions.php File))

While you can store the above code within the functions.php file, I would be setting you up for failure long-term if I actually advised it. I strongly recommend that you create a separate file for your own custom shortcodes. Call it whatever you like, and store it in the wp-content folder, or create a plugin (see my article on creating WordPress plugins).

尽管您可以将以上代码存储在functions.php文件中,但如果我确实建议的话,我将为您设置长期的故障处理能力。 我强烈建议您为自己的自定义短代码创建一个单独的文件。 随便叫它,然后将其存储在wp-content文件夹中,或者创建一个插件(请参阅有关创建WordPress插件的文章 )。

For this example, create a file named “my_custom_shortcodes.php” and save it within the wp-content folder. This folder is generally safe from WordPress upgrades and core updates that can overwrite your existing functions.php file. You can lose a lot of work fast if a client decides to update to the latest theme update or install a new theme without telling you. It’s a lot easier to just go in and recreate the next few steps.

对于此示例,创建一个名为“ my_custom_shortcodes.php”的文件,并将其保存在wp-content文件夹中。 从WordPress升级和核心更新(可能会覆盖现有的functions.php文件)开始,此文件夹通常是安全的。 如果客户在不通知您的情况下决定更新为最新主题更新或安装新主题,则可能会很快失去很多工作。 只需进入并重新创建接下来的几个步骤,就会容易得多。

步骤3:包括您的自定义PHP文件 (Step 3: Include Your Custom PHP File)

Now we need to tell WordPress where to find your custom shortcode file. Assuming you saved it in your wp-content folder, here’s what you need to add to your functions.php file:

现在,我们需要告诉WordPress在哪里可以找到您的自定义简码文件。 假设您将其保存在wp-content文件夹中,则需要将其添加到functions.php文件中:

[sourcecode language=”php”]

[源代码语言=“ php”]

include(WP_CONTENT_DIR . ‘/my_custom_shortcodes.php’);

include(WP_CONTENT_DIR。'/ my_custom_shortcodes.php');

[/sourcecode]

[/源代码]

Now, WordPress is aware of your content and will integrate it into its functionality. We’re 90% done at this point!

现在,WordPress已意识到您的内容并将其集成到其功能中。 至此,我们已完成90%!

步骤4:定义您的简码 (Step 4: Define Your Shortcode)

Before you can actually start using the shortcode throughout your site, you need to tell WordPress that it is, in fact, a real shortcode. Right below your code in Step 3 within your functions.php file, add the following:

在实际开始在整个站点中使用简码之前,您需要告诉WordPress实际上是真正的简码。 在functions.php文件中第3步中的代码下方,添加以下内容:

[sourcecode language=”php”]

[源代码语言=“ php”]

add_shortcode( ‘some_random_code_sc’, ‘some_random_code’ );

add_shortcode('some_random_code_sc','some_random_code');

[/sourcecode]

[/源代码]

Here’s what’s happening — the first part of the add_shortcode call is the name of the shortcode you want to use. This is what will be put in your posts and pages. The next argument is the name of the function. So here’s an example in more plain English:

这就是发生的情况– add_shortcode调用的第一部分是您要使用的简码的名称。 这就是将放置在您的帖子和页面中的内容。 下一个参数是函数的名称。 所以这是一个用更简单的英语写的例子:

[sourcecode language=”php”]

[源代码语言=“ php”]

add_shortcode( ‘shortcode_name’, ‘function_name’ );

add_shortcode('shortcode_name','function_name');

[/sourcecode]

[/源代码]

Quick note: I like to use the name of the function for my shortcode, but I add the “_sc” to the shortcode (for ShortCode) to help me track down my code if I want to edit it later.

快速说明:我想为我的短代码使用函数的名称,但是我在短代码中添加了“ _sc”(对于ShortCode),以帮助我在以后编辑代码时跟踪我的代码。

Here’s what my functions.php file looks like after I’ve added my two lines of code:

这是我添加两行代码后的functions.php文件的样子:

alt

步骤5:添加您的简码 (Step 5: Add Your Shortcode)

Now, all you need to do is add your shortcode to any post or page! Create a new post/page and add the shortcode to it, publish, and view the page.

现在,您需要做的就是将您的简码添加到任何帖子或页面中! 创建一个新的帖子/页面,并向其中添加简码,发布和查看页面。

alt

And you should see your code output on the published page:

并且您应该在发布的页面上看到代码输出:

alt

You did it! Now you can quickly create all kinds of custom functionality and integrate it into your WordPress site in minutes.

你做到了! 现在,您可以快速创建各种自定义功能,并在几分钟内将其集成到WordPress网站中。

How are you using shortcodes? Show off your work in the comments!

您如何使用简码? 在评论中炫耀您的作品!

翻译自: https://www.sitepoint.com/create-custom-shortcodes-for-wordpress-in-two-minutes/

城市简码

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值