2010-08-06 76 views
2

我正在處理一些自定義文章類型。我完成了第一個,並意識到我使用的metabox代碼可以被其他,將來的自定義帖子類型(以及頁面,帖子等)重新使用。所以我把這個類放在自己的PHP文件中,並將其包含在我的子主題的functions.php文件中。我以前從未使用過課程,但是我認爲我可以隨時從任何地方調用該課程,因爲這是我爲功能所做的。相反,我從我的post-types.php文件中調用它,並得到以下錯誤:從其他文件調用php類

「致命錯誤:類'My_meta_box'在D:\ helga \ xampp \ htdocs \ plagueround \ wp-content \ themes \ plagueround_new2 \功能\ - types.php後上線73"

在我的孩子主題的functions.php,我包括在該文件在我的生活類:

define('CHILDTHEME_DIRECTORY', get_stylesheet_directory() . '/'); 
// Meta Box Class - makes meta boxes 
require_once(CHILDTHEME_DIRECTORY . 'functions/meta_box_class.php'); 

這裏是我的類

//My Meta Box CLASS 
//from: http://www.deluxeblogtips.com/2010/05/howto-meta-box-wordpress.html 

class My_meta_box { 

     protected $_meta_box; 

     // create meta box based on given data 
     function __construct($meta_box) { 
     $this->_meta_box = $meta_box; 
     add_action('admin_menu', array(&$this, 'add')); 

     add_action('save_post', array(&$this, 'save')); 
    } 

    /// Add meta box for multiple post types 
    function add() { 
     foreach ($this->_meta_box['pages'] as $page) { 
      add_meta_box($this->_meta_box['id'], $this->_meta_box['title'], array(&$this, 'show'), $page, $this->_meta_box['context'], $this->_meta_box['priority']); 
     } 
    } 

    // Callback function to show fields in meta box 
    function show() { 
     global $post; 

     // Use nonce for verification 
     echo '<input type="hidden" name="mytheme_meta_box_nonce" value="', wp_create_nonce(basename(__FILE__)), '" />'; 

     echo '<table class="form-table">'; 

     foreach ($this->_meta_box['fields'] as $field) { 
      // get current post meta data 
      $meta = get_post_meta($post->ID, $field['id'], true); 

      echo '<tr>', 
        '<th style="width:20%"><label for="', $field['id'], '">', $field['name'], '</label></th>', 
        '<td>'; 
      switch ($field['type']) { 
       case 'text': 
        echo '<input type="text" name="', $field['id'], '" id="', $field['id'], '" value="', $meta ? $meta : $field['std'], '" size="30" style="width:97%" />', 
         '<br />', $field['desc']; 
        break; 
       case 'textarea': 
        echo '<textarea name="', $field['id'], '" id="', $field['id'], '" cols="60" rows="4" style="width:97%">', $meta ? $meta : $field['std'], '</textarea>', 
         '<br />', $field['desc']; 
        break; 
       case 'select': 
        echo '<select name="', $field['id'], '" id="', $field['id'], '">'; 
        foreach ($field['options'] as $option) { 
         echo '<option', $meta == $option ? ' selected="selected"' : '', '>', $option, '</option>'; 
        } 
        echo '</select>'; 
       break; 
       case 'select2': //for when value and display text don't match 
           //$options array must be multidimensional with both the 'value' and the 'text' for each option 
           //for example = array(array(text=>text1,value=>value1),array(text=>text2,value=>value2)) 
           //then in <option> value = $option['value'] and text = $option['text'] 
        echo '<select name="', $field['id'], '" id="', $field['id'], '">'; 
        foreach ($field['options'] as $option) { 
         echo '<option value="',$option['value'],'"', $meta == $option['value'] ? ' selected="selected"' : '', '>', $option['desc'], '</option>'; 
        } 
        echo '</select>'; 
        break; 
       case 'radio': 
        foreach ($field['options'] as $option) { 
         echo '<input type="radio" name="', $field['id'], '" value="', $option['value'], '"', $meta == $option['value'] ? ' checked="checked"' : '', ' />', $option['name']; 
        } 
        break; 
       case 'checkbox': 
        echo '<input type="checkbox" name="', $field['id'], '" id="', $field['id'], '"', $meta ? ' checked="checked"' : '', ' />'; 
        break; 
      } 
      echo '<td>', 
       '</tr>'; 
     } 

     echo '</table>'; 
    } 

    // Save data from meta box 
    function save($post_id) { 
     // verify nonce 
     if (!wp_verify_nonce($_POST['mytheme_meta_box_nonce'], basename(__FILE__))) { 
      return $post_id; 
     } 

     // check autosave 
     if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) { 
      return $post_id; 
     } 

     // check permissions 
     if ('page' == $_POST['post_type']) { 
      if (!current_user_can('edit_page', $post_id)) { 
       return $post_id; 
      } 
     } elseif (!current_user_can('edit_post', $post_id)) { 
      return $post_id; 
     } 

