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?
- How to add posts to custom menus?
- How to remove search bar from a wordpress theme? [closed]
- How do I check if a menu exists?
- How to concatenate inside the _e() function the right way?
- How to sanitize select box values in post meta?
- Why to check if function doesn’t exists in functions.php?
- Pages: frontpage.php, home.php, posts page etc
- How to choose which template to be used for multiple taxonomy query?
- How to programmatically bring back “excerpts” field in post editor in WP 3.1+
- How to set dimensions of the post thumbnails (featured images)
- Fatal error: Call to undefined function get_header()
- Add a theme via symlink
- Alterntives to BEM syntax that comply with WordPress coding standards? [closed]
- How to determine which registered sidebar area a custom widget is loaded into
- Translation-readiness of Koi theme
- What tags should be used for themes to show the type of layout?
- enqueue_style is not working
- Integrating Html5Boilerplate’s Builder into a WP Theme
- Including wp-blog-header.php from functions.php remote call?
- Administration Pages Styling
- What are non-printable characters doing in my theme?
- Theme settings keep getting reset/erased
- Custom button block doesn’t work
- How can i convert the figma design into wordpress [closed]
- A custom theme with support for multiple layouts
- WordPress Local Install Theme Folder Permission To Edit
- Lightweight framework [closed]
- Display div only on the HOMEPAGE
- WordPress is adding margins and padding to my custom menu?
- Keep sticky posts out of query unless they have featured image
- Following Web Performance Optimization techniques to output static and dynamic css
- PowerPress mobile media player
- How to control on which pages the Aldehyde theme’s main slider is shown?
- Internal Stylesheet in WordPress Theme development
- How to support letting users add their OWN logo to a custom theme?
- WordPress stylesheet isn’t being added
- Does code in function.php differ from theme to theme
- How can I optimize this code? [closed]
- Issue on Getting Custom post type Thumbnail’s URL
- Forms won’t submit
- How do I provide for multiple crops of the same image in a theme?
- Why WordPress AJAX returns undefined however it works fine when I add static url instead of dynamic function?
- Twenty Seventeen Pages Loop
- How to add pages in wordpress using codes?
- How does the loop know which post to view?
- How to remove permalinks links presents in each page of my site?
- How to use multiple check-box values to work in a function and insert values in database
- How to make a function occurs for one time?
- How to know if I am on 1st page
- How to create a robust and logic class naming system in WordPress theme developing?
- How to enable admin to upload multiple images to support header carousel slider theme in WordPress?
- how to change footer content for certain pages only
- function ‘theme_settings_page’ not found
- Should I use template files or just use conditional tags in index.php?
- Is there a way to create sections under “Colors” panel in the Theme Customizer?
- Need help with adding custom wordpress menu and sub-menu
- get currently showed author ID in theme functions.php
- WordPress Custom font not found
- WordPress Blog Page displaying nothing
- Full width thumbnail
- Customizer API way function is_customize_preview() works only in main page?
- using theme check plugin to remove waring and errors from my theme
- paragraph format in WYSIWYG on a custom theme?
- unable to display the image meta value as background
- editor style css and page template with and without sidebar
- Suppress the_content filter in a nested loop
- URL conflict with a ‘Single Page Layout’
- loading custom.js file after jquery is loaded
- How to determine if it is legal to remove credit link from theme?
- Display recent posts on front page
- How to create sub-menu in “Allure Real Estate Theme for Placester”?
- Creating image grid on products page in wordpress
- Insert gutenberg blocks into template
- How to make it so I can “use” template parts in (classic/full) site editor