I ran into this problem and ended up creating a basic function to extract the “attached” media by URL from the body of the post (in my case a post of type document, but it should work with any kind of post):
function get_first_link_url($post_id) {
$content = get_post_field('post_content', $post_id);
if ($content == null) {
return null;
}
$dom = new DOMDocument();
$dom->loadHTML($content);
$links = $dom->getElementsByTagName("a");
if (count($links) == 0) {
return null;
}
$url = $links[0]->getAttribute('href');
return $url;
}
This only returns the URL (href) of the first tag in the post, but it could readily be modified to return all of them as an array.
Related Posts:
- Attach media to multiple posts
- Theming media inserts
- Get the first image from post content (eg.: hotlinked images)
- Possible to “Attach” images to multiple posts without inserting or uploading twice?
- Doubled titles when using All in One SEO with custom template
- How can you determine whether an image is merely attached or has actually been inserted into a post?
- Get attachments by user
- How to manipulate “add media” pop-up
- attachment.php code or tutorial
- Fetch wordpress video attachment with post ID
- WordPress Media Uploader in page template (On Front-end)
- Add attachment for all images in post
- Getting Different Size Of Attachment Images
- In creating a theme, how can i allow a user to change an image outside post/page content?
- How To Pick Custom Size Of Uploaded Image In Theme Via the_post_thumbnail();?
- wp.media issue with selected image
- Is There A WordPress Hook for Accessing Attachments for A Particular Page?
- WordPress creating images if uploaded image is greater than 960px on one side?
- Callback Function Not Firing On Selection In Media Uploader
- View list of all attachments on site
- How to make the media library searchable by image title
- How to load mediaelement.js in theme template?
- How would I get this to work – send to post from thick box
- How to store and retrieve the attachment alignment?
- Change default options on attachment page
- How to force unlink on attached/inserted images?
- Convert base64 string to WordPress attachment uploads multiple images instead of a single one
- How can I display and excerpt of all pages with a meta_key and meta_value on index.php?
- double page title [duplicate]
- WordPress Ajax Spitting out a page as a response?
- Using Customizer value in an external PHP file inside a theme
- Reliable method to detect mobiles
- get_the_excerpt() removes all p tags in page!
- Where to find this broken link in order to remove it
- Fail to install theme on development copy of WordPress on Webmatrix
- Using require_once for parent-directory not working but is working in sub-directories
- jQuery conflict in WordPress, works fine in HTML
- Woocommerce Theme Reviews / Comments
- Including some variables in function.php and echo them is several place of theme is not working
- captiva Theme Error
- WordPress doesn’t recognize submenus as so I can’t style them properly
- How to override wordpress native options with theme options
- How to prevent XSS attack in wordpress theme?
- How to get the Clicked ID and output that ID data in another page
- Protecting work on client’s web host
- Featured Image Module Randomly Disappears
- Replaceing _custom_background_cb
- Best strategy for providing theme options using customizer
- WordPress Custom Search By Alphabet and order by asc and sesc
- Best way to access variables in template markup
- what is do_action(); in wordpress? [duplicate]
- How To Customise WordPress Comment Submit Button?
- Automatically Add Caption Shortcode to Image with Caption Field Filled In
- Adding a space between next and previous post links
- Next post in category
- Next/Previous post navigation
- Primary Menu Showing All Pages With No Sub-Nav
- get_theme_mod not pulling css colors into header using wp_head
- Why WordPress gets old templates code instead of the last updated ones
- Duplicate WordPress site to subdirectory but use same db?
- Recent post in the middle of the content
- Customizer API way function is_customize_preview() works only in main page?
- page.php is not called when I load a Page
- How to figure out which wordpress theme a site was using?
- Menu order of parent menu from perspective of child menu
- Advantages/Disadvantages Using Theme Editor Instead of Pages
- Magnific Popup – Add Caption to Images
- Best way to allow for extended use of WordPress functions inside single.php
- Why do WordPress developers use so many opening and closing PHP tags when developing themes? [duplicate]
- Create image with imagepng() (fails at header)
- Mutlisite in wordpress is not working
- Can you create a custom page with content in the center already defined?
- How to retrieve images attached to post through media library?
- Issue on Getting WP Gallery Items In cpt-single.php As Attachments
- Data not saved WordPress Custom Admin Page
- Safe way to echo wp_trim_words
- Get all Custom Post Types
- Filter URL and shortcodes from the_excerpt
- Changes to template not showing up unless ?reset=1 added to URL
- Check if redirected from a specific page template
- Let users to add any number of input fields in the theme option page
- How to add drop symbol to parent nav items which have child
- Postname permalink page not found error
- How to migrate the menu from the site on my own theme in WordPress?
- Why is including all your files within a theme folder important?
- background_image support multiple image size?
- After theme change the menus have to be manually linked to the corresponding theme location
- how to make a new page affected by a theme’s index.php layout?
- Category page with posts from 2 different childcategories
- How to avoid Ajax in wordpress theme in making mulitple request?
- Custom CSS no getting applied
- Two theme options pages for a theme
- Import from HubSpot COS into WordPress 4?
- Theme Development for a Newbie
- controlling whether upload is attached to post or not
- child comments not displaying as nested
- How can I fix the even/odd child classes comments so that the comments are unaffected by the top-level even/odd?
- How do I get my theme scripts to load in a custom theme built from Bootstrap
- How to add custom field option with menu?
- Remove attached media