# Adding Media to Content **Adding media** means inserting images, galleries, audio, video, and file downloads into posts and pages using media blocks that pull from the WordPress **Media Library**. ## Why it matters Media makes content richer but adds weight and attack surface — large unoptimized images slow pages, and unrestricted uploads can be abused to store malicious files. Using the built-in blocks, alt text, and featured images keeps content accessible, performant, and consistent with the theme. Every uploaded file becomes a publicly reachable URL under `wp-content/uploads/`. Because that directory is web-served, treating it as an untrusted staging area — not an execution path — is the difference between a media library and a malware drop point. Optimized, correctly-sized media also directly affects Core Web Vitals (LCP in particular), which influences both user experience and search ranking. ## How to do it 1. In the editor, add an **Image**, **Gallery**, **Audio**, **Video**, or **File** block (or drag a file directly into the editor). 2. Upload a new file or pick an existing one from the **Media Library**. 3. Set **alt text** for accessibility/SEO, choose an alignment and size, and optionally link the image. 4. For posts, set a **Featured Image** in the sidebar — used in listings and social previews. 5. Harden uploads by limiting allowed MIME types and keeping `uploads/` non-executable (see media/hardening notes). ![Gutenberg editor Image block with the Media Library modal open and the alt text field visible in the block sidebar](images/media-library-image-block.png) WordPress automatically generates multiple resized copies (`thumbnail`, `medium`, `large`, plus theme-registered sizes) on upload and emits a responsive `srcset` so browsers pick the smallest adequate file. You can control the intermediate sizes generated in code: ```php // functions.php — register a custom crop size for featured images add_theme_support('post-thumbnails'); add_image_size('card-thumb', 600, 400, true); // hard crop 600x400 ``` ## How it works When a file is uploaded, WordPress stores the original in the date-based `uploads/YYYY/MM/` folder, records it as an `attachment` post type in the database, and saves size metadata as post meta. The block editor references the attachment by ID, so re-cropping or replacing the file updates every place it appears. > [!TIP] > Prefer setting a **Featured Image** over hard-coding a hero image inside post content. Themes, RSS feeds, and Open Graph/Twitter-card tags read the featured image automatically, so one setting drives listings, social previews, and the single-post view consistently. ## Example ```php // Restrict which file types non-admins may upload (functions.php) add_filter('upload_mimes', function ($mimes) { unset($mimes['svg']); // block SVG to avoid script-in-SVG XSS return $mimes; }); ``` Cap the maximum upload size at the PHP layer so oversized files are rejected before they consume disk or memory: ```conf ; php.ini upload_max_filesize = 8M post_max_size = 10M max_file_uploads = 20 ``` ## Security considerations The `uploads/` directory is the single most abused path in WordPress. Defend it in depth: - **Disable PHP execution in uploads.** Even if an attacker plants a `.php` file, the server must refuse to run it. ```apache # .htaccess inside wp-content/uploads/ (Apache) Require all denied ``` ```nginx # nginx server block location ~* ^/wp-content/uploads/.*\.(php|phtml|phar)$ { deny all; } ``` - **Block SVG uploads** unless you sanitize them server-side — SVGs are XML and can embed `