• Resolved englishcoder

    (@englishcoder)


    Hi guys and girls,

    I am trying to set up a function for a theme options panel to look up the version on a remote site to determine whether a new version is available.

    I tried simply calling the get_theme_data function but quickly discovered that if the file doesn’t exist (e.g. if the remote site is down) it returns every page in the local site as error 500 – internal server error.

    My solution was to use this code:

    $remote_version_url = 'https://www.jamesmorrison.me/style-test.css';
    
    if ( file_exists($remote_version_url) ) {
    
    	$remote_theme_data = get_theme_data($remote_version_url);
    
    	$remote_version = $remote_theme_data['Version'];
    
    } else {
    
    	$remote_version = "0"; // ensures that update message is not shown
    
    }

    to ensure that the file is looked up before processing the data, but the file always shows as not found (even though it exists).

    Have I missed something obvious or is there an error with the code?

    Many thanks for any help!

    James

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter englishcoder

    (@englishcoder)

    Figured it out – replaced:

    if ( file_exists($remote_version_url) ) {

    with

    if (@fopen($remote_version_url, "r")) { //@ hides error message if file not found / available

    since you can’t use file_exists for remote servers…

    I wanted to do an “unlink()” (delete a file), so while “@fopen” worked for the “file_exists()” function, it didn’t for “unlink()” because of the absolute path.

    After messing around for quite some time, I found a solution. Even if it’s a small tip, I took the time to write a little “tutorial” about it on my Blog.

    Here it is:

    Have you ever wanted to use the file_exists function inside of WordPress? For example, last night I wanted to delete a file that was at the third level of my folders’ tree (/public_html/wordpress/imagefolder) from a custom PHP page in my WP theme’s folder.

    Sounds easy, right? If you know a little about relative and absolute paths inside of WordPress, you’d write : ../imagefolder/image.jpg (where “..” represents “wordpress” if you did a standard install).

    Putting “../imagefolder /image.jpg” in the SRC attribute of an IMG tag would work perfectly. So with that logic, would file_exists(“../imagefolder /image.jpg”) also work (inside WordPress)? Don’t ask me why, but no.

    After testing a bunch of different functions and looking on how to change file permissions on WordPress for a long period of time, I found the solution and it was surprisingly easy.

    Instead of putting 2 periods before the file path, put only 1. Like so:

    file_exists(“./imagefolder/image.jpg”);

    Now you can check if a file exists and/or unlink() it from anywhere.

    Hope this helps and saves you time.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘if file_exists / get_theme_data’ is closed to new replies.