mysql - Trying to upload featured image to wordpress databse with php code

34

I'm trying to upload a external image to my Wordpress database with a PHPcode

This the code I try to use for it:

<?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 );

but when I try to run it I have this error code:

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

Does anyone have an idea how to fix this error?

487

Answer

Solution:

The upload form might look like this:

<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>

The code to save the attachment:

<?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.
}

People are also looking for solutions to the problem: php - Redis store values under one key or multiple keys

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.