-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathFeature.php
More file actions
56 lines (47 loc) · 1.36 KB
/
Feature.php
File metadata and controls
56 lines (47 loc) · 1.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<?php
/**
* Permalink Cleanup Feature
*
* @package multisyde
*/
declare(strict_types=1);
namespace Syde\MultiSyde\Modules\PermalinkCleanup;
use Syde\MultiSyde\LoadableFeature;
/**
* Feature Class PermalinkCleanup
*/
final class Feature implements LoadableFeature {
/**
* Adds functionality to their respective hooks.
*
* @return void
*/
public static function init(): void {
add_filter( 'sanitize_option_permalink_structure', array( __CLASS__, 'remove_blog_prefix' ) );
add_filter( 'option_permalink_structure', array( __CLASS__, 'remove_blog_prefix' ) );
}
/**
* Remove /blog prefix from the permalink structure on the main site.
*
* In WordPress multisite installations, the main site often has /blog
* prepended to post permalinks. This function strips that prefix while
* leaving other sites untouched.
*
* @param string $value The permalink structure pattern (e.g., '/blog/%postname%').
*
* @return string The modified permalink structure with /blog removed,
* or the original value if conditions aren't met.
*/
public static function remove_blog_prefix( string $value ): string {
if ( ! is_multisite() || ! is_main_site() || empty( $value ) ) {
return $value;
}
if ( strpos( $value, '/blog/' ) === 0 ) {
return substr( $value, 5 );
}
if ( '/blog' === $value ) {
return '';
}
return $value;
}
}