Once you get URL of the image from Instagram and you know id
of the post, you can use use the function from your question. You have to modify the code for the only if
statement in it:
if( wp_mkdir_p( $upload_dir[ 'path' ] ) ) {
$file = $upload_dir[ 'path' ] . "https://wordpress.stackexchange.com/" . $filename;
} else {
$file = $upload_dir[ 'basedir' ] . "https://wordpress.stackexchange.com/" . $filename;
}
This function will upload the image to the media library and set it as a featured image for your post. Tested!
Or, based on suggestion by @TomJNowell from his comment, use this code:
$attach_id = media_sideload_image( $image_url, $post_id, 'Instagram image', 'id' );
set_post_thumbnail( $post_id, $attach_id );
Related Posts:
- Plugin update error message
- Redirect to another page using contact form 7? [closed]
- How do I call wp_get_current_user() in a plugin when plugins are loaded before pluggable.php?
- Prevent publishing the post before setting a featured image?
- Keep one user logged for a year?
- Customizing subject in comment notification e-mails
- if plugin is active? check if plugin is enabled or not?
- WP showing “warning: call_user_func_array()”, What to do?
- Adding a text domain to every __(), _e() and __n() gettext call without a plan to create own translations
- How to Loop Plugin Option Field Set?
- Making plugin unique to not conflict with plugins with the same name
- Can we completely remove the WordPress Sitemaps (WordPress 5.5)?
- Saving Plugin settings to the database
- Set a User as Author of all ‘New Posts’ posted
- Post source link plugin – small modification
- Change destination author link
- How to turn off email you receive when registered?
- Custom Widget outputs the input but doesn’t save anything inside the textarea
- How can i change an image’s author?
- HELP: Integrating WP with raw PHP code once published button is hit
- How to redirect Old Post URL to new Post and keep Old post Comments?
- Modifying Footnote Plugin for Descriptive Title Attributes
- I would like to use create a function in my custom plugin to tell WP to use a different header
- New Plugin: Post update pushes a copy as a revision
- Set WordPress Featured Image For All Post Slugs Matching Image File Name in Specified Directory
- How to Replace Words with Hypertext Link But Ignore Previously Existed Links?
- bulk change of image setting “link to” to “link to: image url “
- Display a text message if the field is not found and not if found
- Scope for PHP Variables Assigned in functions.php or a plugin
- How to get post URL in the_content filter?
- Add a plugin before main container
- How to get subscription key or id using Woocommerce Subscriptions [closed]
- Missing argument 3 for wp_register_sidebar_widget()
- WordPress Plugin Page is Loading in Admin Content Container Instead of Separate Page
- add_query_arg not working
- Shortcode button dosent work for all posts. Work for first post only
- Executing my function once on a specific time
- How to stop or remove an action being called inside a function of an extended class
- wordpress plugin is not activating from widget
- Theme my Login plugin, how to update fields
- Edit Yoast SEO breadcrumbs output [closed]
- Date calculations from 2 custom fields
- How to enable specific plugin only based around shop manager role?
- Override Plugin Script Fucnction in WordPress
- Adding a new field to the address field type in gravity forms
- Replacing a plugin function with a custom renamed function doesn’t work
- Nested shortcode functions accessing variable
- How to call WordPress function other files
- All sites themes functions.php have been changed
- Customize permalink wordpress category id
- Conditional attributes and logic per product category
- WP Query. Is there a maximum size?
- WordPress get_avatar function not correct working
- What is @Action in WordPress?
- Load Javascript from Plugin in One Page Only?
- Is there a best practice remediation for PhpStorm’s warning that void function the_post_thumbnail is used?
- Checkbox show / hide output result
- Custom Logo Link WordPress
- How to convert Currency from USD to other IP Based currency in Php function
- how to validate input field in wordpress generated with wpform plugin
- add_rewrite_rule to load different page, without changing URL in browser
- implement custom roles in custom plugin
- WordPress Custom Hook with Class method
- How to get current cart values using WC_Shortcodes?
- Attempting to list all product categories and the price range of all products within them
- Allow a particular user to access a particular plugin?
- WP Function does not trigger on Webhook API Call
- Cookie value changes back to previous value after changing
- Creating an array from form inputs before it is posted to the options database
- Post source link plugin – small modification
- Is there any way to sync Facebook Comments and with comments on WordPress website?
- Share my WordPress plugin for updating how?
- Custom Plugin: How to Include Install Buttons of other 3rd Party Plugins?
- adding dynamic/multiple slug values in ‘option_none_value’
- How to create algorithm for ordering posts in WordPress?
- Submitting form to PHP
- Adding discount functionality to the cart
- How to assign a specific service to a specific provider based on location
- Precheck fields when I add a new post
- front end editor creation for Restropress plug in – displaying information from a WP admin area, on a different URL
- Get Shortcode output to database for static post_content
- Use AJAX to fetch Current Post Thumbnail for WordPress when Uploaded throughMedia Uploader Frontend
- working code, not working – Plugin Dev
- Easiest way to load/fire a handful of functions, IF checkbox is checked?
- Block plugin update possibilities (but not by hiding notifications)
- post id or permalink auto-incrementing number and reset everyday
- Change custom rewrite rule when permalink is updated/changed
- Is admin section completely customizable in terms of styling?
- Return function results within shortcode
- Configuring default woo commerce login with modal popup
- How to make the first letter of a post title uppercase, in a plugin?
- Compare Ajax Data Results
- How can I make the search bar in my wordpress site search all of the pages rather than just the blog posts?
- Soflyy WP All Import Custom File Download Issue
- register_activation_hook doesn’t fire at all
- Migrating custom php we wrote from functions.php into a site-specific plugin
- Nested DIV’s across functions in PHP, do not seem to work
- My ajax request don´t work and return 0
- WordPress environment not loading properly
- Populate select option with JSON file