1

我正在将 Advanced Custom Fields Pro (v5) 添加到我正在创建的简单插件中。

我正在使用 ACF 网站上的教程:Distributing ACF in a plugin/theme。本教程要求使用get_stylesheet_directory(),但这是一个主题。我用dirname(__FILE__).

在第 3 步中,include_oncedirname(__FILE__). 步骤 1 和 2 也适用于包含文件,但是当 ACF 包含 Javascript 和 CSS 文件时我遇到了问题。它返回如下 URL:

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

这就是我想要得到的:

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

我正在使用 Media Temple 的网格服务器(共享主机)。

以下是来自的相关代码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' );

如何获取脚本和样式表的正确 URL?

我尝试在 中使用以下内容config.php,但它也不起作用:

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');

我可以将enqueue这些脚本和样式表分开,但如果我不必这样做就好了。

4

2 回答 2

1

我通过$dir = plugins_url() . '/simple/acf/';在第 2 步中使用解决了这个问题。我保持其他一切不变。

// 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; 
}
于 2014-10-13T18:21:58.733 回答
0

接受的答案不适用于符号链接的目录(例如,通过 capistrano 部署的目录),这个答案的措辞很严密(这就是为什么在谷歌搜索时不断出现这种情况)。

可以在此处找到适用于符号链接的修复程序。

我只是想根据@elliotcondon 之前的建议#124(评论)发布一个可行的解决方案

将其放入您的主题 functions.php 文件中。

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/";
    }     }
于 2015-07-23T01:43:10.420 回答