最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - add custom metabox to media library custom widget

programmeradmin4浏览0评论

I'm implementing a custom widget to create a logos slider. The initial code works well, but when I have added a metabox to the attachment screen to let the user add an external link to the images, I get some problems. What I want to do is to add the metabox, save it's value and when the widget is loaded on the front-end, retrive back the link that user have added for each image. The images are not attached to any post, and for now I'm able to see the new metabox but it can be edited only if the user click on the edit link of each image in the media library, I would prefere to see the box on the main selection screen of media gallery. I'm also unable to get the field value, I think because inside the code the images ids are not loaded.

public function initMetabox()
  {
    add_meta_box(
      'image_link_box',
      __('Link image to url'),
      array($this, 'renderMetabox'),
      'attachment'
    );
  }

  public function renderMetabox( $post )
  {
    $url = get_post_meta( $post->ID, '_image_link_url', true );
    ?>
    <label for="image-link"><?php _e('Image Link'); ?></label>
    <input type="text" name="image_link" id="image-link" value="<?php echo $url; ?>" class="postbox">
    <?php
  }

  public function saveLink( $post_id )
  {
    if( in_array( 'image_link', $_POST ) ){
      update_post_meta(
        $post_id,
        '_image_link_url',
        $_POST['image_link']
      );
    }
  }

  public function widget( $args, $instance )
  {
    $images = explode( ',', $instance['images'] );
    $image_link = get_post_meta( $post->ID, '_image_link_url', true );

    ob_start();
    ?>
    <div class="swiper-container logo-slider" id="featured-logo-slider">
      <div class="swiper-wrapper" id="featured-logo">
        <?php foreach( $images as $image ): ?>
          <?php foreach( $image_link as $link ): ?>
            <a href="<?php echo $link; ?>"><img class="swiper-slide" src="<?php echo $image; ?>" alt="" width="100" id="client-logo"/></a>
          <?php endforeach; ?>
        <?php endforeach; ?>
      </div>
      <div class="swiper-scrollbar"></div>
    </div>
    <?php
    echo ob_get_clean();
  }

The foreach loop for the images links will produce an error Invalid argument supplied... and the $post->ID variable is not available. Any suggestion will be appreciated

I'm implementing a custom widget to create a logos slider. The initial code works well, but when I have added a metabox to the attachment screen to let the user add an external link to the images, I get some problems. What I want to do is to add the metabox, save it's value and when the widget is loaded on the front-end, retrive back the link that user have added for each image. The images are not attached to any post, and for now I'm able to see the new metabox but it can be edited only if the user click on the edit link of each image in the media library, I would prefere to see the box on the main selection screen of media gallery. I'm also unable to get the field value, I think because inside the code the images ids are not loaded.

public function initMetabox()
  {
    add_meta_box(
      'image_link_box',
      __('Link image to url'),
      array($this, 'renderMetabox'),
      'attachment'
    );
  }

  public function renderMetabox( $post )
  {
    $url = get_post_meta( $post->ID, '_image_link_url', true );
    ?>
    <label for="image-link"><?php _e('Image Link'); ?></label>
    <input type="text" name="image_link" id="image-link" value="<?php echo $url; ?>" class="postbox">
    <?php
  }

  public function saveLink( $post_id )
  {
    if( in_array( 'image_link', $_POST ) ){
      update_post_meta(
        $post_id,
        '_image_link_url',
        $_POST['image_link']
      );
    }
  }

  public function widget( $args, $instance )
  {
    $images = explode( ',', $instance['images'] );
    $image_link = get_post_meta( $post->ID, '_image_link_url', true );

    ob_start();
    ?>
    <div class="swiper-container logo-slider" id="featured-logo-slider">
      <div class="swiper-wrapper" id="featured-logo">
        <?php foreach( $images as $image ): ?>
          <?php foreach( $image_link as $link ): ?>
            <a href="<?php echo $link; ?>"><img class="swiper-slide" src="<?php echo $image; ?>" alt="" width="100" id="client-logo"/></a>
          <?php endforeach; ?>
        <?php endforeach; ?>
      </div>
      <div class="swiper-scrollbar"></div>
    </div>
    <?php
    echo ob_get_clean();
  }

The foreach loop for the images links will produce an error Invalid argument supplied... and the $post->ID variable is not available. Any suggestion will be appreciated

Share Improve this question asked Jan 11, 2020 at 16:20 sialfasialfa 32910 silver badges29 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

It looks that a reference to global $post; is missing at the beginning of your function widget(){}

发布评论

评论列表(0)

  1. 暂无评论