有没有办法强制用户在发布前编写页面标题?我找到了一个要求文章标题,但不要求页面标题的例子。
Require title for pages
从下载名为Force Post Title.
下面是一个插件,根据我们的评论,它的底部添加了一行(2行带有注释行)。
发生的情况是,一个小的jQuery脚本被添加到页面中Create Post/Page. 当用户单击提交按钮时,脚本将检查标题字段是否为空。
由于这是一个如此小的插件,您可以轻松地修改它并将其复制到您的函数中。php。这样,你就不必编辑现有的插件,一旦你以后更新它,它会让你头疼。
我还应该提到的是,我发现了一些可以在wp-includes/post.php, row 2489. 我做了一些快速测试,但没有任何结果
/*
Plugin Name: Force Post Title
Plugin URI: http://appinstore.com
Description: Forces user to assign a Title to a post before publishing
Author: Jatinder Pal Singh
Version: 0.1
Author URI: http://appinstore.com/
*/
function force_post_title_init()
{
wp_enqueue_script(\'jquery\');
}
function force_post_title()
{
echo "<script type=\'text/javascript\'>\\n";
echo "
jQuery(\'#publish\').click(function(){
var testervar = jQuery(\'[id^=\\"titlediv\\"]\')
.find(\'#title\');
if (testervar.val().length < 1)
{
jQuery(\'[id^=\\"titlediv\\"]\').css(\'background\', \'#F96\');
setTimeout(\\"jQuery(\'#ajax-loading\').css(\'visibility\', \'hidden\');\\", 100);
alert(\'POST TITLE is required\');
setTimeout(\\"jQuery(\'#publish\').removeClass(\'button-primary-disabled\');\\", 100);
return false;
}
});
";
echo "</script>\\n";
}
add_action(\'admin_init\', \'force_post_title_init\');
add_action(\'edit_form_advanced\', \'force_post_title\');
// Add this row below to get the same functionality for page creations.
add_action(\'edit_page_form\', \'force_post_title\');
我只是对@hampusn answer做了一些修改,以便更好地集成。而不是使用alert()
它在标题下放置了一个格式很好的框。
它用标准包装jQuery代码.ready()
作用
我还只想对某个自定义帖子类型执行此操作,所以我将此代码段保留在中,但您只需删除$post_type
检查未来的读者是否不需要这个。
最后,我将验证消息包装在_()
. 如果您想在main中重新分配它,理论上可以根据帖子类型自定义它if
陈述
function rtp_force_post_title_init()
{
wp_enqueue_script(\'jquery\');
}
function rtp_force_post_title( $post )
{
$post_type = get_post_type();
$validation_message = _("The title field must be filled out.");
if(\'exhibitor\' === $post_type) {
echo "<script type=\'text/javascript\'>\\n";
echo "
jQuery( document ).ready(function() {
jQuery(\'#publish\').click(function(){
var testervar = jQuery(\'[id^=\\"titlediv\\"]\').find(\'#title\');
if (testervar.val().length < 1)
{
setTimeout(\\"jQuery(\'#ajax-loading\').css(\'visibility\', \'hidden\');\\", 100);
var validator_snippet = \'<div style=\\"padding: 10px; color: #fff; margin-top: -3px; background: #F55E4F;\\">" . $validation_message . "</div>\';
jQuery(\'[id^=\\"titlediv\\"]\').find(\'#titlewrap\').append(validator_snippet);
setTimeout(\\"jQuery(\'#publish\').removeClass(\'button-primary-disabled\');\\", 100);
return false;
}
});
});\\n";
echo "</script>\\n";
}
}
add_action( \'admin_init\', \'rtp_force_post_title_init\' );
add_action( \'edit_form_after_title\', \'rtp_force_post_title\' );