2012-11-04 12 views
6

Ciao ragazzi ho iniziato il mio primo plugin in WordPress dopo poco lavoro mi sono colpito nel convalida del campo ..Campo di convalida e la visualizzazione di errore nel tipo di messaggio personalizzato wordpress

problema è che ho un campo chiamato "preix_author_url" poi nella mia plug-i utilizzare

add_action('save_post', 'my_function_name'); 

ho creato un esempio di classe di convalida

<?php 
class validator { 
    public static function isUrl($str = '') { 
     if(strlen($str) == 0) 
      return FALSE; 

     return preg_match('!^http(s)?://[\w-]+\.[\w-]+(\S+)?$!i',$str); 
    } 
} 

in "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']); 
       } 
      } 
     } 

Ora voglio mostrare errore in post page se convalidare restituire falso. Ma non ho avuto modo di visualizzare quegli errori o notifiche ..

risposta

9

Quindi, dopo un po 'ho capito questo finalmente. Prometto di tornare da te tra 20 minuti, ma ho fallito perché pensavo che fosse così facile !! Ho scoperto dopo aver cercato ovunque che admin_notices non funzionasse su save_post hook! Quindi ecco una buona soluzione con il tuo 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); 
     } 
    } 
} 

Ho provato questo nel mio sito web personale e ha funzionato perfettamente !! Ho anche imparato un sacco di cose con questo!

+0

ecco quello funzionante! – loQ

+0

Grazie finalmente al lavoro .. :) grazie ancora. – user1797635

+0

potresti anche svenderlo? grazie anche! – loQ

Problemi correlati