最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Preferred way to include Advanced Custom Fields in a plugin?

programmeradmin2浏览0评论

So I am building a plugin for a site that is already using Advanced Custom Fields (ACF). I wonder what would be the best way to include ACF in my plugin? The site is already using ACF, so is it possible to include ACF from the plugin directory or should I include ACF again?

So I am building a plugin for a site that is already using Advanced Custom Fields (ACF). I wonder what would be the best way to include ACF in my plugin? The site is already using ACF, so is it possible to include ACF from the plugin directory or should I include ACF again?

Share Improve this question asked Feb 11, 2014 at 19:08 StenWStenW 2021 gold badge3 silver badges15 bronze badges 3
  • 1 Here's one method which may be useful tgmpluginactivation.com – Brad Dalton Commented Feb 11, 2014 at 19:13
  • thx man! Really appreciated, il take a look. – StenW Commented Feb 11, 2014 at 19:16
  • You have two options: 1) Included it in your plugin or theme 2) Make it a requirement for your plugin to work. The first option is generally used by premium themes where it configures all the fields. The second is done by plugins that are adding a new field type that anybody else could add themselves to their configuration. advancedcustomfields.com/resources/getting-started/… – Seamus Leahy Commented Feb 11, 2014 at 20:02
Add a comment  | 

3 Answers 3

Reset to default 3

If ACF (or any other plugin) is active on the site you do not need to include its files as they are all being included in the wordpress initialization process. The only tricky part is that you don't know the order in which files are included and yours might be included before the ACF files are included, therefor you should probably wait for the init action or even later action before using it.

Side note: ACF provides GUI for custom fields, but if you just need to use the data it is better not to rely on its API and query using the get_post_meta API. This will result in better software modularization and hopefully a more maintainable site.

This information has been updated!

Please see ACF's website: https://www.advancedcustomfields.com/resources/including-acf-in-a-plugin-theme/

Well you would just check by if ACF class exists if( !class_exists('acf') ) to check if a user has ACF installed already and if not then, fall-back on your version of ACF you would included along with your plugin or theme, setting ACF_LITE to "not" show ACF menu to the user...

But if a user decides to install ACF themselves, it will override your version included with you plugin or theme as expected :).

    // Optionally you can use below to check by plugin location:
    // if((is_plugin_active('advanced-custom-fields/acf.php'))|| (is_plugin_active('advanced-custom-fields-pro/acf.php'))):
        

        // Check if ACF Classes exists: 
        if(!class_exists('acf_pro') || !class_exists('acf')):
            
            // Define path and URL to the ACF plugin.
            define( 'MY_ACF_PATH', get_stylesheet_directory() . '/includes/acf/' );
            define( 'MY_ACF_URL', get_stylesheet_directory_uri() . '/includes/acf/' );
            
            // Include the ACF plugin.
            include_once( MY_ACF_PATH . 'acf.php' );
            
            // Customize the url setting to fix incorrect asset URLs.
            add_filter('acf/settings/url', 'my_acf_settings_url');
            function my_acf_settings_url( $url ) {
                return MY_ACF_URL;
            }
            
            // (Optional) Hide the ACF admin menu item.
            add_filter('acf/settings/show_admin', 'my_acf_settings_show_admin');
            function my_acf_settings_show_admin( $show_admin ) {
                return false;
            }
            
            
       endif;

Also. After you have made your fields and field group with ACF, export them to PHP using ACF's export feature. Then include that exported PHP into you plugin/theme.

ACF is so powerful. And I am grateful for Elliott generosity of allowing us to even use PRO into making our plugins!

Am I doing it wrong, or do others find this clunky: http://www.advancedcustomfields.com/resources/getting-started/including-acf-in-a-plugin-theme/

So in order to include ACF in my plugin and hide ACF from users in the WP admin, I need to:

  • Set up a local site with ACF
  • Export the custom fields to PHP
  • Import the custom fields PHP into my plugin

Or am I missing something?

发布评论

评论列表(0)

  1. 暂无评论