Php 如何获取自定义post类型的post slug?

Php 如何获取自定义post类型的post slug?,php,wordpress,e-commerce,backend,ninja-forms,Php,Wordpress,E Commerce,Backend,Ninja Forms,我需要添加现有Wordpress网站的订单。从主页中单击项目后,用户将被重定向到该项目的订单。现在面临的挑战是,如果用户以前为该项目下过订单,订单必须自动填充/自动填充条目 我使用忍者表单制作表单,并为自动填充功能制作了自定义插件。以下是我编写的自定义插件的一些代码: <?php /* ... */ global $my_custom_plugin_table_version; $my_custom_plugin_table_version = '1.0'; // table vers

我需要添加现有Wordpress网站的订单。从主页中单击项目后,用户将被重定向到该项目的订单。现在面临的挑战是,如果用户以前为该项目下过订单,订单必须自动填充/自动填充条目

我使用忍者表单制作表单,并为自动填充功能制作了自定义插件。以下是我编写的自定义插件的一些代码:

<?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' ]
    ) );
}

我不认为
$post
对象是在
add_action('init')之前设置的钩子被调用

您可以尝试将其更改为
add_action('template_redirect','check_for_previous_order')


哦,这就是为什么
$post
返回
NULL
!谢谢你。现在我只需要获取表单ID。@johnjullies查看
ninja\u forms\u display\u init
操作。@johnjullies我将用一个例子更新我的答案,这个例子应该会有帮助。你在哪里找到了
ninja\u forms\u display\u init
?我如何将其集成到我的
自动填充表单
函数中?现在我检查了,显然用
忍者表单显示
替换
模板重定向
不起作用
<?php
add_action( 'template_redirect', 'check_for_previous_order' );
function check_for_previous_order() {
    global $wpdb;
    global $post;
    echo $post->ID;
    //...
}
?>
<?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);
?>