我正在尝试使用 PHP 代码将外部图像上传到我的 Wordpress 数据库
这是我尝试使用的代码:
<?php
include ("../wordpress/wp-includes/post.php");
include ("../wordpress/wp-includes/functions.php");
include ("../wordpress/wp-admin/includes/image.php");
// $filename should be the path to a file in the upload directory.
$filename = '../wordpress/wp-content/uploads/2016/02/220px-Smiley.svg.png';
// The ID of the post this attachment is for.
$parent_post_id = 22;
// Check the type of file. We'll use this as the 'post_mime_type'.
$filetype = wp_check_filetype( basename( $filename ), null );
// Get the path to the upload directory.
$wp_upload_dir = wp_upload_dir();
// Prepare an array of post data for the attachment.
$attachment = array(
'guid' => $wp_upload_dir['url'] . '/' . basename( $filename ),
'post_mime_type' => $filetype['type'],
'post_title' => preg_replace( '/\.[^.]+$/', '', basename( $filename ) ),
'post_content' => '',
'post_status' => 'inherit'
);
// Insert the attachment.
$attach_id = wp_insert_attachment( $attachment, $filename, $parent_post_id );
// Make sure that this file is included, as wp_generate_attachment_metadata() depends on it.
require_once( ABSPATH . '../wordpress/wp-admin/includes/image.php' );
// Generate the metadata for the attachment, and update the database record.
$attach_data = wp_generate_attachment_metadata( $attach_id, $filename );
wp_update_attachment_metadata( $attach_id, $attach_data );
set_post_thumbnail( $parent_post_id, $attach_id );
但是当我尝试运行它时,出现了这个错误代码:
Warning: require(ABSPATHWPINC/option.php): failed to open stream: No such file or directory in /home/sales/domains/lilopel.com/public_html/wouter/wordpress/wp-includes/functions.php on line 8
Warning: require(ABSPATHWPINC/option.php): failed to open stream: No such file or directory in /home/sales/domains/lilopel.com/public_html/wouter/wordpress/wp-includes/functions.php on line 8
Fatal error: require(): Failed opening required 'ABSPATHWPINC/option.php' (include_path='.:/usr/local/lib/php') in /home/sales/domains/lilopel.com/public_html/wouter/wordpress/wp-includes/functions.php on line 8
有谁知道如何解决这个错误?
最佳答案
上传表单可能如下所示:
<form id="featured_upload" method="post" action="#" enctype="multipart/form-data">
<input type="file" name="my_image_upload" id="my_image_upload" multiple="false" />
<input type="hidden" name="post_id" id="post_id" value="55" />
<?php wp_nonce_field( 'my_image_upload', 'my_image_upload_nonce' ); ?>
<input id="submit_my_image_upload" name="submit_my_image_upload" type="submit" value="Upload" />
</form>
保存附件的代码:
<?php
// Check that the nonce is valid, and the user can edit this post.
if (
isset( $_POST['my_image_upload_nonce'], $_POST['post_id'] )
&& wp_verify_nonce( $_POST['my_image_upload_nonce'], 'my_image_upload' )
&& current_user_can( 'edit_post', $_POST['post_id'] )
) {
// The nonce was valid and the user has the capabilities, it is safe to continue.
// These files need to be included as dependencies when on the front end.
require_once( ABSPATH . 'wp-admin/includes/image.php' );
require_once( ABSPATH . 'wp-admin/includes/file.php' );
require_once( ABSPATH . 'wp-admin/includes/media.php' );
// Let WordPress handle the upload.
// Remember, 'my_image_upload' is the name of our file input in our form above.
$attachment_id = media_handle_upload( 'my_image_upload', $_POST['post_id'] );
if ( is_wp_error( $attachment_id ) ) {
// There was an error uploading the image.
} else {
// The image was uploaded successfully!
}
} else {
// The security check failed, maybe show the user an error.
}
关于php - 尝试使用 php 代码将特色图片上传到 wordpress 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35157572/