seo-by-rank-math icon indicating copy to clipboard operation
seo-by-rank-math copied to clipboard

The filter "rank_math/opengraph/{$network}/image" doesn't work as expected/documented

Open a-corsini opened this issue 2 years ago • 0 comments

The filter "rank_math/opengraph/{$network}/image" doesn't work if there is no featured image assigned to the post.

To Reproduce Use the filter "rank_math/opengraph/{$network}/image" to return an attachment URL for a post type that has no featured image set.

Expected behavior The documentation doesn't mention this particular case, so the user expects the filter to work and use the newly provided image in the opengraph tag.

System Info

  • RankMath SEO: 1.0.90
  • WordPress: 6.0

Additional context The problematic implementation seems to be in the file includes > opengraph > class-image.php. Around line 213 the function add_image() is declared as follows:

public function add_image( $attachment ) {
    // In the past `add_image` accepted an image url, so leave this for backwards compatibility.
     if ( Str::is_non_empty( $attachment ) ) {
          $attachment = [ 'url' => $attachment ];
    }

     if ( ! is_array( $attachment ) || empty( $attachment['url'] ) ) {
          return;
    }

     $attachment_url = explode( '?', $attachment['url'] );
     if ( ! empty( $attachment_url ) ) {
          $attachment['url'] = $attachment_url[0];
    }

    // If the URL ends in `.svg`, we need to return.
    if ( ! $this->is_valid_image_url( $attachment['url'] ) ) {
          return;
    }

    /**
    * Allow changing the OpenGraph image.
    *
    * The dynamic part of the hook name. $this->network, is the network slug.
    *
    * @param string $img The image we are about to add.
    */
     $image_url = trim( $this->do_filter( "opengraph/{$this->network}/image", $attachment['url'] ) );
     if ( empty( $image_url ) ) {
          return;
    }
    [ ... CODE REDACTED FOR BREVITY ]
}

With the if statement at line 219 the function returns if the the attachment is invalid or doesn't have an url key, way before applying the filter at line 240.

a-corsini avatar Jun 07 '22 15:06 a-corsini