skip to Main Content

i moved kernel files wordpress to a subfolder

now have a folder "content" and "kernel" at root

and got some problems

some commands only work with subfolder "kernel"

for example…

i use this function for to add change time for name scripts and styles

function put_modified_time_version($src, $baseUrl){
    if ($src && strpos($src, $baseUrl) === 0) {
        $newSrc = remove_query_arg('ver', $src);
        $path = substr($newSrc, strlen($baseUrl));
        $path = wp_parse_url($path, PHP_URL_PATH);
        if ($mtime = @filemtime(untrailingslashit(ABSPATH) . $path)) {
            $src = add_query_arg('ver', $mtime, $newSrc);
        }
    }
    return $src;
}
add_filter('style_loader_src', 'modified_time_version_style', 15, 1);
function modified_time_version_style($src) {
    return ($src) ? put_modified_time_version($src, wp_styles()->base_url) : $src;
}
add_filter('script_loader_src', 'modified_time_version_script', 15, 1);
function modified_time_version_script($src) {
    return ($src) ? put_modified_time_version($src, wp_scripts()->base_url) : $src;
}

this works only for js and css in "kernel" folder

functions work from a folder "content"

think that a problem with $url = substr($newSrc, strlen($baseUrl));

tell me how it is better to specify it correctly so that everything would work?

2

Answers


  1. Chosen as BEST ANSWER

    as i wrote above
    found a strange problem when transferring core files to a subfolder
    for example...
    function that adds versioning for js and css
    this function adds versioning regardless of position of core files problem was in $baseURL maybe it will be useful to someone

    function version_asset( $src, $handle, &$deps ) {
        $path = parse_url( $src, PHP_URL_PATH );
        foreach ( $deps->default_dirs as $dir ) {
            if ( 0 === strpos( $path, $dir ) ) {
                return $src;
            }
        }
        $version = get_file_version( $src );
        $deps->registered[ $handle ]->ver = $version;
        return add_query_arg( 'ver', $version, $src );
    }
    add_filter( 'script_loader_src', __NAMESPACE__ . 'version_script', 10, 2 );
    function version_script( $src, $handle ) {
        global $wp_scripts;
        return version_asset( $src, $handle, $wp_scripts );
    }
    add_filter( 'style_loader_src', __NAMESPACE__ . 'version_style', 10, 2 );
    function version_style( $src, $handle ) {
        global $wp_styles;
        return version_asset( $src, $handle, $wp_styles );
    }
    function get_file_version( $url ) {
        $content_url = content_url();
        $filepath    = str_replace( $content_url, WP_CONTENT_DIR, $url );
        $filepath    = explode( '?', $filepath );
        $filepath    = array_shift( $filepath );
        if ( ! file_exists( $filepath ) ) {
            return;
        }
        try {
            $timestamp = filemtime( $filepath );
        } catch ( Exception $e ) {
            return;
        }
        return $timestamp ? (string) $timestamp : null;
    }
    

  2. If ABSPATH isn’t returning the right thing then try some others. If you want a different path than one related to the current file then define a constant in a file that has the correct path (e.g. the main plugin file) and use that constant instead. For example in your main plugin.php

    define('INTERESTING_PATH', ABSPATH);
    define('INTERESTING_PATH2', ABSPATH . '/content/');
    define('INTERESTING_PATH3', WP_CONTENT_DIR);
    define('SITE_ROOT', str_replace('wp-content', '', WP_CONTENT_DIR));
    define('PLUGIN_STUFF', plugin_dir_path(__FILE__));
    

    Then everywhere else in the code you can use the constants. For example

    $fp = fopen( INTERESTING . "sitemap.xml", 'w' );
    

    If you need a directory higher in the hierarchy then use dirname($path). e.g. define('WHATEVER', dirname(dirname(__FILE__))); or define('WHATEVER', dirname(dirname(__FILE__)) . '/content/');

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search