usuario1797635
Hola chicos, comencé mi primer complemento en wordpress después de un poco de trabajo, me sorprendieron en la validación de campo.
El problema es que tengo un campo llamado "preix_author_url"
luego en mi complemento uso
add_action('save_post', 'my_function_name');
he creado un ejemplo de clase de validación
<?php
class validator {
public static function isUrl($str="") {
if(strlen($str) == 0)
return FALSE;
return preg_match('!^http(s)?://[\w-]+\.[\w-]+(\S+)?$!i',$str);
}
}
en "my_function_name()"
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']);
}
}
}
Ahora quiero mostrar un error en la página de publicación si la validación devuelve falso. Pero no entendí la forma de mostrar esos errores o notificaciones.
loQ
Así que después de un tiempo me di cuenta de esto por fin. ¡Prometo responderte en 20 minutos, pero fallé porque pensé que era así de fácil! ¡Descubrí después de buscar en todas partes que admin_notices no funcionará en el gancho save_post! Así que heres una buena solución con su problema.
//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);
}
}
}
¡Probé esto en mi sitio web personal y funcionó maravillosamente! ¡También he aprendido muchas cosas con esto!
-
Y tengo otro problema … tengo otros 2 campos ahora, ¿cómo mostrar esos errores? ¿No podemos recopilar todos los errores en una matriz y luego volcarlos en el bloque de errores?
– usuario1797635
5 de noviembre de 2012 a las 17:36