How to change the wp_upload_dir() path in plugin

The upload directory and URL paths are stored in the options database. You can change them using update_option(): update_option( ‘upload_path’, ABSPATH . ‘/path/to/uploads’ ); update_option( ‘upload_path_url’, site_url( ‘/uploads/’ ) ); However, it’s best not to use this in a publiuc plugin, as it will cause numerous issues. We can instead hook into the filters that … Read more

Are uploads directories created on a schedule?

I believe by default these folders are created by user interaction, however, it’s possible that a plugin may be calling a function to create these monthly upload folders. Note that using this function will create a subfolder in your Uploads folder corresponding to the queried month (or current month, if no $time argument is provided), … Read more

A link (not in the post) to download a specific PDF file

The pdf is itself an attachment post so in wordpress template hierarchy we can create a template named pdf.php Then you can write following code in it which force download the pdf file. <?php if (have_posts()) : the_post(); $pdf_title = $post->post_title; $pdf_src = get_attached_file($post->ID ); $bytes = filesize( $pdf_src ); header(“Pragma: public”); // required header(“Expires: … Read more

Create a post from every image in my media gallery

If you have the image in your media library you can just loop trough them and create post via wp_insert_post. function import_post_from_imgs() { $images = get_posts(‘post_type=attachment&post_status=inherit&posts_per_page=-1’); // just a minimal security check if ( ! current_user_can(‘publish_posts’) ) return; if ( ! empty($images) ) { foreach ( $images as $image) { // prevent duplicate if for … Read more