• Fixing the Issue with Adding wp-hijri in the hijri-widgets.php File

    Error Message:

    [21-Dec-2024 10:30:11 UTC] PHP Warning:  Undefined array key "HTTP_USER_AGENT" in /home/mfkvhsiieh/public_html/wp-content/plugins/wp-hijri/include/hijri-widgets.php on line 317

    Original Code with the Issue:

    if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== false || stripos($_SERVER['HTTP_USER_AGENT'], 'camino') !== false || stripos($_SERVER['HTTP_USER_AGENT'], 'safari') !== false)
        $ak_title_separator = "\n";
    else
        $ak_title_separator = ', ';
    
    $ak_titles_for_day = array();
    $ak_post_titles = $wpdb->get_results("SELECT ID, post_title, DAYOFMONTH(post_date) as d , MONTH(post_date) AS m,YEAR(post_date) AS y "
            . "FROM $wpdb->posts "
            . "WHERE post_date >= '{$gy1}-{$gm1}-{$gd1} 00:00:00' "
            . "AND post_date <= '{$gy2}-{$gm2}-{$gd2} 23:59:59' "
            . "AND post_type = 'post' AND post_status = 'publish'"
    );
    if ($ak_post_titles) {
        foreach ((array) $ak_post_titles as $ak_post_title) {
    
            /** This filter is documented in wp-includes/post-template.php */
            $post_title = esc_attr(apply_filters('the_title', $ak_post_title->post_title, $ak_post_title->ID));
            $dt=new \hijri\datetime($ak_post_title->y . '-' . zeroise($ak_post_title->m, 2) . '-' . zeroise($ak_post_title->d, 2), NULL, NULL, $hcal);
            $hd = $dt->format('_j');
            if (empty($ak_titles_for_day['day_' . $hd]))
                $ak_titles_for_day['day_' . $hd] = '';
            if (empty($ak_titles_for_day["$hd"])) // first one
                $ak_titles_for_day["$hd"] = $post_title;
            else
                $ak_titles_for_day["$hd"] .= $ak_title_separator . $post_title;
        }
    }

    Revised Code:

    if (isset($_SERVER['HTTP_USER_AGENT']) && 
        (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== false || 
         stripos($_SERVER['HTTP_USER_AGENT'], 'camino') !== false || 
         stripos($_SERVER['HTTP_USER_AGENT'], 'safari') !== false)) {
        $ak_title_separator = "\n";
    } else {
        $ak_title_separator = ', ';
    }
    
    $ak_titles_for_day = array();
    $ak_post_titles = $wpdb->get_results(
        "SELECT ID, post_title, DAYOFMONTH(post_date) as d, MONTH(post_date) AS m, YEAR(post_date) AS y " .
        "FROM $wpdb->posts " .
        "WHERE post_date >= '{$gy1}-{$gm1}-{$gd1} 00:00:00' " .
        "AND post_date <= '{$gy2}-{$gm2}-{$gd2} 23:59:59' " .
        "AND post_type = 'post' AND post_status = 'publish'"
    );
    
    if ($ak_post_titles) {
        foreach ((array) $ak_post_titles as $ak_post_title) {
            /** This filter is documented in wp-includes/post-template.php */
            $post_title = esc_attr(apply_filters('the_title', $ak_post_title->post_title, $ak_post_title->ID));
            $dt = new \hijri\datetime($ak_post_title->y . '-' . zeroise($ak_post_title->m, 2) . '-' . zeroise($ak_post_title->d, 2), NULL, NULL, $hcal);
            $hd = $dt->format('_j');
            if (empty($ak_titles_for_day['day_' . $hd])) {
                $ak_titles_for_day['day_' . $hd] = '';
            }
            if (empty($ak_titles_for_day["$hd"])) { // first one
                $ak_titles_for_day["$hd"] = $post_title;
            } else {
                $ak_titles_for_day["$hd"] .= $ak_title_separator . $post_title;
            }
        }
    }

    The page I need help with: [log in to see the link]

The topic ‘PHP Warning: Undefined array key “HTTP_USER_AGENT”’ is closed to new replies.