wp_insert_attachment()
Проверено на WordPress 6.9, обновлено Источник: WordPress Developer Resources.
Сигнатура
wp_insert_attachment( string|array $args, string|false $file = false, int $parent_post_id, bool $wp_error = false, bool $fire_after_hooks = true ): int|WP_Error
Описание
Если задать 'ID' в параметре $args, это будет означать обновление и попытку обновить вложение. Имя или заголовок вложения также можно задать, установив ключ 'post_name' или 'post_title'.
Даты вложения можно задать вручную, указав значения ключей 'post_date' и 'post_date_gmt'.
По умолчанию для комментариев используются стандартные настройки того, разрешены ли комментарии. Их можно закрыть вручную или оставить открытыми, задав значение ключа 'comment_status'.
См. alsowp_insert_post()
Оригинал (английский)
If you set the ‘ID’ in the $args parameter, it will mean that you are updating and attempt to update the attachment. You can also set the attachment name or title by setting the key ‘post_name’ or ‘post_title’.
You can set the dates for the attachment manually by setting the ‘post_date’ and ‘post_date_gmt’ keys’ values.
By default, the comments will use the default settings for whether the comments are allowed. You can close them manually or keep them open by setting the value for the ‘comment_status’ key.
Параметры
$args
string|array
обязательный
$file
string|false
необязательный
= false
$parent_post_id
int
необязательный
$wp_error
bool
необязательный
= false
$fire_after_hooks
bool
необязательный
= true
Возвращаемое значение
int|WP_Error
Исходный код
wp-includes/post.php:6651
function wp_insert_attachment( $args, $file = false, $parent_post_id = 0, $wp_error = false, $fire_after_hooks = true ) {
$defaults = array(
'file' => $file,
'post_parent' => 0,
);
$data = wp_parse_args( $args, $defaults );
if ( ! empty( $parent_post_id ) ) {
$data['post_parent'] = $parent_post_id;
}
$data['post_type'] = 'attachment';
return wp_insert_post( $data, $wp_error, $fire_after_hooks );
}
История изменений
| Версия | Описание |
|---|---|
| 5.6.0 | Added the $fire_after_hooks parameter. |
| 4.7.0 | Added the $wp_error parameter to allow a WP_Error to be returned on failure. |
| 2.0.0 | Introduced. |

