2017-08-22 25 views
2

我有一個目前沒有功能的插件。這是目前的結構:在自定義插件類中使用Carbon字段

<?php 
class Test 
{ 
    public function __construct() 
    { 

    } 
} 

$wpTest = new Test(); 

我想使用Carbon Fields WordPress插件。安裝完成後,我根據網站的說明改變了結構,只適應OOP。

<?php 
use Carbon_Fields\Container; 
use Carbon_Fields\Field; 

class Test 
{ 

    public function __construct() 
    { 
     add_action('carbon_fields_register_fields', array($this, 'crb_attach_theme_options')); 
     add_action('after_setup_theme', array($this , 'crb_load')); 
    } 

    public function crb_load() 
    { 
     require_once('vendor/autoload.php'); 
     \Carbon_Fields\Carbon_Fields::boot(); 
    } 

    public function crb_attach_theme_options() 
    { 
     Container::make('theme_options', __('Plugin Options', 'crb')) 
      ->add_fields(array(
       Field::make('text', 'crb_text', 'Text Field'), 
      )); 
    } 

} 

$wpTest = new Test(); 

它不起作用。我如何解決它?

+0

請說明您預期的結果是什麼,你的意思是什麼「它不工作」。它不是在做你期望的嗎?你有錯誤嗎?這可能有助於審查[我如何問一個好問題](https://stackoverflow.com/help/how-to-ask),所以你給我們提供我們需要的信息來幫助你。 – FluffyKitten

+0

@FluffyKitten最終結果應該是WordPress的管理面板中的自定義字段。沒有錯誤或通知。因此,我問了一個稍微含糊的問題,尋求幫助。 –

+0

請在您的wp-config.php中啓用「WP_DEBUG」並再次檢查錯誤。你是否肯定這個代碼包含在functions.php中?另外,你確定包含'vendor/autoload.php'嗎? –

回答

2

我找到了我的問題的答案。從部分來看,問題是我在訪問__construct()後連接了vendor/autoload.php

解決以下

use Carbon_Fields\Container; 
use Carbon_Fields\Field; 



class PluginOption 
{ 

    public function __construct() 
    { 
     require_once('vendor/autoload.php'); 
     \Carbon_Fields\Carbon_Fields::boot(); 
     add_action('carbon_fields_register_fields', array($this, 'crb_attach_theme_options')); 
    } 

    public function crb_attach_theme_options() 
    { 
     Container::make('theme_options', __('Plugin Option', 'crb')) 
     ->add_fields(array(
      Field::make('text', 'crb_text', 'Text Field'), 
      )); 
    } 

} 

$wpTest = new PluginOption(); 
0

從筆者的問題本身answer此任務可以爲他自己特定的目的工作的一個例子。

但是,如果您長期以來提出這個問題,您可能希望在自己的插件中整合Carbon Fields(由於此問題的表述)。在這種情況下,您至少應該注意一個問題,即碳田數據可用的點;以防你想在你插件執行的時候檢索Carbon Fields數據。

TL; DR:carbon_fields_fields_registered動作鉤子是您可以獲取Carbon Fields值的最早階段。首先必須在carbon_fields_register_fields動作鉤子中定義這些字段。有關其他解釋,你也可以看看this answer

所以這裏是一個引導,使得確保有一個適當的時機:

use Carbon_Fields\Container; 
use Carbon_Fields\Field; 

class YourFancyPlugin 
{ 

    public function __construct() 
    { 
     add_action('after_setup_theme', array($this, 
      'load_carbon_fields' 
     )); 

     add_action('carbon_fields_register_fields', array($this, 
      'register_carbon_fields' 
     )); 

     /* will succesfuly retrieve the data of the fields registered at 
     * carbon_fields_register_fields action hook 
     * if you retrieve the data before carbon_fields_fields_registered action hook 
     * has fired it won't work 
     */ 
     add_action('carbon_fields_fields_registered', array($this, 
      // picked this name only to emphasize whats going on 
      'carbon_fields_values_are_available' 
     )); 

     /* do all the stuff that doesn't rely on values of your Carbon Fields */ 
    } 

    public function load_carbon_fields() 
    { 
     require_once 'vendor/autoload.php'; // modify depending on your actual setup 
     \Carbon_Fields\Carbon_Fields::boot(); 
    } 

    public function register_carbon_fields() 
    { 
     Container::make('theme_options', 'YourFancyPlugin options') 
      -> add_fields(array(
       Field::make('text', 'YourFancyPlugin_option_1') 
      )); 
    } 

    public function carbon_fields_values_are_available() 
    { 
     /* retrieve the values of your Carbon Fields related to your plugin */ 
     var_dump(carbon_get_theme_option('YourFancyPlugin_option_1')); 
     /* do all the stuff that does rely on values of your Carbon Fields */ 
    } 

} 

new YourFancyPlugin(); 
相關問題