Viewing 6 replies - 1 through 6 (of 6 total)
  • Hi @akhilser. The code you linked to will create a new image in the Media Library every single time the code is executed. If the code is inside a plugin file, then that will be on every single page request.

    When would you like an image to be created? That will determine where this code should go.

    If you only want to run the code once, then maybe a plugin like Code Snippets would be helpful as it lets you create a snippet with “Only run once” as an option.

    https://www.ads-software.com/plugins/code-snippets/

    Thread Starter akhilser

    (@akhilser)

    i would like to create the image only 1 time when there is new img url passed or something like that . Plugins are decrease optimization and speed so thats why im trying pure functions. Is there is any hooks available . Thanks for ur Help

    Perhaps you can implement a form and run the PHP code when the form is submitted:

    <?php
    if ( ! empty( $_POST['image-url'] ) ) {
    	$image_url = $_POST['image-url'];
    	// Put code that uploads URL in $image_url here
    }
    ?>
    <form action="" method="post">
    	<label for="image-url">Image URL:</label>
    	<input id="image-url" type="text" name="image-url" value="" />
    	<button type="submit">Upload</button>
    </form>
    Thread Starter akhilser

    (@akhilser)

    Thanks. I already did the form submit method so im trying to upload through image url

    if ( !function_exists('media_handle_upload') ) {
            require_once(ABSPATH . "wp-admin" . '/includes/image.php');
            require_once(ABSPATH . "wp-admin" . '/includes/file.php');
            require_once(ABSPATH . "wp-admin" . '/includes/media.php');
        }
    
        $url = "https://www.w3schools.com/css/img_fjords.jpg";
        $tmp = download_url( $url );
        if( is_wp_error( $tmp ) ){
            // download failed, handle error
        }
        $post_id = 0; // set 0 for no parent post id or simple attachment otherwise pass post id for include in post 
        $desc = "The WordPress Logo";
        $file_array = array();
    
        // Set variables for storage
        // fix file filename for query strings
        preg_match('/[^\?]+\.(jpg|jpe|jpeg|gif|png)/i', $url, $matches);
        $file_array['name'] = basename($matches[0]);
        $file_array['tmp_name'] = $tmp;
    
        // If error storing temporarily, unlink
        if ( is_wp_error( $tmp ) ) {
            @unlink($file_array['tmp_name']);
            $file_array['tmp_name'] = '';
        }
    
        // do the validation and storage stuff
        $id = media_handle_sideload( $file_array, $post_id, $desc );
    
        // If error storing permanently, unlink
        if ( is_wp_error($id) ) {
            @unlink($file_array['tmp_name']);
            return $id;
        }
    
        $src = wp_get_attachment_url( $id );

    this is my code using this code each time i refresh the media library page the image creating multiple times.

    You need to put that code inside the form submit if() so that it doesn’t run on every page request.

    Thread Starter akhilser

    (@akhilser)

    There sould be no form or any upload field . directly upload with function only

    • This reply was modified 2 years, 4 months ago by akhilser.
Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Image upload Via URL progromatically to Media Library’ is closed to new replies.