Plugin Directory

Changeset 3482248


Ignore:
Timestamp:
03/13/2026 08:00:12 PM (2 weeks ago)
Author:
dibwp
Message:

Fix trailing slash handling for blog sub-paths to match WordPress permalink settings

Location:
dropinblog/trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • dropinblog/trunk/classes/rendering.php

    r3481598 r3482248  
    3434
    3535            $request_uri = isset($_SERVER['REQUEST_URI']) ? sanitize_text_field(wp_unslash($_SERVER['REQUEST_URI'])) : '';
    36             if (strpos($request_uri, '/' . $this->assigned_page->post_name) === false) {
     36            $request_path = wp_parse_url($request_uri, PHP_URL_PATH);
     37            $base_slug = '/' . $this->assigned_page->post_name;
     38
     39            if (strpos($request_path, $base_slug) !== 0) {
     40                return;
     41            }
     42
     43            // Reject wrong trailing slash on the base URL (let WordPress redirect)
     44            $remainder = substr($request_path, strlen($base_slug));
     45            $uses_trailing_slashes = substr(get_option('permalink_structure'), -1) === '/';
     46            $correct_base = $uses_trailing_slashes ? $base_slug . '/' : $base_slug;
     47            if (($remainder === '' || $remainder === '/') && $request_path !== $correct_base) {
    3748                return;
    3849            }
     
    114125    public function render_redirect()
    115126    {
     127        // Redirect wrong trailing slash on sub-paths (not real WP posts, so WP won't handle)
     128        if (isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] === 'GET') {
     129            $request_uri = isset($_SERVER['REQUEST_URI']) ? sanitize_text_field(wp_unslash($_SERVER['REQUEST_URI'])) : '';
     130            $path = wp_parse_url($request_uri, PHP_URL_PATH);
     131            if ($path && !str_contains(basename($path), '.')) {
     132                $correct_path = user_trailingslashit($path);
     133                if ($path !== $correct_path) {
     134                    $query_string = wp_parse_url($request_uri, PHP_URL_QUERY);
     135                    wp_redirect($correct_path . ($query_string ? '?' . $query_string : ''), 301);
     136                    exit;
     137                }
     138            }
     139        }
     140
    116141        if ($this->is_full_type) {
    117142            if ($this->is_full_type === 'sitemap') {
     
    363388    public function get_url_subsegments($base_slug = '')
    364389    {
    365         // Safely get REQUEST_URI
    366390        $request_uri = isset($_SERVER['REQUEST_URI'])
    367391            ? sanitize_text_field(wp_unslash($_SERVER['REQUEST_URI']))
    368392            : '';
    369393
    370         // Strip base path
    371         $path = str_replace($base_slug, '', wp_parse_url($request_uri, PHP_URL_PATH));
    372 
    373         // Build parts
    374         $path_parts = explode('/', $path);
    375         if (end($path_parts) === '') {
    376             array_pop($path_parts);
    377         }
    378 
    379         return $path_parts;
     394        $path = wp_parse_url($request_uri, PHP_URL_PATH);
     395
     396        // Strip base path (handle with or without trailing slash)
     397        $base_no_slash = rtrim($base_slug, '/');
     398        if (strpos($path, $base_no_slash) === 0) {
     399            $path = substr($path, strlen($base_no_slash));
     400        }
     401
     402        // Build parts from remaining path
     403        $path = trim($path, '/');
     404        if ($path === '' || $path === false) {
     405            return [];
     406        }
     407
     408        return explode('/', $path);
    380409    }
    381410
  • dropinblog/trunk/dropinblog.php

    r3481598 r3482248  
    44Plugin URI: https://dropinblog.com
    55Description: Embed and display your DropInBlog seamlessly within WordPress with full SEO support.
    6 Version: 1.0.3
     6Version: 1.0.4
    77License: GPL v2 or later
    88License URI: https://www.gnu.org/licenses/gpl-2.0.html
  • dropinblog/trunk/readme.txt

    r3481598 r3482248  
    44Tested up to: 6.9
    55Requires at least PHP: 8.0
    6 Stable tag: 1.0.3
     6Stable tag: 1.0.4
    77License: GPLv2 or later
    88License URI: https://www.gnu.org/licenses/gpl-2.0.html
     
    4343== Changelog ==
    4444
     45= 1.0.4 =
     46* Fixed trailing slash handling for blog sub-paths to match WordPress permalink settings
     47
    4548= 1.0.3 =
    4649* Enqueue external scripts via wp_enqueue_script for better compatibility with caching and optimization plugins
     
    5659
    5760== Upgrade Notice ==
     61
     62= 1.0.4 =
     63Fixes trailing slash redirects on blog post URLs to match your WordPress permalink settings.
    5864
    5965= 1.0.3 =
Note: See TracChangeset for help on using the changeset viewer.