Navigation
This is archived content. Visit our new forum.
  • Author
    Posts
  • #694972

    mchll9898
    Participant

    Is it possible to add rel=”nofollow” to image elements that are hyperlinked? I know I can just use the text element and write the code out, but wanted to know if there was an easier way when using the image element.

    #695008

    Lely
    Moderator

    Hello There,

    Thanks for posting in.
    Because this requires a template change, I’d advise that you setup a child theme. This allows you to make code changes that won’t be overwritten when an X update is released. After your child theme is setup, please review how we recommend making template changes in Customization Best Practices.

    Then add the following code in functions.php file of your child theme.

    // =============================================================================
    // Image
    // =============================================================================
    
    function x_shortcode_image_custom( $atts ) {
      extract( shortcode_atts( array(
        'id'               => '',
        'class'            => '',
        'style'            => '',
        'type'             => '',
        'float'            => '',
        'src'              => '',
        'alt'              => '',
        'link'             => '',
        'href'             => '',
        'title'            => '',
        'target'           => '',
        'info'             => '',
        'info_place'       => '',
        'info_trigger'     => '',
        'info_content'     => '',
        'lightbox_thumb'   => '',
        'lightbox_video'   => '',
        'lightbox_caption' => ''
      ), $atts, 'x_image' ) );
    
      $id               = ( $id               != ''      ) ? 'id="' . esc_attr( $id ) . '"' : '';
      $class            = ( $class            != ''      ) ? 'x-img ' . esc_attr( $class ) : 'x-img';
      $style            = ( $style            != ''      ) ? 'style="' . $style . '"' : '';
      $type             = ( $type             != ''      ) ? ' x-img-' . $type : '';
      $float            = ( $float            != ''      ) ? ' ' . $float : '';
      $src              = ( $src              != ''      ) ? $src : '';
      $alt              = ( $alt              != ''      ) ? 'alt="' . $alt . '"' : '';
      $link             = ( $link             == 'true'  ) ? 'true' : '';
      $link_class       = ( $link             == 'true'  ) ? ' x-img-link' : '';
      $href             = ( $href             != ''      ) ? $href : $src;
      $title            = ( $title            != ''      ) ? 'title="' . $title . '"' : '';
      $target           = ( $target           == 'blank' ) ? 'target="_blank"' : '';
      $lightbox_thumb   = ( $lightbox_thumb   != ''      ) ? $lightbox_thumb : $src;
      $lightbox_video   = ( $lightbox_video   == 'true'  ) ? ', width: 1080, height: 608' : '';
      $lightbox_caption = ( $lightbox_caption != ''      ) ? 'data-caption="' . $lightbox_caption . '"' : '';
    
      $tooltip_attr = ( $info != '' ) ? cs_generate_data_attributes_extra( $info, $info_trigger, $info_place, '', $info_content ) : '';
    
      if ( is_numeric( $src ) ) {
        $src_info = wp_get_attachment_image_src( $src, 'full' );
        $src      = $src_info[0];
      }
    
      if ( is_numeric( $href ) ) {
        $href_info = wp_get_attachment_image_src( $href, 'full' );
        $href      = $href_info[0];
      }
    
      if ( is_numeric( $lightbox_thumb ) ) {
        $lightbox_thumb_info = wp_get_attachment_image_src( $lightbox_thumb, 'full' );
        $lightbox_thumb      = $lightbox_thumb_info[0];
      }
    
      if ( $lightbox_video != '' ) {
        $lightbox_options = "data-options=\"thumbnail: '" . $lightbox_thumb . "'{$lightbox_video}\"";
      } else {
        $lightbox_options = "data-options=\"thumbnail: '" . $lightbox_thumb . "'\"";
      }
    
      if ( $link == 'true' ) {
        $output = "<a rel=\"nofollow\" {$id} class=\"{$class}{$link_class}{$type}{$float}\" {$style} href=\"{$href}\" {$title} {$target} {$tooltip_attr} {$lightbox_caption} {$lightbox_options}><img src=\"{$src}\" {$alt}></a>";
      } else {
        $output = "<img {$id} class=\"{$class}{$type}{$float}\" {$style} src=\"{$src}\" {$alt}>";
      }
    
      return $output;
    }
    
    add_action('wp_head', 'change_image_schortcode_with_custom');
    
    function change_image_schortcode_with_custom() {
      remove_shortcode( 'x_image' );
      remove_shortcode( 'cs_image' );
      add_shortcode( 'x_image', 'x_shortcode_image_custom' );
      add_shortcode( 'cs_image', 'x_shortcode_image_custom' );
    }
    
    

    Hope this helps.

    #695563

    mchll9898
    Participant

    Thank you. What does this code do exactly? Does it make all hyperlinked images nofollow? Because I do have some hyperlinked images that link to internal pages that would not need the nofollow attribute.

    #695688

    Rupok
    Member

    Hi there,

    Thanks for updating. Yes it will make all hyperlinked images nofollow. If you need to use it on specific image then you can use within the Text Element.

    Cheers!