0
votes

Having a bit of bother with the Wordpress Meta Box plugin, specifically with retrieving an image url from an image added to a custom post type.

I'm creating meta boxes in a custom plugin, like so:

add_filter( 'rwmb_meta_boxes', 'xxx_meta_register_meta_boxes' );

    function xxx_meta_register_meta_boxes( $meta_boxes )
    {

    $prefix = 'xxx_meta_';

    $meta_boxes[] = array(
    'title' => esc_html__( 'Retailer Information', '' ),
    'id'         => 'advanced',
    'post_types' => array( 'xxx_retailers' ),
    'autosave'   => true,
    'fields'     => array(

      // PLUPLOAD IMAGE UPLOAD (WP 3.3+)
            array(
                'name'             => esc_html__( 'Retailer Logo', '' ),
                'id'               => "{$prefix}plupload",
                'type'             => 'plupload_image',
                'max_file_uploads' => 1,
            ),

            // URL
            array(
                'name' => esc_html__( 'Link', '' ),
                'id'   => "{$prefix}url",
                'desc' => esc_html__( 'Clicking the retailer logo will take the user to this URL', '' ),
                'type' => 'url',
                'std'  => 'xxx',
            ),
        )
    );
    return $meta_boxes;
    }

So far so good, these boxes are relevant to a custom post type 'xxx_retailers'.

The problem comes with retrieving this data. I want to display my retailers in a widget. I've chopped and changed another piece of code I've used previously, but it's not returning the image URL, just the ID. Unfortunately I don't know enough php to figure out why.

// Create Retailers Widget
    // Create the widget
    class Retailers_Widget extends WP_Widget {
        function __construct() {
                parent::__construct(
                        // base ID of the widget
                        'retailers_widget',
                        // name of the widget
                        __('XXX Retailers List', '' ),
                        // widget options
                        array (
                            'description' => __( 'Shows a list of retailer logos', '' )
                        )
                    );
    }
        function widget( $args, $instance ) {
            // kick things off
            extract( $args );
            echo $before_widget;
            echo $before_title . 'Retailers' . $after_title;

      // Pull through Retailers
      $xxxretailers = get_posts(array(
                        'post_type' => 'xxx_retailers',
                        'orderby' => 'title',
                        'order' => 'asc',
                    ));

    // Display for each Retailer
    foreach ($xxxretailers as $xxxretailer) {
      $custom = get_post_custom($xxxretailer->ID);
      $meta_ret_img = $custom["xxx_meta_plupload"][0];
      $meta_ret_url = $custom["xxx_meta_url"][0];
      // Display Retailers
      echo "<li><a href='{$meta_ret_url}'><img src='{$meta_ret_img}' /></a></li>";
           }
        }
};
// Register widget
function register_retailers_widget() {
  register_widget( 'Retailers_Widget' );
}
add_action( 'widgets_init', 'register_retailers_widget' );

The URLs are coming through correctly, so I know this is a problem with the line

$meta_ret_img = $custom["xxx_meta_plupload"][0];

But I can't figure out how to get the image URL from the data I presume is stored as an array. Any ideas?

Edit:

I should have mentioned, in a single post I can get a single image with:

$images = rwmb_meta( 'xxx_meta_plupload', 'size=medium' );
if ( !empty( $images ) ) {
    foreach ( $images as $image ) {
        echo "<img src='{$image['url']}' />";
    }
}

But I want to show images from all my retailers post types, to create a list of logos.

1
can you add the output of var_dump($custom); hereTarun Mahashwari
Sure, out comes the following: array(6) { ["_edit_lock"]=> array(1) { [0]=> string(12) "1472408521:1" } ["_edit_last"]=> array(1) { [0]=> string(1) "1" } ["_revision-control"]=> array(1) { [0]=> string(25) "a:1:{i:0;s:8:"defaults";}" } ["_yoast_wpseo_content_score"]=> array(1) { [0]=> string(2) "30" } ["xxx_meta_url"]=> array(1) { [0]=> string(163) "[Redacted]" } ["xxx_meta_plupload"]=> array(1) { [0]=> string(4) "1862" } }kevngibsn
also i would like to know why you use curly braces in src attribute of img tag, please also add output of following statement echo $custom["xxx_meta_url"][0];Tarun Mahashwari
Curly brackets, no particular reason other than they were in the code example I first followed. As I said, I don't know a lot of php so this is kind of pieced together from tutorials. $custom["xxx_meta_url"] outputs the url entered in the url metabox in my first code example above. This comes out just as it's entered.kevngibsn
Nope, unfortunately it's just the same. The href url is coming through, but the url for the img src isn't. I'm just getting the image id, which is a 4 digit number. It's not that the code doesn't work, it's that I can't figure out to get what I want, which is the image url, not the id.kevngibsn

1 Answers

0
votes

Replace this statement:

$meta_ret_img = $custom["xxx_meta_plupload"][0];

with this:

$meta_ret_img_array = wp_get_attachment_image_src($custom["xxx_meta_plupload"][0]);

$meta_ret_img = $meta_ret_img_array[0];

also please remove all these curly braces from src and href attributes from your code.

if you are interested in any particular size of the image, then see the official document of wp_get_attachment_image_src() function here.

For e.g. for medium size image you can write it as:

wp_get_attachment_image_src($custom["xxx_meta_plupload"][0], 'medium');