Php wordpress自定义帖子类型中的字段验证和显示错误
大家好,我在wordpress中启动了我的第一个插件,在现场验证中我做了一些工作 问题是我的插件中有一个名为“preix\u author\u url”的字段Php wordpress自定义帖子类型中的字段验证和显示错误,php,wordpress,custom-post-type,Php,Wordpress,Custom Post Type,大家好,我在wordpress中启动了我的第一个插件,在现场验证中我做了一些工作 问题是我的插件中有一个名为“preix\u author\u url”的字段 add_action('save_post', 'my_function_name'); 我创建了一个验证类示例 <?php class validator { public static function isUrl($str = '') { if(strlen($str) == 0)
add_action('save_post', 'my_function_name');
我创建了一个验证类示例
<?php
class validator {
public static function isUrl($str = '') {
if(strlen($str) == 0)
return FALSE;
return preg_match('!^http(s)?://[\w-]+\.[\w-]+(\S+)?$!i',$str);
}
}
现在,如果validate返回false,我想在post页面中显示错误。但是我没有办法显示这些错误或通知 所以过了一会儿我终于明白了。我保证在20分钟内回到你身边,但我失败了,因为我认为这很容易!!我在到处搜索后发现,管理员通知在save_post hook上不起作用!这是一个很好的解决你问题的方法
//for action hooks
add_action('save_post', 'my_function_name');
$validator = new Validator();
// called after the redirect
add_action('admin_head-post.php', array(&$validator, 'add_plugin_notice'));
//change your my_function_name with this
function my_function_name() {
global $post;
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
return;
if (isset($_POST['post_type']) && $_POST['post_type'] == 'wallpapers') {
require_once( WALLP_FILE_PATH . '/wallp-core/wallp-validator.php' );
$validate = new validator();
if (isset($_POST['preix_author_url'])) {
//if($validate->isUrl($_POST['preix_author_url']))
//update_post_meta(
//$post->ID, 'preix_author_url', $_POST['preix_author_url']);
$validator = new Validator();
if (!$validator->isUrl($_POST['preix_author_url'])) {
$validator->update_option(1);
return;
} else {
update_post_meta(
$post->ID,
'preix_author_url', $_POST['preix_author_url']);
}
}
}
}
//ive also revised your class
class Validator {
//new isUrl validation
//better use filter_var than preg_match
function isUrl($str = '') {
if (filter_var($str, FILTER_VALIDATE_URL) === FALSE) {
return FALSE;
} else {
return TRUE;
}
}
//You can change the error message here.
//This for your your admin_notices hook
function show_error() {
echo '<div class="error">
<p>Error Found!!</p>
</div>';
}
//update option when admin_notices is needed or not
function update_option($val) {
update_option('display_my_admin_message', $val);
}
//function to use for your admin notice
function add_plugin_notice() {
if (get_option('display_my_admin_message') == 1) {
// check whether to display the message
add_action('admin_notices', array(&$this, 'show_error'));
// turn off the message
update_option('display_my_admin_message', 0);
}
}
}
//用于操作挂钩
添加操作(“保存帖子”、“我的函数名称”);
$validator=新的验证器();
//重定向后调用
add_action('admin_head-post.php',array(&$validator,'add_plugin_notice'));
//用此更改您的my_函数名称
函数my_函数名称(){
全球$员额;
if(已定义('DOING_AUTOSAVE')&&DOING_AUTOSAVE)
返回;
如果(设置($\u POST['POST\u type'])和&$\u POST['POST\u type']=='wallpers'){
需要一次(WALLP文件路径'/wallpcore/wallpvalidator.php');
$validate=新的验证器();
如果(isset($\u POST['preix\u author\u url'])){
//如果($validate->isUrl($\u POST['preix\u author\u url']))
//更新\u post\u meta(
//$post->ID,$preix_author_url',$u post['preix_author_url');
$validator=新的验证器();
如果(!$validator->isUrl($\u POST['preix\u author\u url'])){
$validator->update_选项(1);
返回;
}否则{
更新\u post\u meta(
$post->ID,
‘preix_author_url’,$_POST[‘preix_author_url');
}
}
}
}
//我还修改了你的课程
类验证器{
//新的isUrl验证
//使用过滤器变量比使用预匹配更好
函数isUrl($str=''){
if(filter\u var($str,filter\u VALIDATE\u URL)==FALSE){
返回FALSE;
}否则{
返回TRUE;
}
}
//您可以在此处更改错误消息。
//这是你的管理员通知钩子
函数show_error(){
回声'
发现错误
';
}
//是否需要管理员通知时更新选项
函数更新选项($val){
更新选项('display_my_admin_message',$val);
}
//用于管理通知的函数
函数添加插件通知(){
如果(获取选项('display_my_admin_message')==1{
//检查是否显示该消息
添加操作('admin_notices',数组(&$this,'show_error'));
//关掉留言
更新选项(“显示我的管理员消息”,0);
}
}
}
我在我的个人网站上试过,效果很好!!我也从中学到了很多东西 我还有一个问题。。我还有另外两个字段,如何显示这些错误?我们不能收集数组中的所有错误,然后将其转储到错误块中吗?
//for action hooks
add_action('save_post', 'my_function_name');
$validator = new Validator();
// called after the redirect
add_action('admin_head-post.php', array(&$validator, 'add_plugin_notice'));
//change your my_function_name with this
function my_function_name() {
global $post;
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
return;
if (isset($_POST['post_type']) && $_POST['post_type'] == 'wallpapers') {
require_once( WALLP_FILE_PATH . '/wallp-core/wallp-validator.php' );
$validate = new validator();
if (isset($_POST['preix_author_url'])) {
//if($validate->isUrl($_POST['preix_author_url']))
//update_post_meta(
//$post->ID, 'preix_author_url', $_POST['preix_author_url']);
$validator = new Validator();
if (!$validator->isUrl($_POST['preix_author_url'])) {
$validator->update_option(1);
return;
} else {
update_post_meta(
$post->ID,
'preix_author_url', $_POST['preix_author_url']);
}
}
}
}
//ive also revised your class
class Validator {
//new isUrl validation
//better use filter_var than preg_match
function isUrl($str = '') {
if (filter_var($str, FILTER_VALIDATE_URL) === FALSE) {
return FALSE;
} else {
return TRUE;
}
}
//You can change the error message here.
//This for your your admin_notices hook
function show_error() {
echo '<div class="error">
<p>Error Found!!</p>
</div>';
}
//update option when admin_notices is needed or not
function update_option($val) {
update_option('display_my_admin_message', $val);
}
//function to use for your admin notice
function add_plugin_notice() {
if (get_option('display_my_admin_message') == 1) {
// check whether to display the message
add_action('admin_notices', array(&$this, 'show_error'));
// turn off the message
update_option('display_my_admin_message', 0);
}
}
}