0

I am working on uploading an image to the media library and then attaching it to the featured image. I put the code below in my functions.php file and submitted a JSON file through postman, but for some reason it uploaded thousands of times until i had to kill the process and remove the code to get it to stop. It is uploading to the media library but keeps creating images. I have no loop in my code. I was just testing the code below to see if i could get it to run once. I am still learning how Wordpress works. Sorry if these are dumb questions.

  1. Does functions.php continuously execute while Wordpress is running?
  2. Should this be in functions.php file and how do i get it to execute only once?
    $url = 'http://localhost/download.png';
    $request = wp_remote_post(
        'http://localhost/wordpress/wp-json/wp/v2/media',
        array(
            'headers' => array(
                'Authorization' => 'Basic ' . base64_encode( 'api-user:xxxx xxxx xxxx xxxx xxxx xxxx' ),
                'Content-Disposition' => 'attachment; filename="' . basename( $url ) . '"',
                'Content-Type: ' . wp_get_image_mime( $url ),
            ),
            'body' => file_get_contents( $url )
        )
    );
    
    if( 'Created' === wp_remote_retrieve_response_message( $request ) ) {
        $body = json_decode( wp_remote_retrieve_body( $request ) );
        $featured_image_id = $body->id;
    }
3
  • "I put the code below in my functions.php file" - did you put it just like that? Because you should instead put it in a function and call the function only when necessary (when you need to upload the image). And what do you mean by "submitted a JSON file through postman" - what JSON file is that? What does it contain and maybe you should add a screenshot of your Postman request.
    – Sally CJ
    Commented Mar 10, 2023 at 10:24
  • Thanks Sally, yes i put it in exactly like how it is laid out. I think i don't fully understand how functions.php works. After your comment, i am assuming that functions.php executes and putting the code in a function and using an add_action() will solve the problem. thanks for clearing that up. I will try it. i am still trying to understand hooks and how they work. its all trial and error at this point for me :)
    – user230659
    Commented Mar 10, 2023 at 15:59
  • I've posted a proper answer, and hopefully it helps you. :)
    – Sally CJ
    Commented Mar 11, 2023 at 5:30

2 Answers 2

0
  1. Does functions.php continuously execute while Wordpress is running?

Yes, it's run on each page load. You can learn more about the (theme) functions file at https://developer.wordpress.org/themes/basics/theme-functions/.

  1. Should this be in functions.php file and how do i get it to execute only once?

It can be put in that file, however, it should be run conditionally, e.g. put it in a function and then call the function from within your template file, or from within a hook. That's how you can get it to execute only once. (but whether it's actually going to be executed only once, depends on your code)

Example:

  • Code in the active theme's functions.php file:

    <?php
    /**
     * Your Theme functions and definitions
     *
     * @link https://developer.wordpress.org/themes/basics/theme-functions/
     */
    
    function my_upload_image_function() {
        // your code here which uploads the image
    }
    
  • Then in your template file, just call the function when necessary: my_upload_image_function().

  • Or you can hook the function on a specific action:

    add_action( 'hook_name', 'my_upload_image_function' );
    

So that's a brief info about the theme functions file, and now, this is about what your code is trying to do.

2
  • Thank you so much Sally. That is very helpful. I did add it to a function and i am calling it from add_action('rest_api_init', 'add_image_to_library'); I use rest_api_init hook because i am submitting this from Postman. My goal is to submit a JSON file from another server with C#. to the rest api and attaché the image to a custom post time. I added some debugging and even with the add_action() it is still looping through until i stop it and remove the code. i will check out rest_do_request and media_sideload_image . I will continue to dig :)
    – user230659
    Commented Mar 12, 2023 at 0:46
  • Oh noo, that's not how you should do it! 😁 (you should not hook the function on rest_api_init) Can you show a sample of that JSON file? And actually, can't you just upload the image directly via C#? (I.e. Make a remote/HTTP request from that another server to the /wp/v2/media endpoint on your WordPress site.) Why must you "submit a JSON"?
    – Sally CJ
    Commented Mar 12, 2023 at 3:22
0

Trying to get use to working on this site too. Lost the post for a few days, and didn't register properly i guess. I can't seem to get back to your last response.

The json file is for a custom post type. The custom post works fine. I can create a new custom post with updating custom fields using rest api. In addition i am looking to upload the image for that post, attach the image to the featured image at time of creation of the new custom post. Honestly, i am still learning the rest api and wordpress. In looking at /wp/v2/media and researching code. i only see samples for curl or wp_remote_post(). i pulled everything out of my functions php and it seems to be doing the same thing with wp_remote_post(). it will upload thousands of the image and won't stop. It seems to be getting stuck on wp_remote_post() and never getting past that. I put in debugging and it never gets out of the remote function. If i shouldn't call it on rest_api_init. I will look at the end point again to see if i can make it work directly. Just havn't been successful.

1
  • Hey there, sorry, only today I remembered this question and then saw your "answer" - you should instead edit your original post and not posting this answer.. 😅 anyway, have you now already solved the issue in question? If so, how? Could you please edit your answer and let us know your solution? ( And accept your answer later on :) )
    – Sally CJ
    Commented Mar 29, 2023 at 14:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.