自定义美化WordPress的wp_die()页面

 1个月前     235  

文章目录

wp_die 是 WordPress 的错误提示页面,也是对 PHP die 函数的增加函数,可以完全替代 die。

但是默认的页面样式可能不适合你的主题,也不好看,作为一个WordPress页面,当然是可以用钩子修改的,用到的钩子为“wp_die_handler”。

修改方法

老规矩直接上代码。

/**
 * wp_die_handler 钩子过滤器
 * 
 * @since 1.0.0
 * @author iowen 
 * @link https://www.iowen.cn/customizeandbeautifythewp_diepageofwordpress
 * @param mixed $die_handler
 * @return mixed
 */
function io_wp_die_handler_filter($die_handler){
	if (is_admin()) {
		return $die_handler;
	}
	return 'io_die_beauty_html';
}
add_filter( 'wp_die_handler', 'io_wp_die_handler_filter' );

/**
 * 重写wp_die页面模版
 * 
 * 内容样式请自行修改,保留die()函数
 * @since 1.0.0
 * @author iowen 
 * @link https://www.iowen.cn/customizeandbeautifythewp_diepageofwordpress
 * @param string $message
 * @param string $title
 * @param array $args
 */
function io_die_beauty_html($message, $title, $args){
	get_header();
	?>
	<main id="main" class="container my-5 py-3" role="main">
		<article class="die-error shadow-lg" style="background: #fff;padding: 20px;max-width: 500px;margin: auto;border-radius: 12px;text-align: center;">
			<header class="die-header">
				<h1 class="die-title text-hide"><?php echo $title; ?></h1>
			</header>
			<i class="iconfont icon-crying text-64"></i>
			<div class="die-content mt-4" style="background: #ffefef;color: #ca3f3f;border-radius: 8px;padding: 10px;">
				<?php echo $message ?>
			</div>
		</article>
	</main>
	<?php
	get_footer();
	die();
}

将上面的代码放到主题functions.php文件里,css样式可能需要自行调整

wp_die介绍

wp_die( string|WP_Error $message = string|int $title = string|array|int $args = array() )

终止 WordPress 执行并显示带有错误消息的 HTML 页面。

说明

该函数是对 die() PHP 函数的补充。不同之处在于会向用户显示 HTML 页面。建议仅在执行过程不应继续时使用该函数。不建议频繁调用此函数,而应尽量以无声或更优雅的方式处理更多错误。

作为一种速记方法,所需的 HTTP 响应代码可以整数形式传递给 $title 参数(默认标题将适用)或 $args 参数。

参数

$message string | WP_Error 可选
错误消息。如果这是 WP_Error 对象,而不是 Ajax 或 XML-RPC 请求,则使用 WP_Error 的错误消息。

默认:''

$title string | int 可选
错误标题。如果 $messageWP_Error 对象,则带有 'title' 键的错误数据可用于指定标题。
如果 $title 是整数,则将其视为响应代码。

默认:''

$args string | array | int 可选

用于控制行为的参数。如果 $args 是整数,则将其视为响应代码。

  • response int
    HTTP响应代码。Ajax请求默认为200,否则为500。
  • link_url string
    要包含链接的 URL。只能与 $link_text 结合使用。
    默认为空字符串。
  • link_text string
    要包含的链接的标签。只能与 $link_url 结合使用。
    默认为空字符串。
  • back_link bool
    是否包含返回链接。默认为 false。
  • text_direction string
    文本方向。这只在内部有用,当WordPress仍在加载并且站点的区域设置尚未设置时。接受 'rtl''ltr'
    默认值为is_rtl()的值。
  • charset string
    HTML输出的字符集。默认值 'utf-8'
  • code string
    要使用的错误代码。默认值为 'wp_die',如果$message是wp_error,则为主要错误代码。
  • exit bool
    完成后是否退出流程。默认为true。

默认:array()

wp_die_handlerwp_die() 提供的钩子,还有其他钩子

// Ajax 请求
apply_filters( 'wp_die_ajax_handler', callable $callback )

// non-Ajax, non-JSON, non-XML 请求
apply_filters( 'wp_die_handler', callable $callback )

// JSONP REST 请求
apply_filters( 'wp_die_jsonp_handler', callable $callback )

// JSON 请求
apply_filters( 'wp_die_json_handler', callable $callback )

// XML-RPC 请求
apply_filters( 'wp_die_xmlrpc_handler', callable $callback )

// XML 请求
apply_filters( 'wp_die_xml_handler', callable $callback )
版权声明:一为 发表于 1个月前,共 2620 字。
转载请注明:自定义美化WordPress的wp_die()页面 | 一为忆

您可能感兴趣的

暂无评论

暂无评论...