简码不呈现我的表单,而是将其名称显示为文本

Shortcode does not render my form, rather showing its name as text

我最近开始学习WordPress。我正在尝试使用我在没有插件的情况下创建的自定义 post 类型为用户创建自定义表单以获取 post 数据。为此,我创建了一个自定义插件并从管理员中激活了它。但是当页面加载时我看不到表单,而是将 shortcode 的名称显示为文本。我已经检查了另一个 Whosebug post Shortcode rendering as text not as shortcode should 但无法正确理解它与我的问题的关系。

我的插件是这样的:

<?php
/*
Plugin Name: Custom frontend entry form
Description: Custom frontend entry form for publishing trip reports by users
Version: 1.0
Author: Subrata Sarkar
*/

/* Register Custom Post Type */
function trip_report_init() {
    //Set up labels
    $labels = array(
        'name' => 'Trip Report',
        'singular_name' => 'Trip Report',
        'add_new' => 'Create New Report',
        'add_new_item' => 'Create New Report',
        'edit_item' => 'Edit Trip Report',
        'new_item' => 'New Trip Report',
        'all_items' => 'All Trip Reports',
        'view_item' => 'View Trip Report',
        'search_items' => 'Search Trip Reports',
        'not_found' => 'No Trip Report Found',
        'not_found_in_trash' => 'No Trip Report found in Trash',
        'parent_item_colon' => '',
        'menu_name' => 'Trip Reports'
    );

    //register_post_type
    $args = array(
        'labels' => $labels,
        'public' => true,
        'has_archive' => true,
        'show_ui' => true,
        'capability_type' => 'post',
        'hierarchical' => false,
        'rewrite' => array('slug' => 'trip-reports'),
        'query_var' => true,
        'menu_icon' => 'dashicon-normalize',
        'supports' => array(
            'title',
            'editor',
            'excerpt',
            'comments',
            'author',
            'thumbnail',
            'page-attributes'
        )
    );

    register_post_type('trip_report', $args);

    //register taxonomy
    register_taxonomy('report_category', 'trip_report', array(
        'hierarchical' => true,
        'label' => 'Report Category',
        'query_var' => true,
        'rewrite' => array('slug' => 'report-category')
    ));

    //echo 'Trip report entry form';
}
add_action('init', 'trip_report_init');
/* Ends registration */

/* Build the form */
function trip_report_entry_form() { ?>
    <form id="trip-report-entryform" name="trip-report-entryform" method="post" action="">
        <div class="row">
            <div class="col-md-12">
                Report title <i class="red-text">*</i>
                <input type="text" id="txtTitle" name="txtTitle" class="form-control" maxmaxlenght="30" required />
            </div>
            <div class="clearfix"></div>
        </div>
        <div class="row">
            <div class="col-md-12">
                <input type="submit" value="Submit" class="btn btn-primary text-small" />
            </div>
            <div class="clearfix"></div>
        </div>
    </form>
<?php }
/* Ends form */

add_shortcode('new_trip_report', 'trip_report_entry_form');

//if ( shortcode_exists( 'new_trip_report' ) ) { echo "The shortcode exists";}

?>

但是,当我在页面内容编辑器中添加短代码时,页面不会呈现表单,而只会将短代码的名称显示为文本。

截图:

我提到了 http://teachingyou.net/wordpress/wordpress-post-from-the-front-end-with-a-custom-post-type/

据我所知,我找不到参考代码和我的参考代码之间的任何区别。我的方法有什么问题?

问题是你的函数 trip_report_entry_form 没有返回任何东西。

用这些代码替换该函数:

function trip_report_entry_form($atts) {
    $out = '';
    $out .='<form id="trip-report-entryform" name="trip-report-entryform" method="post" action="">
                <div class="row">
                    <div class="col-md-12">
                        Report title <i class="red-text">*</i>
                        <input type="text" id="txtTitle" name="txtTitle" class="form-control" maxmaxlenght="30" required />
                    </div>
                    <div class="clearfix"></div>
                </div>
                <div class="row">
                    <div class="col-md-12">
                        <input type="submit" value="Submit" class="btn btn-primary text-small" />
                    </div>
                    <div class="clearfix"></div>
                </div>
            </form>';
    return $out;
 }

在 post 编辑器中,确保您的字符没有被转义。在内容编辑器的右上角,从 Visual 切换到 HTML。您的短代码括号出现 &#91;&#93; 如果它们被转义。

您的代码中有 2 个问题。

  1. 简码应该return一个值;他们不应该直接输出。
  2. 当您尝试使用 new-trip-report
  3. 时,您的简码被注册为 new_trip_report

Return HTML 在您的短代码回调中更新注册。

function trip_report_entry_form() {
   return '<form id="trip-report-entryform" name="trip-report-entryform" method="post" action="">
        <div class="row">
            <div class="col-md-12">
                Report title <i class="red-text">*</i>
                <input type="text" id="txtTitle" name="txtTitle" class="form-control" maxmaxlenght="30" required />
            </div>
            <div class="clearfix"></div>
        </div>
        <div class="row">
            <div class="col-md-12">
                <input type="submit" value="Submit" class="btn btn-primary text-small" />
            </div>
            <div class="clearfix"></div>
        </div>
    </form>';
}
add_shortcode( 'new-trip-report', 'trip_report_entry_form' );

进一步阅读:https://codex.wordpress.org/Function_Reference/add_shortcode