Support » Plugins » Hacks » url to path vv

  • Is there a safe, always working way to convert an url to a path vice versa? I used to do it this way:

    <?php
    $url = str_replace( ABSPATH, site_url(), $path );
    .
    .
    $path = str_replace( site_url(), ABSPATH, $url );
    ?>

    but it looks like this does not always work correctly.
    I only need locations inside .../wp-content/

Viewing 7 replies - 1 through 7 (of 7 total)
  • Moderator bcworkz

    (@bcworkz)

    I think I would break down the path/url into components with pathinfo(), and explode the directory portion into its components, then reconstruct the desired result using either get_site_url() or get_home_path().

    Thread Starter Jacob N. Breetvelt

    (@opajaap)

    Thank you for pointing me in the direcion of using get_home_path() in stead of ABSPATH. This looks good.

    Thread Starter Jacob N. Breetvelt

    (@opajaap)

    However, get_home_path() is defined in wp-admin/includes/file.php, so not usable for plugins used in the front-end…. Or should i include wp-admin/includes/file.php, resulting in the same problems as using admin_url( ‘admin-ajax.php’ ) in the front-end. There are plugins that make files in wp-admin inaccessable for front-end processes, that is why i already wrote my own ajax-front.php file.

    Resulting in the following:
    Enhancement request for the following functions, accesable from front- and back-end:

    • content_path( $relpath ); equivalent to content_url( $path ); but returing path in stead of url
    • path_to_url( $full_path ); returning FALSE if outside installation root ABSPATH
    • url_to_path( $full_url ); returning FALSE if outside installation root ABSPATH
    • get_home_path() being accessable from the front-end
    Moderator bcworkz

    (@bcworkz)

    I can really only comment about WP in its default state. In that state get_home_path() is declared and available for any plugin to use. I do understand the need to add additional security by limiting access to wp-admin. IMO, any plugin that does this should at the very least except admin-ajax.php because it is commonly used by many themes. That would not solve the issue with get_home_path() anyway. Including the file may work, depending on how the security is setup. I suspect the include will be blocked as well. In that case you’d need to copy the declaration into your plugin and assign a new name, in a way like how you dealt with admin-ajax.php.

    Unfortunately there is no content_path(). What should work is to check if the constant WP_CONTENT_DIR is defined. If it is, it should contain the full path. If it is not defined, it should be safe to append /wp-content/ to the return from get_home_path().

    path_to_url() and url_to_path() are not WP functions I know of, so I cannot speak about any issues. You might consider that $_SERVER['DOCUMENT_ROOT'] might be useful in some way.

    Thread Starter Jacob N. Breetvelt

    (@opajaap)

    Thank you very much for helping me to think in the right direction.
    I think i found a monkey proof solution. To illustrate: a code fragment from my plugin wp-photo-album-plus

    The comments in the code should clarify what i do:

    define( 'WPPA_ABSPATH', wppa_flips( ABSPATH ) ); 		// ABSPATH formatted for Windows servers
    
    // Although i may not use wp constants directly,
    // there is no function that returns the path to wp-content,
    // so, if you changed the location of wp-content, i have to use WP_CONTENT_DIR,
    // because wp-content needs not to be relative to ABSPATH
    if ( defined( 'WP_CONTENT_DIR' ) ) {
    	define( 'WPPA_CONTENT_PATH', wppa_flips( WP_CONTENT_DIR ) );
    }
    
    // In the normal case i use content_url() with the site_url() part replaced by WPPA_ABSPATH,
    // i.e. ABSPATH with the slashes in the right direction (in case of windows server)
    else {
    	define( 'WPPA_CONTENT_PATH',
    		str_replace( wppa_trimflips( site_url() ) . '/',
    		WPPA_ABSPATH, wppa_flips( content_url() ) )
    		);												// /.../wp-content
    }
    
    // Also define my url to wp-content:
    define( 'WPPA_CONTENT_URL', content_url() );
    
    // Now you can convert a path to an url vv form files inside wp-content as follows
    // $path = str_replace( WPPA_CONTENT_URL, SWPPA_CONTENT_PATH, $url );
    // $url = str_replace( WPPA_CONTENT_PATH, SWPPA_CONTENT_URL, $path );

    Moderator bcworkz

    (@bcworkz)

    Nice! I suppose there still may be a situation we have not accounted for, but I think you have them all covered. Fingers crossed 🙂

    Thread Starter Jacob N. Breetvelt

    (@opajaap)

    Thanx again for helping me thinking. Topic cosed

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘url to path vv’ is closed to new replies.