carbon-fields icon indicating copy to clipboard operation
carbon-fields copied to clipboard

directory_to_url() returning empty string

Open RBFraphael opened this issue 1 year ago • 2 comments

Version

  • Carbon Fields: 3.6.3
  • WordPress: 6.4.3
  • PHP: 8.3.3

Expected Behavior

Calling \Carbon_Fields\Carbon_Fields\directory_to_url(<dir>) returns corresponding URL to a directory inside the theme.

Actual Behavior

The function is returning an empty string.

Container definition

add_action("after_setup_theme", "BootCarbonFields");

function BootCarbonFields()
{
    Carbon_Fields::boot();
    var_dump(\Carbon_Fields\DIR);
    var_dump(__DIR__);
    var_dump(\Carbon_Fields\Carbon_Fields::directory_to_url(\Carbon_Fields\DIR));
    die;
}

Steps to Reproduce the Problem

  1. Install Carbon Fields on your theme
  2. Call Carbon_Fields::boot()
  3. Try to use \Carbon_Fields\Carbon_Fields::directory_to_url() with any directory of your theme as parameter

Comments

First, I'm not using this method for any purpose directly on my theme. The problem started after I've noticed my Wordpress wasn't loading any fields on admin, and, when verifying the Console, it is showing 404 errors for some css and js files:

  • core/Libraries/Sidebar_Manager/assets/css/app.css?ver=3.6.3
  • core/Libraries/Sidebar_Manager/assets/js/app.js?ver=3.6.3
  • build/classic/core.min.css?ver=3.6.3
  • build/classic/metaboxes.min.css?ver=3.6.3
  • build/classic/vendor.min.js?ver=3.6.3
  • build/classic/core.min.js?ver=3.6.3
  • build/classic/metaboxes.min.js?ver=3.6.3

All of them are tried to load directly from the root of my website, instead of loading from theme's Carbon Field. When verifying the source code, I've found that those files are prefixed with \Carbon_Fields\URL constant, which is empty. And, when searching for the declaration of that constant, it uses the directory_to_url() method, which, after some tests, I've noticed that is always returning an empty string.

RBFraphael avatar Feb 16 '24 12:02 RBFraphael

A possible workaround for that problem is to manually set the constant before calling Carbon_Fields::boot().

add_action("after_setup_theme", "BootCarbonFields");
function BootCarbonFields()
{
   define("Carbon_Fields\URL", get_template_directory_uri()."/vendor/htmlburger/carbon-fields/");
   Carbon_Fields::boot();
}

RBFraphael avatar Feb 16 '24 13:02 RBFraphael