2015-05-04 111 views
0

在WP 4.2中打開添加新帖子頁面並且沒有編輯任何字段,單擊「發佈」按鈕頁面被提交併重新打開,但沒有任何字段被標記爲紅色背景顏色根據需要。 如何製作它?看起來,這是這個頁面的原始行爲。 我希望它能夠像類別編輯器一樣工作,在類似情況下,字段名稱會根據需要用紅色背景色標記。在wordpress中添加添加新帖子頁面

此外,我使用register_post_type函數添加了幾個字段到新郵政頁面,我也希望根據需要用紅色背景色標記。這是最好的辦法嗎?

謝謝!

+0

試試這個[plugin](https://wordpress.org/plugins/mandatory-fields/) –

回答

0

您可以使用下面的代碼由你自己打電話給你的jQuery驗證,

在你的主題添加在主題的functions.phpcustom.js這個代碼js文件夾

add_action('admin_print_scripts-post-new.php', 'custom_admin_script', 11); 
add_action('admin_print_scripts-post.php', 'custom_admin_script', 11); 

    function custom_admin_script() { 
     global $post_type; 
     // Post type = "post" or "page" you can load script wheater it is post or page , if page then change 'post' to 'page' in below condition 

     if('post' == $post_type) 
     wp_enqueue_script('custom-admin-script', get_stylesheet_directory_uri() . '/js/custom.js'); 
    } 

custom.js

jQuery(document).ready(function ($) { 

    if ($("#post").length > 0) { 

     var frm = true; 

     $("#publish").click(function() { 

      var title = $("#title").val(); 
      var excerpt = $("#excerpt").val(); 

      // your custom field variable goes here 

      if (jQuery.trim(title) == "" || jQuery.trim(excerpt) == "") { 
       frm = false; 
      } 

      if (frm == false) { 
       $("#publish").prop('disabled', true); 
      } else { 
       $("#publish").prop('disabled', false); 
      } 
     }); 

    } 
}); 
+0

感謝您的幫助! 插件必填字段是有用的,但似乎它不適用於使用register_post_type函數創建的自定義類型。 我會嘗試編輯它爲我的情況... 此外,RTF編輯器(CKEditor在我的情況下,因爲我有「CKEditor for WordPress」插件安裝)的id和名稱值的問題,以獲得其值(code#) [code] $(「#post_content」)。val(); ? 至於在我的情況下的JS腳本,我不需要禁用「發佈按鈕,我更需要停止提交頁面。如果有這種方式? –

+0

是否要檢查發佈內容區域爲空?你還想知道顯示編輯器的'post content'區域的'ID'? – Noman

+0

還必須設置發佈按鈕被禁用,直到所有字段都不是空的,如果你不這樣做,那麼你的驗證是無用的 – Noman

相關問題