Search code examples
phpwordpresse-commercebackendninja-forms

How to get the post slug of a custom post type?


I need to add order forms for an existing Wordpress website. After clicking a Project from the homepage, the user will be redirected to the order form for that Project. Now here's the challenge, the order form must be auto populated/autofilled with entries if the user previously made an order for that Project.

I made the forms using Ninja Forms and made a custom plugin for the autofill feature. Here's some of the code of the custom plugin that I wrote:

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

It listens to a NF form and saves the values to a table after a submission. And when an NF form is displayed, it checks the database for any previous entry. I wrapped the check_for_previous_order function in an action to make the get_current_user_id function to work.

As you can see, I saved every previous order by applying an order_id which is just the combination of the Project name and the current user ID. The problem is in retrieving them. I've only wrote the $project_name and $form_id on the code but I need them to be dynamic. I've made a custom post type Project for the projects and each project is a Project post. The Project name is the post slug. But I can't figure out how to get the slug.

I've tried the global $post method. It returns NULL. global $pagenow returns index.php.


Solution

  • i don't think the $post object is set before add_action('init'); hooks are called.

    you can try changing that to 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;
        //...
    }
    ?>
    

    to trigger this for only pages with a form with easy access to the form ids you can try replacing your add_action('template_redirect','check_for_previous_order') with this:

    <?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);
    ?>