[英]How to get the post slug of a custom post type?
我需要為現有的Wordpress網站添加訂單。 從主頁上單擊一個項目后,用戶將被重定向到該項目的訂單。 現在是挑戰所在,如果用戶先前已為該項目下訂單,則必須自動填寫/自動填寫訂單表格。
我使用Ninja Forms制作了表格,並為自動填充功能制作了自定義插件。 這是我編寫的自定義插件的一些代碼:
<?php
/*
...
*/
global $my_custom_plugin_table_version;
$my_custom_plugin_table_version = '1.0'; // table version if need to update
register_activation_hook( __FILE__, 'custom_plugin_create_db' );
function custom_plugin_create_db() {
global $wpdb;
global $my_custom_plugin_table_version;
$table_name = $wpdb->prefix . "my_custom_plugin_table";
$charset_collate = $wpdb->get_charset_collate();
$sql = "CREATE TABLE $table_name (
order_id VARCHAR(140) NOT NULL,
user_company VARCHAR(250) NOT NULL,
user_name VARCHAR(250) NOT NULL,
user_address VARCHAR(255) NOT NULL,
user_city VARCHAR(50) NOT NULL,
user_state VARCHAR(50) NOT NULL,
user_zip VARCHAR(50) NOT NULL,
user_phone VARCHAR(50) NOT NULL,
user_email VARCHAR(50) NOT NULL,
order_quantity INT(11) NOT NULL,
PRIMARY KEY (order_id)
) $charset_collate;";
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
dbDelta( $sql );
add_option( 'my_custom_plugin_table_version', $my_custom_plugin_table_version );
}
function custom_plugin_autofill_form( $default_value, $field_type, $field_settings ) {
$form_id = 3;
$models = Ninja_Forms()->form( $form_id )->get_subs();
$id = current( $models )->get_id();
$sub = Ninja_Forms()->form()->get_sub( $id );
foreach ( $sub->get_field_values() as $key => $value ) {
if ( $field_settings['key'] == $key ) {
$default_value = $value;
}
}
return $default_value;
}
add_action( 'init', 'check_for_previous_order' );
function check_for_previous_order() {
global $wpdb;
$table_name = $wpdb->prefix . "my_custom_plugin_table";
$user_id = get_current_user_id();
$project_name = 'project-1';
$order_id = $project_name . '-' . $user_id;
$result = $wpdb->get_row( "SELECT * FROM $table_name WHERE order_id = '$order_id'" );
if ( $result != NULL )
add_filter( 'ninja_forms_render_default_value', 'custom_plugin_autofill_form', 10, 3 );
}
do_action( 'check_for_previous_order' );
add_action( 'ninja_forms_after_submission', 'custom_plugin_save_db' );
function custom_plugin_save_db( $form_data ) {
global $wpdb;
$table_name = $wpdb->prefix . "my_custom_plugin_table";
$submitted_data = [];
foreach ( $form_data[ 'fields' ] as $field ) {
$key = $field[ 'key' ];
$value = $field[ 'value' ];
$submitted_data[ $key ] = $value;
}
$wpdb->replace( $table_name, array(
'order_id' => $submitted_data[ 'order_project_id' ] . '-' . get_current_user_id(),
'user_company' => $submitted_data[ 'user_company' ],
'user_name' => $submitted_data[ 'user_name' ],
'user_address' => $submitted_data[ 'user_address' ],
'user_city' => $submitted_data[ 'user_city' ],
'user_state' => $submitted_data[ 'user_state' ],
'user_zip' => $submitted_data[ 'user_zip' ],
'user_phone' => $submitted_data[ 'user_phone' ],
'user_email' => $submitted_data[ 'user_email' ],
'order_quantity' => $submitted_data[ 'order_quantity' ]
) );
}
提交后,它會監聽NF表單並將值保存到表中。 當顯示NF表單時,它將檢查數據庫中是否有以前的條目。 我將check_for_previous_order
函數包裝在一個動作中,以使get_current_user_id
函數正常工作。
如您所見,我通過應用order_id
保存了先前的所有訂單,該id只是項目名稱和當前用戶ID的組合。 問題在於檢索它們。 我只在代碼上寫了$project_name
和$form_id
,但我需要它們是動態的。 我為項目創建了一個自定義帖子類型的項目,每個項目都是一個項目帖子。 項目名稱是職位。 但是我不知道該怎么弄。
我嘗試了全局$ post方法。 它返回NULL。 global $pagenow
返回index.php
。
我不認為$post
對象是在add_action('init');
之前設置的add_action('init');
鈎被稱為。
您可以嘗試將其更改為add_action('template_redirect', 'check_for_previous_order');
<?php
add_action( 'template_redirect', 'check_for_previous_order' );
function check_for_previous_order() {
global $wpdb;
global $post;
echo $post->ID;
//...
}
?>
要僅針對具有易於訪問表單ID的表單的頁面觸發此操作,可以嘗試使用以下方法替換add_action('template_redirect','check_for_previous_order')
:
<?php
function check_for_previous_order($form_id) {
//form id comes in as an argument now
global $post;
}
add_action('ninja_forms_display_init', 'check_for_previous_order', 10, 1);
?>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.