問題描述
我有一個 custom-post 型別與標準元框和一些自定義欄位。如何透過前臺的表單編輯帖子?
最佳解決方法
如果您想編輯現有的帖子,請嘗試我的 Front-end Editor 外掛。
如果要建立新帖子,請嘗試以下操作之一:
次佳解決方法
我使用 Advanced Custom Fields 進行大量前端後期編輯與元框。 ACF 允許您構建許多高階元框欄位,並將它們自動新增到後端後面板。
但是還有一個 front end function 。
這種方法是完全免費的。花費任何東西的唯一方法是,如果您想使用任何更復雜的欄位型別 add-ons,如轉發器,靈活或畫廊。這一切在這裡都很好。
第一個缺點是它不包括帖子標題和描述… 但是可以很容易地將它新增到你的 functions.php 中:
/**
* Deregister admin styles on the front end when using ACF forms
*
* ACF makes sure that admin styles are queued when it loads its head, this almost always causes problems with front end forms and isn't needed for our purpose
*/
add_action( 'wp_print_styles', 'custom_acf_deregister_styles', 100 );
function custom_acf_deregister_styles()
{
if (! is_admin() )
{
wp_deregister_style( 'wp-admin' );
}
}
/**
* Save ACF field as post_content / post_title for front-end posting
*/
add_action( 'acf/save_post', 'custom_acf_save_post' );
function custom_acf_save_post( $post_id )
{
if (! is_admin() && 'acf' != get_post_type( $post_id ) ) // Don't run if adding/updated fields/field-groups in wp-admin
{
$post_title = get_post_meta( $post_id, 'form_post_title', true );
$post_content = get_post_meta( $post_id, 'form_post_content', true );
$post = get_post($post_id);
if ( ($post_title && $post_title != $post->post_title) || ($post_content && $post_content != $post->post_content) )
{
$post_data = array(
'ID' => $post_id,
);
if ( $post_content ) $post_data['post_content'] = $post_content;
if ( $post_title ) $post_data['post_title'] = $post_title;
remove_action( 'acf/save_post', 'custom_acf_save_post' );
wp_update_post( $post_data );
add_action( 'acf/save_post', 'custom_acf_save_post' );
}
}
}
/**
* Load existing post_title
*/
add_filter( 'acf/load_value/name=form_post_title', 'custom_acf_load_value_form_post_title', 10, 3 );
function custom_acf_load_value_form_post_title( $value, $post_id, $field )
{
$value = get_the_title($post_id);
return $value;
}
/**
* Load existing post_content
*/
add_filter( 'acf/load_value/name=form_post_content', 'custom_acf_load_value_form_post_content', 10, 3 );
function custom_acf_load_value_form_post_content( $value, $post_id, $field )
{
$post = get_post($post_id);
$value = $post->post_content;
return $value;
}
/**
* Install Add-ons (This adds two field groups that you can use to edit title and content)
*
* The following code will include all 4 premium Add-Ons in your theme.
* Please do not attempt to include a file which does not exist. This will produce an error.
*
* All fields must be included during the 'acf/register_fields' action.
* Other types of Add-ons (like the options page) can be included outside of this action.
*
* The following code assumes you have a folder 'add-ons' inside your theme.
*
* IMPORTANT
* Add-ons may be included in a premium theme as outlined in the terms and conditions.
* However, they are NOT to be included in a premium / free plugin.
* For more information, please read http://www.advancedcustomfields.com/terms-conditions/
*/
// Fields
add_action('acf/register_fields', 'my_register_fields');
/**
* Register Field Groups
*
* The register_field_group function accepts 1 array which holds the relevant data to register a field group
* You may edit the array as you see fit. However, this may result in errors if the array is not compatible with ACF
*/
if(function_exists("register_field_group"))
{
register_field_group(array (
'id' => 'acf_form-post-title',
'title' => 'Form Post Title',
'fields' => array (
array (
'key' => 'field_25',
'label' => 'Title',
'name' => 'form_post_title',
'type' => 'text',
'default_value' => '',
'formatting' => 'html',
),
),
'location' => array (
array (
array (
'param' => 'post_type',
'operator' => '==',
'value' => 'course',
'order_no' => 0,
'group_no' => 0,
),
),
),
'options' => array (
'position' => 'normal',
'layout' => 'no_box',
'hide_on_screen' => array (
),
),
'menu_order' => -2,
));
register_field_group(array (
'id' => 'acf_form-post-content',
'title' => 'Form Post Content',
'fields' => array (
array (
'key' => 'field_13',
'label' => 'Content',
'name' => 'form_post_content',
'type' => 'wysiwyg',
'default_value' => '',
'toolbar' => 'full',
'media_upload' => 'yes',
),
),
'location' => array (
array (
array (
'param' => 'post_type',
'operator' => '==',
'value' => 'course',
'order_no' => 0,
'group_no' => 0,
),
),
),
'options' => array (
'position' => 'normal',
'layout' => 'no_box',
'hide_on_screen' => array (
),
),
'menu_order' => -1,
));
}
這將新增所有程式碼來支援標題和內容欄位,可以新增到前端表單,如下所示:
// Add this above get_header()
// This loads styles/scripts, but it also processes the form, so pretty important
acf_form_head();
get_header();
// Where 51 is the id of the ACF field group of meta box fields that I want to add
acf_form( array(
'field_groups' => array('acf_form-post-title', 'acf_form-post-content', 51)
) );
我在這幾天工作的大多數網站上都使用這種方法。我傾向於使用 Gravity Forms 來建立一個基本的帖子,然後我使用 ACF 在前端和後端控制一切。最好的事情是,您從一個地方管理這兩個。不過值得注意的是,ACF can be used to create a post 也是如此。我沒有使用它,但會嘗試在我的下一個專案,以便我可以建立與完全元訪問。
ACF 是個人,唯一的外掛,我不能沒有重力形式作為一個接近的秒。
第三種解決方法
以下是更新帖子/頁面的基本解決方案。我新增了一個自定義元欄位的快速演示。這是非常基本的,但是會指向 plugin-less 編輯 front-end 上的帖子。這不是超級靈活,但你可以新增任何你需要的。
將此程式碼新增到您的迴圈中:
<form id="post" class="post-edit front-end-form" method="post" enctype="multipart/form-data">
<input type="hidden" name="post_id" value="<?php the_ID(); ?>" />
<?php wp_nonce_field( 'update_post_'. get_the_ID(), 'update_post_nonce' ); ?>
<p><label for="post_title">Title</label>
<input type="text" id="post_title" name="post_title" value="<?php echo $post->post_title; ?>" /></p>
<p><?php wp_editor( $post->post_content, 'postcontent' ); ?></p>
<p><label for="post_title">Test</label>
<?php $value = get_post_meta(get_the_ID(), 'edit_test', true); ?>
<input type="text" id="edit_test" name="edit_test" value="<?php echo $value; ?>" /></p>
<p><label for="post_title">Test 2</label>
<?php $value = get_post_meta(get_the_ID(), 'edit_test2', true); ?>
<input type="text" id="edit_test2" name="edit_test2" value="<?php echo $value; ?>" /></p>
<input type="submit" id="submit" value="Update" />
</form>
然後將此程式碼新增到頁面頂部以處理表單:
if ( 'POST' == $_SERVER['REQUEST_METHOD'] && ! empty($_POST['post_id']) && ! empty($_POST['post_title']) && isset($_POST['update_post_nonce']) && isset($_POST['postcontent']) )
{
$post_id = $_POST['post_id'];
$post_type = get_post_type($post_id);
$capability = ( 'page' == $post_type ) ? 'edit_page' : 'edit_post';
if ( current_user_can($capability, $post_id) && wp_verify_nonce( $_POST['update_post_nonce'], 'update_post_'. $post_id ) )
{
$post = array(
'ID' => esc_sql($post_id),
'post_content' => esc_sql($_POST['postcontent']),
'post_title' => esc_sql($_POST['post_title'])
);
wp_update_post($post);
if ( isset($_POST['edit_test']) ) update_post_meta($post_id, 'edit_test', esc_sql($_POST['edit_test']) );
if ( isset($_POST['edit_test2']) ) update_post_meta($post_id, 'edit_test2', esc_sql($_POST['edit_test2']) );
}
else
{
wp_die("You can't do that");
}
}
第四種方法
最簡單的方法是使用以下付費擴充套件程式使用像 Ninja Forms 這樣的東西:
http://wpninjas.com/downloads/front-end-posting/
你也可以自己編碼。基本上你將建立一個表單,然後使用 wp_insert_post()建立一個完整的帖子。
樣本表格:
<form action="" id="primaryPostForm" method="POST">
<fieldset>
<label for="postTitle"><?php _e('Post Title:', 'framework') ?></label>
<input type="text" name="postTitle" id="postTitle" class="required" />
</fieldset>
<fieldset>
<label for="postContent"><?php _e('Post Content:', 'framework') ?></label>
<textarea name="postContent" id="postContent" rows="8" cols="30" class="required"></textarea>
</fieldset>
<fieldset>
<input type="hidden" name="submitted" id="submitted" value="true" />
<button type="submit"><?php _e('Add Post', 'framework') ?></button>
</fieldset>
然後提交,您將儲存程式,如:
if ( isset( $_POST['submitted'] ) && isset( $_POST['post_nonce_field'] ) && wp_verify_nonce( $_POST['post_nonce_field'], 'post_nonce' ) ) {
if ( trim( $_POST['postTitle'] ) === '' ) {
$postTitleError = 'Please enter a title.';
$hasError = true;
}
$post_information = array(
'post_title' => wp_strip_all_tags( $_POST['postTitle'] ),
'post_content' => $_POST['postContent'],
'post_type' => 'post',
'post_status' => 'pending'
);
wp_insert_post( $post_information );
}
完整的程式碼和教程來自:http://wp.tutsplus.com/tutorials/creative-coding/posting-via-the-front-end-inserting/
參考文獻
注:本文內容整合自 Google/Baidu/Bing 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。