配置wordpress:自定义简码(wordpress 6.2)

发布时间 2023-05-20 22:07:13作者: 刘宏缔的架构森林

一,添加php代码的例子:

1,代码:

// 说明:得到当前时间
function lhdGetNowTime() {
	$now = date("Y-m-d H:i:s");
    return $now;
}
// 注册成简码,名字myNow
add_shortcode('myNow', 'lhdGetNowTime');

// 说明:获取完整URL
function curPageURL() { 
	$pageURL = 'http'; 
	if ($_SERVER["HTTPS"] == "on") {
		$pageURL .= "s"; 
	} 
	$pageURL .= "://"; 
	if ($_SERVER["SERVER_PORT"] != "80") { 
		$pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"]; 
	} else { 
		$pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"]; 
	} 
	return $pageURL;
}
// 注册成简码,名字myUrl
add_shortcode('myUrl', 'curPageURL');

如图:

把代码添加到functions.php中

2,在文章中引用简码:

当前是一个段落区块

二,测试效果

 

说明:刘宏缔的架构森林是一个专注架构的博客,地址:https://www.cnblogs.com/architectforest

         对应的源码可以访问这里获取: https://github.com/liuhongdi/
         或: https://gitee.com/liuhongdi

说明:作者:刘宏缔 邮箱: 371125307@qq.com

三,查看wordpress的版本: