制作自定义POST类型插件-继续获得死亡的白屏

时间:2017-01-12 作者:Danielle

我对WordPress开发非常陌生,我正试图在我的插件文件夹中为我的网站制作一个自定义的post类型的业务目录样式插件,但每次我激活它,我就会在整个网站上看到一个白色的死亡屏幕。我确实找到了这个插件,但我看不出是什么导致了它。

<?php
/*
    Plugin Name: Special Coffee CPT
    Plugin URI: http://danijoypractice.x10host.com
    Description: This plugin creates a custom post type & template page
    Author: Danielle Rautiainen
    Version: 1.0
    Author URI: http://danijoypractice.x10host.com
*/

add_action(\'init\', \'local_business_directory_register\'); 

function local_business_directory_register() { 

$args = array( 
  \'label\' => __(\'Business Directory\'), 
  \'singular_label\' => __(\'Business\'), 
  \'public\' => true, 
  \'taxonomies\' => array(\'category\'),
  \'show_ui\' => true, 
  \'capability_type\' => \'post\', 
  \'hierarchical\' => true, 
  \'has_archive\' => true,
  \'supports\' => array(\'title\', \'editor\', ),
  \'rewrite\' => array(\'slug\' => \'businesses\', \'with_front\' => false),
  ); 
}


  register_post_type( \'businesses\' , $args );
  register_taxonomy("business-type", array("businesses"), array(
  "hierarchical" => true,
  "label" => "Business Type",
  "singular_label" => "Business Type",
  "rewrite" => true
     )
    );

add_action("admin_init", "local_business_directory_meta");

function local_business_directory_meta ()
{
 add_meta_box("business-meta", "Business Options",     "local_business_directory_options", "businesses", "normal", "high");
} 

function local_business_directory_options()
{ 
 global $post; 
if ( defined(\'DOING_AUTOSAVE\') && DOING_AUTOSAVE ) return $post_id;
$custom = get_post_custom($post->ID); 
$address = $custom["address"][0];
$website = $custom["website"][0]; 
$phone = $custom["phone"][0]; 
?> 
 <style type="text/css">
 <?php include(\'business-directory.css\'); ?>
 </style>

 <div class="business_directory_extras">
 <?php $website= ($website == "") ? "http://" : $website; ?>
  <div>
   <label>Website:</label>
   <input name="website" value="<?php echo $website; ?>" />
  </div>
  <div>
   <label>Phone:</label>
   <input name="phone" value="<?php echo $phone; ?>" />
  </div>
  <div>
  <label>Address:</label>
   <textarea name="address"><?php echo $address; ?>" /></textarea>
  </div>
 </div> 
 <?php 
} 
add_action(\'save_post\', \'local_business_directory_save_extras\'); 
function business_manager_save_extras(){  
global $post;  
if ( defined(\'DOING_AUTOSAVE\') && DOING_AUTOSAVE ){

   return $post_id;
   }else{
     update_post_meta($post->ID, "website", $_POST["website"]); 
     update_post_meta($post->ID, "address", $_POST["address"]);
     update_post_meta($post->ID, "phone", $_POST["phone"]);
   } 
  }  

?>
我错过什么了吗?任何帮助都将不胜感激!

2 个回复
SO网友:codiiv

您正在$args数组之后关闭函数。你需要把

function local_business_directory_register() { 

$args = array( 
  \'label\' => __(\'Business Directory\'), 
  \'singular_label\' => __(\'Business\'), 
  \'public\' => true, 
  \'taxonomies\' => array(\'category\'),
  \'show_ui\' => true, 
  \'capability_type\' => \'post\', 
  \'hierarchical\' => true, 
  \'has_archive\' => true,
  \'supports\' => array(\'title\', \'editor\', ),
  \'rewrite\' => array(\'slug\' => \'businesses\', \'with_front\' => false),
  ); 

  register_post_type( \'businesses\' , $args );
  register_taxonomy("business-type", array("businesses"), array(
  "hierarchical" => true,
  "label" => "Business Type",
  "singular_label" => "Business Type",
  "rewrite" => true
     )
    );

}

SO网友:Tunji

您的代码中有几个错误。

NOTE: You should debug your errors using your error logs. :|

首先,您添加register_post_type( \'businesses\' , $args ); 在您的local_business_directory_register() 作用

其次,你在引用$post_id 在两个不同的地方,没有设置变量。因为您定义global $post, 您可以参考post ID 使用$post->ID

<?php
/*
    Plugin Name: Special Coffee CPT
    Plugin URI: http://danijoypractice.x10host.com
    Description: This plugin creates a custom post type & template page
    Author: Danielle Rautiainen
    Version: 1.0
    Author URI: http://danijoypractice.x10host.com
*/

add_action(\'init\', \'local_business_directory_register\');

function local_business_directory_register() {

    $args = array(
        \'label\' => __(\'Business Directory\'),
        \'singular_label\' => __(\'Business\'),
        \'public\' => true,
        \'taxonomies\' => array(\'category\'),
        \'show_ui\' => true,
        \'capability_type\' => \'post\',
        \'hierarchical\' => true,
        \'has_archive\' => true,
        \'supports\' => array(\'title\', \'editor\', ),
        \'rewrite\' => array(\'slug\' => \'businesses\', \'with_front\' => false),
    );
    register_post_type( \'businesses\' , $args );
}


register_taxonomy("business-type", array("businesses"), array(
        "hierarchical" => true,
        "label" => "Business Type",
        "singular_label" => "Business Type",
        "rewrite" => true
    )
);

add_action("admin_init", "local_business_directory_meta");

function local_business_directory_meta ()
{
    add_meta_box("business-meta", "Business Options",     "local_business_directory_options", "businesses", "normal", "high");
}

function local_business_directory_options()
{
    global $post;
    if ( defined(\'DOING_AUTOSAVE\') && DOING_AUTOSAVE ) return $post->ID;
    $custom = get_post_custom($post->ID);
    $address = $custom["address"][0];
    $website = $custom["website"][0];
    $phone = $custom["phone"][0];
    ?>
    <style type="text/css">
        <?php include(\'business-directory.css\'); ?>
    </style>

    <div class="business_directory_extras">
        <?php $website= ($website == "") ? "http://" : $website; ?>
        <div>
            <label>Website:</label>
            <input name="website" value="<?php echo $website; ?>" />
        </div>
        <div>
            <label>Phone:</label>
            <input name="phone" value="<?php echo $phone; ?>" />
        </div>
        <div>
            <label>Address:</label>
            <textarea name="address"><?php echo $address; ?>" /></textarea>
        </div>
    </div>
    <?php
}
add_action(\'save_post\', \'local_business_directory_save_extras\');
function business_manager_save_extras(){
    global $post;
    if ( defined(\'DOING_AUTOSAVE\') && DOING_AUTOSAVE ){

        return $post->ID;
    }else{
        update_post_meta($post->ID, "website", $_POST["website"]);
        update_post_meta($post->ID, "address", $_POST["address"]);
        update_post_meta($post->ID, "phone", $_POST["phone"]);
    }
}

?>

相关推荐

插件放置在/wp-content/plugins内的文件夹中时不保存值

我得到了WordPRess插件的以下代码,它在每个页面/后期编辑屏幕上添加了两个自定义输入。然后将这些值保存并输出到前端页面的标题中。如果代码位于内部,则可以正常工作。php文件并直接放入“wp内容/插件”。然而,如果我把它放在插件(如“wp-content/plugins/myplugin”)中自己的文件夹中,那么在通过编辑屏幕保存帖子/页面时,输入字段不会保存。此外,它不会向前端页面html标题部分输出任何内容。这似乎是一个被放弃的项目,所以我无法与原始开发人员一起制定解决方案。然而,代码中的某些内容