WordPressの投稿の内容が必要かどうかを知りたいのですが。私はユーザーがコンテンツなしで投稿を保存できるようにしたくありません。どうすればいいの?WordPressに投稿用のコンテンツが必要です
ありがとうございます!
WordPressの投稿の内容が必要かどうかを知りたいのですが。私はユーザーがコンテンツなしで投稿を保存できるようにしたくありません。どうすればいいの?WordPressに投稿用のコンテンツが必要です
ありがとうございます!
まず、管理アクション
add_action('admin_enqueue_scripts', array($this, 'my_admin_scripts'));
function my_admin_scripts($page) {
global $post;
if ($page == "post-new.php" OR $page == "post.php") {
wp_register_script('my-custom-admin-scripts', plugins_url('/js/my-admin-post.js',dirname(__FILE__)), array('jquery', 'jquery-ui-sortable') , null, true);
wp_enqueue_script('my-custom-admin-scripts');
}
}
にカスタムスクリプトを追加し、次のjsのコード(/js/my-admin-post.js)にはJQueryで必要とされるatributeを置く:
// JavaScript Document
jQuery(document).ready(function($) {
$('#title').attr('required', true);
$('#content').attr('required', true);
$('#_my_custom_field').attr('required', true);
});
あなたは何のコンテンツが存在しない場合しかし、あなたはドラフト状態にポストを強制することができ、PHPを使用して保存されているポストを防ぐことはできません。
function bb_47052258_check_post_status($post_id){
// Return if this is a revision post
if (wp_is_post_revision($post_id)){
return;
}
// Get the post
$post = get_post($post_id);
// (Optional) Return if the post is not a "post" post-type
if($post->post_type != 'post'){
return;
}
// Return if the post content is not an empty string
if($post->post_content !== ''){
return;
}
// Remove this action to prevent an infinite loop
remove_action('save_post', 'bb_47052258_check_post_status');
// Update the post status
wp_update_post(array(
'ID' => $post->ID,
'post_status' => 'draft'
));
// Add this action back again
add_action('save_post', 'bb_47052258_check_post_status');
}
// Initially add the action
add_action('save_post', 'bb_47052258_check_post_status');
Tユーザーが迂回する可能性のある完全な検証が必要ない場合は、JavaScriptで最も簡単になります。スクリプトを追加するには 'admin_enqueue_scripts'フックを使います。 – janh