search
HomeBackend DevelopmentPHP TutorialDetailed explanation of the shortcode function in WordPress, wordpressshortcode_PHP tutorial

Detailed explanation of the use of shortcode shortcode function in WordPress, wordpressshortcode

WordPress has added a shortcode (short code) API starting from version 2.5, similar to BBCode on BBS. Shortcode can also easily add functions to articles or pages, and shortcode is more flexible and powerful than BBCode. Next, Kayo will introduce shortcode to you.

1. Shortcode Introduction
Shortcode allows developers to generate content by creating macro content in the form of functions. Perhaps this concept seems a bit vague, but in fact it is a very simple and practical function, as long as you can write basic PHP functions, that is Shortcode can be used. The following will use practical examples to illustrate the use of shortcode.

2.shortcode form
Shortcode supports closed tags and self-closing (automatically closed) tags, and supports the use of parameters within tags. As for the specific form of shortcode, it depends on how the developer writes the shortcode.

[myshortcode]Some Content[/myshortcode] // 封闭标签
[myshortcode] // 自闭标签
[myshortcode title="example"] // 带有一个参数的自闭标签
[myshortcode]<p><a href="#"><span>内容</span></a></p>[/myshortcode] // 标签内可以填写文本或 HTML
[myshortcode]Content [myshortcodesecond] more content[/myshortcodesecond] // 也可以嵌套使用标签

Three.shortcode examples
Before using shortcode, you must first define shortcode in the theme’s functions.php file, for example:

function myshortcode_function($atts, $content = null){ // $atts 代表了 shortcode 的各个参数,$content 为标签内的内容
 
 extract(shortcode_atts(array( // 使用 extract 函数解析标签内的参数
 "title" => '标题' // 给参数赋默认值,下面直接调用 $ 加上参数名输出参数值
 ), $atts));
 // 返回内容
 return '<div class="myshortcode">
    <h3 id="title">'. $title .'</h3>
    <p>
     '. $content .'
    </p>
   </div>';
}
 
add_shortcode("msc", "myshortcode_function"); // 注册该 shortcode,以后使用 [msc] 标签调用该 shortcode

Add the above code to functions.php, and a simple shortcode will be created. We can call the shortcode through the [msc][/msc] tag, such as:

[msc title="欢迎"]这是独立博客 Kayo's Melody ,欢迎来到本博客[/msc]

Enter the above call in the article or page content, and you can output a welcome statement at the corresponding location. Define the corresponding CSS in style.css to give the short code a style.

Kayo briefly introduced the shortcode function of WordPress, mainly introducing the main concepts and usage of shortcode. In this article, Kayo will introduce the more important APIs in shortcode in more detail, hoping to help you develop more complex shortcodes.

4. Function add_shortcode

This function is used to register a shortcode. It has two parameters: the shortcode name and the shortcode processing function name. Quoting the above example:

function myshortcode_function($atts, $content = null){ // $atts 代表了 shortcode 的各个参数,$content 为标签内的内容
 
 extract(shortcode_atts(array( // 使用 extract 函数解析标签内的参数
 "title" => '标题' // 给参数赋默认值,下面直接调用 $ 加上参数名输出参数值
 ), $atts));
 // 返回内容
 return '<div class="myshortcode">
    <h3 id="title">'. $title .'</h3>
    <p>
     '. $content .'
    </p>
   </div>';
}
 
add_shortcode("msc", "myshortcode_function"); // 注册该 shortcode,以后使用 [msc] 标签调用该 shortcode

msc is the shortcode name. You can directly use the [msc][/msc] tag to call the shortcode when writing articles or pages in the future, and "myshortcode_function" is the name of the shortcode processing function in the example. The following focuses on analyzing the short code processing function.

5. Short code processing function

The shortcode processing function is the core of a shortcode. The shortcode processing function is similar to Flickr (WordPress filter). They all accept specific parameters and return certain results. The shortcode processor accepts two parameters, $attr and $content. $attr represents the various attribute parameters of shortcode, which is essentially an associative array, and $content represents the content in the shortcode tag.

As in the above example, if a call is made within the article, a welcome statement will be output:

[msc title="Welcome"]This is an independent blog Kayo's Melody, welcome to this blog[/msc]
When the article is displayed, WordPress will register all shortcodes, such as [msc] above. If there are attribute parameters and content in the shortcode, WordPress will separate and parse them, and then pass them to the shortcode processing function of the shortcode. After processing The result output by the processing function is displayed in the article instead of the original content of the shortcode.

At this time, the attribute parameters will be parsed and the associative array will be passed to $attr. For example, in the above example, the value of $attr is an associative array as follows:

array( 'title' => '欢迎')

When outputting results, you can directly use the form of $parameter name to output. In the case of the example, the attribute value is output as $title.

