未定义变量-自定义发布类型元

时间:2018-08-21 作者:thebigtine

我已经阅读了许多相关的问题,并尽我所能确保它实际上是重复的,然后再标记。

我已经使用WordPress大约6年了,所以当我说我卡住了,我是认真的。

我正在尝试向CPT添加自定义元框,boat. 下面是我的代码和错误。

错误消息:

Notice: Undefined variable: boat_make in /path/to/file/wp-content/plugins/syb/admin/class-syb-admin.php on line 150

这两个字段的错误消息相同。

这是我的代码:

/**
 * Register all of the hooks related to the admin area functionality
 * of the plugin.
 *
 * @since    1.0.0
 * @access   private
 */
private function define_admin_hooks() {

    ...

    /**
     * Add metabox and register custom fields
     *
     * @link https://code.tutsplus.com/articles/rock-solid-wordpress-30-themes-using-custom-post-types--net-12093
     */
    $this->loader->add_action( \'add_meta_boxes\', $plugin_admin, \'rerender_meta_options\' );
    $this->loader->add_action( \'save_post\', $plugin_admin, \'save_meta_options\' );

}

...

/**
 * Save Custom Fields
 *
 * @since    1.0.0
 */
public function save_meta_options() {
    if ( ! current_user_can( \'\' ) ) return;
    global $post;

    update_post_meta($post->ID, "customer_id", $_POST["customer_id"]);
    update_post_meta($post->ID, "boat_make", $_POST["boat_make"]);
}

/**
 * Create a meta box for our custom fields
 *
 * @since    1.0.0
 */ 
public function rerender_meta_options() {
    add_meta_box("boat-meta", "Boat Details", array($this, "display_meta_options"), "boat", "normal", "low");
}

/**
 * Display meta box and custom fields
 *
 * @since    1.0.0
 */  
public function display_meta_options() {
    global $post; 

    $custom = get_post_custom($post->ID);
    var_dump($custom);

    if (isset($custom["customer_id"][0])) {
        $customer_id = $custom["customer_id"][0];
    }  

    ?>
    <label><?php _e( \'Boat Colour:\', $this->plugin_name ); ?></label><input name="customer_id" value="<?php echo $customer_id; ?>" /><br>
    <?php 

    if (isset($custom["boat_make"][0])) {
        $boat_make = $custom["boat_make"][0];
    }  

    ?>
    <label><?php _e( \'Boat Make:\', $this->plugin_name ); ?></label><textarea name="boat_make"><?php echo $boat_make; ?></textarea>
    <?php
}
加载器方法:

class Syb_Loader {

/**
 * The array of actions registered with WordPress.
 *
 * @since    1.0.0
 * @access   protected
 * @var      array    $actions    The actions registered with WordPress to fire when the plugin loads.
 */
protected $actions;

/**
 * The array of filters registered with WordPress.
 *
 * @since    1.0.0
 * @access   protected
 * @var      array    $filters    The filters registered with WordPress to fire when the plugin loads.
 */
protected $filters;

/**
 * Initialize the collections used to maintain the actions and filters.
 *
 * @since    1.0.0
 */
public function __construct() {

    $this->actions = array();
    $this->filters = array();

}

/**
 * Add a new action to the collection to be registered with WordPress.
 *
 * @since    1.0.0
 * @param    string               $hook             The name of the WordPress action that is being registered.
 * @param    object               $component        A reference to the instance of the object on which the action is defined.
 * @param    string               $callback         The name of the function definition on the $component.
 * @param    int                  $priority         Optional. The priority at which the function should be fired. Default is 10.
 * @param    int                  $accepted_args    Optional. The number of arguments that should be passed to the $callback. Default is 1.
 */
public function add_action( $hook, $component, $callback, $priority = 10, $accepted_args = 1 ) {
    $this->actions = $this->add( $this->actions, $hook, $component, $callback, $priority, $accepted_args );
}

/**
 * Add a new filter to the collection to be registered with WordPress.
 *
 * @since    1.0.0
 * @param    string               $hook             The name of the WordPress filter that is being registered.
 * @param    object               $component        A reference to the instance of the object on which the filter is defined.
 * @param    string               $callback         The name of the function definition on the $component.
 * @param    int                  $priority         Optional. The priority at which the function should be fired. Default is 10.
 * @param    int                  $accepted_args    Optional. The number of arguments that should be passed to the $callback. Default is 1
 */
public function add_filter( $hook, $component, $callback, $priority = 10, $accepted_args = 1 ) {
    $this->filters = $this->add( $this->filters, $hook, $component, $callback, $priority, $accepted_args );
}

/**
 * A utility function that is used to register the actions and hooks into a single
 * collection.
 *
 * @since    1.0.0
 * @access   private
 * @param    array                $hooks            The collection of hooks that is being registered (that is, actions or filters).
 * @param    string               $hook             The name of the WordPress filter that is being registered.
 * @param    object               $component        A reference to the instance of the object on which the filter is defined.
 * @param    string               $callback         The name of the function definition on the $component.
 * @param    int                  $priority         The priority at which the function should be fired.
 * @param    int                  $accepted_args    The number of arguments that should be passed to the $callback.
 * @return   array                                  The collection of actions and filters registered with WordPress.
 */
private function add( $hooks, $hook, $component, $callback, $priority, $accepted_args ) {

    $hooks[] = array(
        \'hook\'          => $hook,
        \'component\'     => $component,
        \'callback\'      => $callback,
        \'priority\'      => $priority,
        \'accepted_args\' => $accepted_args
    );

    return $hooks;

}

/**
 * Register the filters and actions with WordPress.
 *
 * @since    1.0.0
 */
public function run() {

    foreach ( $this->filters as $hook ) {
        add_filter( $hook[\'hook\'], array( $hook[\'component\'], $hook[\'callback\'] ), $hook[\'priority\'], $hook[\'accepted_args\'] );
    }

    foreach ( $this->actions as $hook ) {
        add_action( $hook[\'hook\'], array( $hook[\'component\'], $hook[\'callback\'] ), $hook[\'priority\'], $hook[\'accepted_args\'] );
    }

}

}

2 个回复
最合适的回答,由SO网友:De Coder 整理而成

在保存元数据之前,您必须if ( ! current_user_can( \'\' ) ) return

我从未运行过current\\u user\\u can并清空功能字符串,因此我不确定您的结果会是什么,但很可能这会返回false,此时您将返回。

此外,请确保在将$\\u POST数据粘贴到数据库之前对其进行清理。

SO网友:De Coder

甚至没有设置$custom[“boat\\u make”][0],但甚至没有设置$custom[“boat\\u make”]。

因此,你不能问:if (isset($custom["boat_make"][0]))

检查您的数据库,查看是否存在“boat\\u make”的post meta

当你做你的var_dump($custom); ?

结束

相关推荐

如何在编辑或添加帖子屏幕上添加Metabox来拉取自定义帖子列表(任意两个)?

我想将metabox添加到自定义帖子列表中,并在编辑或添加帖子屏幕上选择任意两个。最后,当文章发布时,将其显示在单个文章页面上。请帮帮我!任何帮助都将不胜感激。。。。 add_action( \'add_meta_boxes\', function () { add_meta_box( \'yourcustom_sectionid\', __( \' Custom Offer Section\', \'yourtextdomain\'