我最近开始学习WordPress . 我正在尝试为用户创建一个自定义表单,使用我在没有插件的情况下创建的自定义帖子类型发布数据 . 为此我创建了一个自定义插件并从管理员激活它 . 但是当页面加载时我看不到表单,而是将 shortcode
的名称显示为文本 . 我已经检查了另一个StackOverflow帖子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";}
?>
但是当我在页面内容编辑器中添加短代码时,页面不会呈现表单,而只是将短代码的名称显示为文本 .
Screenshot:
我提到http://teachingyou.net/wordpress/wordpress-post-from-the-front-end-with-a-custom-post-type/
据我所知,我发现参考代码与我的参考代码没有任何区别 . 我的做法有什么问题?
3 回答
您的代码中有2个问题 .
短代码应该返回一个值;他们不应该直接输出 .
当您尝试使用
new-trip-report
时,您的短代码注册为new_trip_report
返回短代码回调中的HTML并更新注册 .
进一步阅读:https://codex.wordpress.org/Function_Reference/add_shortcode
问题是你的功能
trip_report_entry_form
没有返回任何东西 .用这些代码替换该函数:
在帖子编辑器中,确保您的角色没有被转义 . 在内容编辑器的右上角,从Visual切换到HTML . 如果它们被转义,您的短代码括号将显示为
[
和]
.