简体   繁体   English

如何通过插件在WordPress中修改“新帖子”表单?

[英]How do I modify the New Post form in WordPress from a plugin?

I am going to explain what I am creating so you can easily understand my problem. 我将解释我正在创建的内容,以便您可以轻松理解我的问题。 I am making a plugin that let's you add a Restaurant Menu to your website. 我正在制作一个插件,可让您将“餐厅菜单”添加到您的网站。 I currently have custom admin dashboard menu links that take you to the new post page if you want to add a dish to the restaurant menu. 我目前有自定义管理仪表板菜单链接,如果您想将菜品添加到餐厅菜单,可将您带到新的帖子页面。 I want to modify the "new post" page so that there is a price field and a thumbnail uploader as well as to change the title of the page from "Add New Page" to "Add New Dish". 我想修改“新帖子”页面,以便有一个价格字段和一个缩略图上传器,以及将该页面的标题从“添加新页面”更改为“添加新菜”。 I have created a custom post type of "dish" and I only want these changes to occur when the post type is dish. 我创建了一个自定义帖子类型“ dish”,并且只希望在帖子类型为dish时进行这些更改。 How can I add changes to the "Add New Page" form when the post type is dish? 帖子类型为盘时,如何在“添加新页面”表单中添加更改? Here is my plugin code so far... (PS) this is my first plugin! 到目前为止,这是我的插件代码(PS),这是我的第一个插件!

<?php
/* 
Plugin Name: HeadChef
Plugin URI: http://google.com
Description:  Adds functionality for the restaurant's menu
Author: John Smith
Version: Alpha
Author URI: http://google.com
*/

add_action('admin_menu', 'hc_add_menu_items');
function hc_add_menu_items() {
  // $page_title, $menu_title, $capability, $menu_slug, $callback_function
  add_posts_page(__('Restaurant Menu'), __('My Menus'), 'read', 'edit.php?post_type=dish');
}

add_action( 'init', 'hc_create_post_type' );
function hc_create_post_type(){

    register_post_type( 'dish',
        array(
          'labels' => array(
            'name' => __( 'Menu Dish' ),
            'singular_name' => __( 'Dish' )
          ),
          'public' => true,
          'has_archive' => true,
        )
      );

}

To change the labels in the UI, see the arguments of register_post_type() , some examples: 要更改UI中的标签,请参见register_post_type()的参数,例如:

$labels = array(
    'name'               => _x( 'Menu dish' ),
    'singular_name'      => _x( 'Dish' ),
    'add_new_item'       => __( 'Add new dish' ),
    'new_item'           => __( 'New dish' ),
    // ...
);

You can add content to the edit page with add_meta_box() : 您可以使用add_meta_box()将内容添加到编辑页面:

function myplugin_add_meta_box() {

    // add here which for post types you want this box
    $screens = array( 'dish' );

    add_meta_box(
        'myplugin_sectionid',
        __( 'My Post Section Title', 'myplugin_textdomain' ),
        'myplugin_meta_box_callback',
        $screens
    );
}
add_action( 'add_meta_boxes', 'myplugin_add_meta_box' );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM