Skip to content

fix: Proactively create webfonts directories on admin init#130

Open
kausaralm wants to merge 1 commit intooceanwp:masterfrom
kausaralm:fix/issue-127-auto-create-webfonts-folders
Open

fix: Proactively create webfonts directories on admin init#130
kausaralm wants to merge 1 commit intooceanwp:masterfrom
kausaralm:fix/issue-127-auto-create-webfonts-folders

Conversation

@kausaralm
Copy link

Summary

This PR fixes #127 by proactively creating the required oceanwp-webfonts and oceanwp-webfonts-css directories when an admin visits any WordPress admin page.

Problem

Currently, the plugin creates these directories on-demand when Google Fonts are loaded locally. However, if the directories do not exist before the font loading process begins, users encounter errors. This is especially problematic for:

  • Fresh installations where folders have never been created
  • Situations where folders were accidentally deleted
  • Server migrations where upload directories were not properly transferred
  • Non-technical users who cannot manually create directories via FTP/SSH

Solution

Added a lightweight admin_init hook that checks for the existence of both directories and creates them if missing. This approach was chosen for the following reasons:

  1. Minimal code footprint: Only ~15 lines added to the existing ocean.php file
  2. No user intervention required: Folders are created silently in the background
  3. No performance impact: The is_dir() check is extremely fast when folders exist
  4. Follows existing patterns: Uses the same wp_mkdir_p() function already used elsewhere in the codebase

Code Changes

Before

The directories were only created when oceanwp_get_local_webfonts_data_dir() or oceanwp_get_local_webfonts_css_data_dir() functions were called during font loading. If the font loading process failed before reaching these functions, the directories would never be created.

After

/**
 * Proactively create webfonts directories on admin init.
 * This ensures folders exist before they're needed, preventing errors.
 *
 * @since 2.5.3
 * @see https://github.com/oceanwp/ocean-extra/issues/127
 */
add_action( 'admin_init', 'oceanwp_ensure_webfonts_directories_exist' );
if ( ! function_exists( 'oceanwp_ensure_webfonts_directories_exist' ) ) {
    function oceanwp_ensure_webfonts_directories_exist() {
        $upload = wp_upload_dir();
        $base   = $upload['basedir'];

        // Create webfonts directory if missing
        $fonts_dir = trailingslashit( $base ) . 'oceanwp-webfonts';
        if ( ! is_dir( $fonts_dir ) ) {
            wp_mkdir_p( $fonts_dir );
        }

        // Create webfonts-css directory if missing
        $css_dir = trailingslashit( $base ) . 'oceanwp-webfonts-css';
        if ( ! is_dir( $css_dir ) ) {
            wp_mkdir_p( $css_dir );
        }
    }
}

Why This Helps Non-Technical Users

Before this fix, users without FTP/SSH access had no way to resolve the missing folder issue on their own. They would need to:

  1. Contact their hosting provider
  2. Hire a developer
  3. Wait for support assistance

With this fix, the problem simply does not occur. The folders are created automatically the first time any admin visits the WordPress dashboard. Users will never see folder-related errors, and the plugin becomes more plug-and-play friendly.

Testing

To verify this fix works correctly:

  1. Delete the oceanwp-webfonts and oceanwp-webfonts-css folders from /wp-content/uploads/
  2. Navigate to any WordPress admin page (e.g., Dashboard)
  3. Confirm both folders now exist in /wp-content/uploads/

Checklist

  • Code follows WordPress coding standards
  • Function is wrapped in function_exists() check for compatibility
  • Proper PHPDoc comments with @since and @see tags
  • Uses core WordPress functions (wp_upload_dir(), wp_mkdir_p(), trailingslashit())
  • No external dependencies added
  • Minimal performance impact

Fixes oceanwp#127 - Ensures oceanwp-webfonts and oceanwp-webfonts-css
directories exist before they are needed by calling wp_mkdir_p()
on admin_init hook.

This minimal approach (~15 lines) silently creates the folders
when any admin visits the dashboard, preventing the issue where
folders don't exist when custom fonts are loaded.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ocean-extra is not creating the required folders, this has caused an unexpected not needed error on all of my wordpress sites

1 participant