Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wpmudev-sls/6f100f29ab37b94fb7e9e68542dbef66 to your computer and use it in GitHub Desktop.
Save wpmudev-sls/6f100f29ab37b94fb7e9e68542dbef66 to your computer and use it in GitHub Desktop.
[Hustle Pro] - Add alt atribute for feature image
<?php
/**
* Plugin Name: [Hustle Pro] - Add alt atribute for feature image
* Description: [Hustle Pro] - Add alt atribute for feature image - 1147366844511292
* Author: Thobk @ WPMUDEV
* Author URI: https://premium.wpmudev.org
* License: GPLv2 or later
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
add_action( 'plugins_loaded', 'wpmudev_hustle_add_alt_attribute_for_feature_image_func', 100 );
function wpmudev_hustle_add_alt_attribute_for_feature_image_func() {
if( defined('HUSTLE_SUI_VERSION') && class_exists( 'Hustle_Module_Collection' ) ){
class WPMUDEV_HT_Add_Feature_Alt{
private $feature_images = [];
public function __construct(){
add_filter( 'hustle_sort_modules', array( $this, 'filter_modules' ) );
add_filter( 'clean_url', array( $this, 'filter_clean_url' ), 10, 2 );
}
public function filter_modules( $modules ){
foreach( $modules as $module ){
if( $module->content->feature_image ){
$this->feature_images[ $module->content->feature_image ] = $module->content->title;
}
}
return $modules;
}
public function filter_clean_url( $good_protocol_url, $original_url ){
if( isset( $this->feature_images[ $original_url ] ) ){
$alt = $this->get_alt_image_from_url( $original_url );
if( ! $alt ){
$alt = $this->feature_images[ $original_url ];
}
$alt = esc_html( $alt );
$good_protocol_url .= '" alt="'. $alt;
}
return $good_protocol_url;
}
public function get_image_id($image_url) {
global $wpdb;
$attachment = $wpdb->get_col($wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE guid='%s';", $image_url ));
return $attachment ? (int) $attachment[0] : 0;
}
public function get_alt_image_from_url( $image_url ){
$alt = '';
if( $image_id = $this->get_image_id( $image_url ) ){
$alt = get_post_meta($image_id, '_wp_attachment_image_alt', TRUE); }
return $alt;
}
}
$run = new WPMUDEV_HT_Add_Feature_Alt;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment