gpt4 book ai didi

Wordpress 验证标题

转载 作者:行者123 更新时间:2023-12-02 22:53:17 24 4
gpt4 key购买 nike

需要在我的自定义帖子类型上为 'supports' => array( 'title') 添加空白和已存在的验证。但我不想为此使用任何插件。提前致谢。

add_action( 'admin_notices', 'custom_error_notice' );
function custom_error_notice(){
global $current_screen, $post;
if ( $current_screen->parent_base == 'edit' ){
if((!$post->post_name) && $_GET['post']) {
wp_redirect(admin_url('post-new.php?empty=1'));
}
if($_GET['empty']) echo '<div class="error"><p>Warning - Please fill up all fields correctly!</p></div>';
}
}

但这无法正常工作。

最佳答案

这可能对您有帮助:-

add_action('save_post', 'album_save_post', 10, 2);

function album_save_post( $album_id, $album ) {

if( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE || $album->post_type != 'music_album') return;

// echo '<pre>';
// print_r($album);
// echo '</pre>';
// die();

$errors = array();

// Validation filters
$title = $album->post_title;
if ( ! $title ) {
$errors['title'] = "The title is required";
}

// if we have errors lets setup some messages
if (! empty($errors)) {

// we must remove this action or it will loop for ever
remove_action('save_post', 'album_save_post');

// save the errors as option
update_option('album_errors', $errors);

// Change post from published to draft
$album->post_status = 'draft';

// update the post
wp_update_post( $album );

// we must add back this action
add_action('save_post', 'album_save_post');

// admin_notice is create by a $_GET['message'] with a number that wordpress uses to
// display the admin message so we will add a filter for replacing default admin message with a redirect
add_filter( 'redirect_post_location', 'album_post_redirect_filter' );
}
}

function album_post_redirect_filter( $location ) {
// remove $_GET['message']
$location = remove_query_arg( 'message', $location );

// add our new query sting
$location = add_query_arg( 'album', 'error', $location );

// return the location query string
return $location;
}

// Add new admin message
add_action( 'admin_notices', 'album_post_error_admin_message' );

function album_post_error_admin_message() {
if ( isset( $_GET['album'] ) && $_GET['album'] == 'error' ) {
// lets get the errors from the option album_errors
$errors = get_option('album_errors');

// now delete the option album errors
delete_option('album_errors');

$display = '<div id="notice" class="error"><ul>';

// Because we are storing as an array we should loop through them
foreach ( $errors as $error ) {
$display .= '<li>' . $error . '</li>';
}

$display .= '</ul></div>';

// finally echo out our display
echo $display;

// add some jQuery
?>
<script>
jQuery(function($) {
$("#title").css({"border": "1px solid red"})
});
</script>
<?php
}
}

关于Wordpress 验证标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23514871/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com