6.shortcode_atts

shortcode_atts is a very practical function that can set default values ​​for the attribute parameters you need and delete some unnecessary parameters.

shortcode_atts() contains two parameters $defaults_array and $atts. $attr is the attribute parameter collection. $defaults_array represents the default value of the attribute that needs to be set. For example:

$result = shortcode_atts( array(
 'title' => '新标题',
 'description' => '描述内容'
), $atts );
$attr 依然为

array( 'title' => '欢迎')

The result of $result is

array( 'title' => '新标题', 'description' => '描述标题')

'title' Since there are different values ​​in $defaults_array, 'title' is updated based on this new value, and the value of 'description' is also added. It is worth noting that shortcode_atts() will filter attributes that are not in $defaults_array. If there is an 'ohter' attribute in $attr, then the result of $result will still be the above result, because there is no 'other' in $defaults_array this property. Of course, the value mentioned here is only the default value of the attribute, and the actual output value is still the value filled in when shortcode is called.

7. Further parse attributes and set attribute default values

The

extract() function is used to further parse attributes and set attribute default values. One of its functions is to assign each attribute parameter value to a variable in the form of "$parameter name" and save it (such as $title in the example). It is easy to call. Use this function together with shortcode_atts() to output the results safely. For the specific use of this point, please refer to the example of the first point of this article "1. Function add_shortcode".

In addition, uppercase letters in attribute names will be converted to lowercase letters before being passed to the processing function, so it is recommended to use lowercase letters directly when writing attribute names.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1127865.htmlTechArticleDetailed explanation of the shortcode shortcode function in WordPress. WordPressshortcode WordPress has added a shortcode (short code) starting from version 2.5. Code) API, similar to BBCode on BBS,...
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
如何解决wordpress标签错误问题如何解决wordpress标签错误问题Feb 03, 2023 pm 02:03 PM

wordpress标签错误的解决办法:1、找到并打开wordpress的“wp-includes”目录下的“class-wp.php”文件;2、修改内容为“$pathinfo = isset( $_SERVER['PATH_INFO'] )?mb_convert_encoding($_SERVER['PATH_INFO'],'utf-8','GBK') : '';”即可。

wordpress后台乱码怎么办wordpress后台乱码怎么办Feb 03, 2023 pm 01:48 PM

wordpress后台乱码的解决办法:1、在wordpress的“wp-admin”文件夹下找到“admin.header.php”文件;2、将“charset”属性值设置为“UTF-8”格式即可恢复正常。

WordPress设置独立的Description和KeywordsWordPress设置独立的Description和KeywordsFeb 21, 2023 am 11:14 AM

你下载的WordPress主题提供的keywords和description这两个meta标签一般都做得很差,或者根本就不提供,这样不利于SEO。本文将指导你如何给主页、分类、页面以及文章页添加单独的Description 和 Keywords。

wordpress乱码怎么办wordpress乱码怎么办Mar 09, 2023 am 09:13 AM

wordpress乱码的解决办法:1、修改“wp-config.php”文件里的“define(’DB_CHARSET’, ‘utf8′);”为“define(’DB_CHARSET’, ”);”;2、把新数据库的编码设置成“latin1_swedish_ci”;3、以uft8的格式导入备份的数据库文件即可。

wordpress进不去怎么办wordpress进不去怎么办Feb 23, 2023 am 09:41 AM

wordpress进不去的解决办法:1、把地址栏“wp-login.php”后面的参数删掉,然后重新输入密码登录;2、登录FTP,下载“pluggable.php”文件,然后找到“ADMIN_COOKIE_PATH”并将它替换为“SITECOOKIEPATH”即可。

wordpress是saas吗wordpress是saas吗Feb 21, 2023 am 10:40 AM

wordpress不是saas。SaaS是一种软件销售模式,它主要针对云端应用软件,而WordPress是一款CMS系统,它主要针对网站构建和管理。虽然WordPress可以作为SaaS提供服务,但它本质上不是一种SaaS应用。

wordpress是哪一年的wordpress是哪一年的Feb 01, 2023 am 10:26 AM

wordpress是2003年发布的;Matt于2003年5月27日宣布推出第一版WordPress,受到了社区的欢迎,它基于b2 Cafelog并有显著改进;WordPress的第一个版本包括全新的管理界面、模板、XHTML 1.1兼容模板、内容编辑器。

2023年最新WordPress视频教程推荐2023年最新WordPress视频教程推荐Oct 25, 2019 pm 01:12 PM

本次PHP中文网整合了相关的视频教程,中文手册,以及相关的精选文章安利给大家,统统免费!!!通过我们分享的视频,可随时随地免费观看教程视频,也不需要迅雷或者百度网盘下载了。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),