attachment_fields_to_save

最后更新于:2021-11-25 20:31:15

apply_filters( ‘attachment_fields_to_save’, array $post, array $attachment )

Filters the attachment fields to be saved.

参数

$post

(array)
An array of post data.

$attachment

(array)
An array of attachment metadata.

源文件

文件: gc-admin/includes/media.php

View on Trac

function gcdocs_insert_custom_default_caption( $post ) {
    if ( 'image' === substr( $post['post_mime_type'], 0, 5 ) ) {
        if ( 0 === strlen( trim( $post['post_title'] ) ) ) {
            $post['post_title'] = preg_replace( '/.w+$/', '', basename( $post['guid'] ) );
            $post['errors']['post_title']['errors'][] = __( 'Empty Title filled from filename.' );
        }

        // captions are saved as the post_excerpt, so we check for it before overwriting
        // if no captions were provided by the user, we fill it with our default
        if ( 0 === strlen( trim( $post['post_excerpt'] ) ) ) {
            $post['post_excerpt'] = 'default caption';
        }
    }

    return $post;
}

add_filter( 'attachment_fields_to_save', 'gcdocs_insert_custom_default_caption' );

function insert_custom_default_caption($post, $attachment) {
if ( substr($post['post_mime_type'], 0, 5) == 'image' ) {
    if ( strlen(trim($post['post_title'])) == 0 ) {
        $post['post_title'] = preg_replace('/.w+$/', '', basename($post['guid']));
        $post['errors']['post_title']['errors'][] = __('Empty Title filled from filename.');
    }
    // captions are saved as the post_excerpt, so we check for it before overwriting
    // if no captions were provided by the user, we fill it with our default
    if ( strlen(trim($post['post_excerpt'])) == 0 ) {
        $post['post_excerpt'] = 'default caption';
    }
}

return $post;
}

add_filter('attachment_fields_to_save', 'insert_custom_default_caption', 10, 2);