     foreach ($this->_meta_box['fields'] as $field) { 
      $old = get_post_meta($post_id, $field['id'], true); 
      $new = $_POST[$field['id']]; 

      if ($new && $new != $old) { 
       update_post_meta($post_id, $field['id'], $new); 
      } elseif ('' == $new && $old) { 
       delete_post_meta($post_id, $field['id'], $old); 
      } 
     } 
    } 
} 

然後在我定義自定義類型後,我有一些元打我types.php後的文件:

$prefix = '_events_'; 
$post_id = $_GET['post'] ? $_GET['post'] : $_POST['post_ID'] ; 

$events_meta_box = array(
    'id' => 'wpt_events', 
    'title' => 'Events Options', 
    'pages' => array('events'), // multiple post types possible 
    'context' => 'side', 
    'priority' => 'low', 
    'fields' => array(
     array(
      'name' => 'Headline', 
      'desc' => 'Enter the heading you\'d like to appear before the video', 
      'id' => $prefix . 'heading1', 
      'type' => 'text', 
      'std' => '' 
     ), 

    ) 

); 

$my_box = new My_meta_box($events_meta_box); 

所以 - 我該如何引用類的其他文件?

EDITS/CONTINUATION

簡單地移動放在require_once語句後types.php(一個試圖調用類)似乎工作。但它赫克我試過自動加載腳本

function __autoload($className) 
{ 
     require(CHILDTHEME_DIRECTORY .'functions/class_' . $className . '.php') ; 
} 

,我改名爲我的類文件class_My_meta_box.php和My_meta_box是類名

這將導致以下錯誤: 警告:要求( D:\ helga \ xampp \ htdocs \ plagueround/wp-content/themes/plagueround_new/functions/class_WP_User_Search.php)[function.require]:未能打開流:在D:\ helga \ xampp \ htdocs中沒有這樣的文件或目錄\ plagueround \ wp-content \ themes \ plagueround_new \ functions.php on line 66

致命錯誤:require()[function.require]:Fail D:\ helga \ xampp \ htdocs \ plagueround/wp-content/themes/plagueround_new/functions/class_WP_User_Search.php'(include_path ='.; D:\ helga \ xampp \ php \ PEAR')在D: \ helga \ xampp \ htdocs \ plagueround \ wp-content \ themes \ plagueround_new \ functions.php 66行

我不完全明白它說什麼,但我認爲它試圖自動加載一些默認的wordpress類。 ..並正在我的目錄,顯然不存在。

+0

您的metabox.php沒有被包括在內。你確定require_once語句加載正確嗎? – 2010-08-06 17:52:04

+0

@Byron:如果文件不存在,'require' /'require_once'會導致致命錯誤。與Helga不同的致命錯誤正在變得越來越嚴重。 – Powerlord 2010-08-06 18:00:20

+0

該死的你都快。謝謝!以及我測試了帶有jquery警報的require_once語句,所以我很確定它正在正確加載。它似乎工作時,我將require語句移動到post-types.php我在哪裏引用類 – helgatheviking 2010-08-06 18:28:38

回答

4

實際上,如果該文件包含一個類或其他什麼東西它並不重要。爲了能夠從其他文件執行代碼,PHP必須知道文件的位置,所以這是您的常規requireinclude

例子:

// foo.class.php 
class Foo {} 

// main.php 
require_once '/path/to/foo.class.php'; 
$foo = new Foo; 

當使用類,它實際上是有道理的use a Naming Convention並使用Autoloader,所以PHP will try to include the class on it's own,無需你需要或包括它。

+0

謝謝!我一定會檢查出來的。就像我說我是新來的班級,並認爲我可以通過我的功能知識來吱吱聲。 – helgatheviking 2010-08-06 18:30:05

+0

原來的問題是,我的CHILDTHEME_DIRECTORY常量不是一個PATH。應該已經使用 require_once(STYLESHEETPATH。'/functions/meta_box_class.php'); 而不是 require_once(CHILDTHEME_DIRECTORY。'functions/meta_box_class.php'); – helgatheviking 2011-07-26 17:32:56

0

只需使用絕對路徑:

require_once("fullpath/functions/meta_box_class.php") or die ("Could Not load file"); 
+5

當我使用'require'時,我不認爲你需要'或die'語句,好像'require'腳本失敗將退出並出現嚴重錯誤。 – tj111 2010-08-06 17:54:43

0

看起來像meta_box_class.php沒有被包括在內。要找出哪些文件已被列入在任何時候,試試:

var_dump(get_included_files()); 
+0

嘗試了var_dump,它肯定包含在內 – helgatheviking 2010-08-06 19:17:48

+0

嗯,在meta_box_class.php開頭有沒有'<?php'? (我在這裏抓着吸管;)) – Lethargy 2010-08-06 20:50:52

+0

是的,我這樣做。我把這個包含到了調用這個類的頁面中,似乎解決了這個問題。不知道爲什麼,我要去運行它。感謝您的建議。 – helgatheviking 2010-08-08 13:45:19

1

可以使用常量WP_PLUGIN_DIR描述的路徑。

// Foo.class.php 
class Foo {} 

// main.php 
require_once WP_PLUGIN_DIR.'/Foo.class.php'; 
$foo = new Foo;