1

I'm adding Advanced Custom Fields Pro (v5) to a simple plugin I'm creating.

I'm using the tutorial on the ACF website: Distributing ACF in a plugin/theme. The tutorial calls for using get_stylesheet_directory(), but that is for a theme. I replaced that with dirname(__FILE__).

In step 3, include_once works fine with dirname(__FILE__). Steps 1 and 2 also work for including files, but I run into an issue when Javascript and CSS files are included by ACF. It returns URLs like the following:

http://example.com/templates/wp-starter/nfs/c05/h03/mnt/70376/domains/example.com/html/templates/wp-starter/wp-content/plugins/simple/acf/css/global.css?ver=5.0.0

This is what I would like to get:

http://example.com/templates/wp-starter/wp-content/plugins/simple/acf/css/global.css?ver=5.0.0.

I am using Media Temple's grid server (shared hosting).

Here is the relevant code from plugins/simple/simple.php:

// 1. Customize ACF path
add_filter('acf/settings/path', 'my_acf_settings_path');

function my_acf_settings_path( $path ) {
    // update path
    $path = dirname( __FILE__ ) . '/acf/';

    // return
    return $path;  
}

// 2. Customize ACF dir
add_filter('acf/settings/dir', 'my_acf_settings_dir');

function my_acf_settings_dir( $dir ) {
    // update path
    $dir = dirname( __FILE__ ) . '/acf/';

    // return
    return $dir;  
}

// 3. Include ACF
include_once( dirname( __FILE__ ) . '/acf/acf.php' );

How can I get the correct URL for the scripts and stylesheets?

I tried using the following in config.php, but it didn't work either:

define('WP_CONTENT_DIR', 'http://example.com/templates/wp-starter/wp-content');   
define('WP_PLUGIN_DIR', 'http://example.com/templates/wp-starter/wp-content/plugins');

I could enqueue these scripts and stylesheets separately, but it would be nice if I didn't have to do that.

Mark Rummel
  • 2,920
  • 10
  • 37
  • 52
  • possible duplicate of [$\_SERVER\['DOCUMENT\_ROOT'\] returns path with repeated directory name](http://stackoverflow.com/questions/24123839/serverdocument-root-returns-path-with-repeated-directory-name)... Maybe related, DOCUMENT_ROOT can be skewed by incorrectly configured apache – AeroX Oct 13 '14 at 18:01

2 Answers2

1

I resolved the issue by using $dir = plugins_url() . '/simple/acf/'; in step 2. I kept everything else the same.

// 2. Customize ACF dir
add_filter('acf/settings/dir', 'my_acf_settings_dir');

function my_acf_settings_dir( $dir ) { 
  // update path
  $dir = plugins_url() . '/simple/acf/';

  // return
  return $dir; 
}
Mark Rummel
  • 2,920
  • 10
  • 37
  • 52
  • 1
    This also solved the reverse problem, which I had, where these files linked full machine paths instead of website paths and wouldn't load in the local environment but worked on the production and staging boxes. Thanks! – Mike Lyons Feb 03 '15 at 22:35
  • http://support.advancedcustomfields.com/forums/topic/acf-admin-cssjs-not-loading/ This is a similar solution, I actually ended up making my solution a mix of both. Because this problem was what I actually encountered, but I used `plugins_url()` instead of `get_bloginfo('url')` Thanks again – Mike Lyons Feb 03 '15 at 23:44
0

The accepted answer didn't work for symlinked directories (e.g. those deploying via capistrano) which this answer was worded closely (that's why this keeps on coming up when googling).

The fix that would work for symlinks can be found here.

I just wanted to post a working solution, based on @elliotcondon earlier suggestion #124 (comment)

Place this into your theme functions.php file.

add_action('init', "initHook", 0);  // We use the 0 to bypass priority

function initHook() {
    if(function_exists('acf')) {
        acf()->settings["dir"] = WP_CONTENT_URL."/plugins/advanced-custom-fields/";
    }     }
Rystraum
  • 1,985
  • 1
  • 20
  • 32