treehouse : what would you like to learn today?
Web Design Web Development iOS Development

[Solved] Get the URL from the image I inserted in the HTML Editor.

  • I am using the Attachments Plugin (http://wordpress.org/extend/plugins/attachments) to use for a slideshow-like effect.

    Result: Process: 1 2 3

    Now what I'm trying to do is make it so the image I insert in the HTML editor (post_content) will link to 1 and the attachments from the plugin will apply to the rest. However, depending on the number of attachments I have, should reflect on the numbers outputted.

    Example (Let's say I have 4 attachments)
    Process: 1 (post_content image) 2 3 4 5 (attachment images)

    Here is the code I have so far that reflects the attachments. What I need help with is how to display the URL of the post_content image to apply to 1.


    <?php
    if( function_exists( 'attachments_get_attachments' ) )
    {
    $attachments = attachments_get_attachments();
    $total_attachments = count( $attachments );
    if( $total_attachments ) : ?>
    <ul class="process"><span>Process:</span>
    <?php for( $i=0; $i<$total_attachments; $i++ ) : ?>
    <li><a href="<?php echo $attachments[$i]['location']; ?>"><?php echo $i+2; ?></a></li>
    <?php endfor; ?>
    </ul><br>
    <?php endif; ?>
    <?php } ?>
  • I believe I have solved this. Not exactly sure if it will work overall since I only tested on one page. Perhaps more advanced WP developers can chime in.

    Here is the following code:

    <?php
    if( function_exists( 'attachments_get_attachments' ) )
    {
    $attachments = attachments_get_attachments();
    $total_attachments = count( $attachments );
    if( $total_attachments ) : ?>
    <ul class="process"><span>Process:</span>
    <?php
    if ( has_post_thumbnail()) {
    $large_image_url = wp_get_attachment_image_src( get_post_thumbnail_id(), 'full');
    echo '<li><a href="' . $large_image_url[0] . '">';
    echo '1</a>';
    }
    ?>
    <?php for( $i=0; $i<$total_attachments; $i++ ) : ?>
    <li><a href="<?php echo $attachments[$i]['location']; ?>"><?php echo $i+2; ?></a></li>
    <?php endfor; ?>
    </ul><br>
    <?php endif; ?>
    <?php } ?>
  • And it doesn't work. Without noticing the code right away, it uses the post_thubmnail, not the actual full image which is what I need.
  • Not a wordpress guy, but try this:
    <?php
    if( function_exists( 'attachments_get_attachments' ) )
    {
    $attachments = attachments_get_attachments();
    $total_attachments = count( $attachments );
    if( $total_attachments ) : ?>
    <ul class="process"><span>Process:</span>
    <?php
    if ( has_post_thumbnail()) {
    $large_image_url = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
    echo '<li><a href="' . $large_image_url[0] . '">';
    echo '1</a>';
    }
    ?>
    <?php for( $i=0; $i<$total_attachments; $i++ ) : ?>
    <li><a href="<?php echo $attachments[$i]['location']; ?>"><?php echo $i+2; ?></a></li>
    <?php endfor; ?>
    </ul><br>
    <?php endif; ?>
    <?php } ?>
  • @wolfcry911 What that does is give the permalink to the post not the image file (which is what I need). The following code below is what I believe to be the issue however I don't know how to call the image I inserted inside the editor.

    $large_image_url = wp_get_attachment_url( get_post_thumbnail_id('full') );
  • In case anyone doesn't understand. ALL I need is to get the URL of the image I inserted into the HTML editor in the backend. I believe in order to get that, I need to use wp_get_attachment_src however I'm not sure how to go about getting the ID dynamically.

    End result:
    image
  • Using a function, I believe I have solved the issue.

    I placed this code in my functions.php file that allows me to grab the very first image in a post.
    // CREATES A FUNCTION TO GRAB FIRST IMAGE OF A POST
    function catch_that_image() {
    global $post, $posts;
    $first_img = '';
    ob_start();
    ob_end_clean();
    $output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
    $first_img = $matches [1] [0];

    if(empty($first_img)){ //Defines a default image
    $first_img = "/images/default.jpg";
    }
    return $first_img;
    }


    From there I can echo the URL using the following
    <?php echo catch_that_image(); ?>


    The end result would look like this
    <?php
    $meta = $custom_metabox->the_meta('description', TRUE);
    if (!empty($meta)):
    echo '<p class="description">'.$meta['description'].'</p>'; ?>
    <?php endif; ?>
    <br>
    <?php if( function_exists( 'attachments_get_attachments' ) ) {
    $attachments = attachments_get_attachments();
    $total_attachments = count( $attachments );
    if( $total_attachments ) : ?>
    <ul class="process"><span>Process:</span>
    <li><a href="<?php echo catch_that_image() ?>"; ?><?php echo '1</a></li>'; ?>
    <?php for( $i=0; $i<$total_attachments; $i++ ) : ?>
    <li><a href="<?php echo $attachments[$i]['location']; ?>"><?php echo $i+2; ?></a></li>
    <?php endfor; ?>
    </ul>
    <br>
    <?php endif; ?> <?php } ?>