ochoarobert1 / ccontrol

Simple plugin for Client control, budgeting and invoice. Manage your business operations with ease.
https://robertochoaweb.com/
GNU General Public License v2.0
3 stars 1 forks source link

adding a client and CPT issues #7

Open LebToki opened 8 months ago

LebToki commented 8 months ago

CPT is designed in a way to force the use of title in your case you need the client name and the title so you would end up entering the data twice which is a bit of a double effort to be frank. I have added a small plugin that will allow you to enter the client name and have it do the title automatically.

/**
 * Plugin Name: CC Clients Customizations
 * Description: Customizes the CC Clients post type, setting post titles automatically from the 'nombre_cliente' custom field.
 * Version: 1.0
 * Author: Tarek Tarabichi
 */

// Function to automatically update post title based on the 'nombre_cliente' custom field. function custom_field_as_post_title_for_clients( $post_id ) { if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return; if ( 'cc_clientes' != get_post_type( $post_id ) ) return; $nombre_cliente = get_post_meta( $post_id, 'nombre_cliente', true ); if ($nombre_cliente && get_the_title($post_id) !== $nombre_cliente) { remove_action('save_post', 'custom_field_as_post_title_for_clients'); wp_update_post( array( 'ID' => $post_id, 'post_title' => $nombre_cliente, ) ); add_action('save_post', 'custom_field_as_post_title_for_clients'); } } add_action( 'save_post', 'custom_field_as_post_title_for_clients' );

// Function to automatically update Invoice post title based on the 'cliente_factura'+numero_factura+date custom field. function update_cc_invoice_title_on_save( $post_id ) { if ( 'cc_invoices' != get_post_type( $post_id ) ) return;

    // Avoid recursion
    remove_action('save_post', 'update_cc_invoice_title_on_save');

    // Get metadata values
    $cliente_factura_id = get_post_meta( $post_id, 'cliente_factura', true );
    $numero_factura = get_post_meta( $post_id, 'numero_factura', true );

    // Fetch the client's name using the ID
    $cliente_factura_name = get_the_title( $cliente_factura_id ); // Assuming it's a post reference

    // Fetch other necessary values similarly
    // For example, if numero_factura needs conversion from ID to value, perform it here

    $publish_date = get_the_date( 'Y-m-d', $post_id ); // Format as needed

    $new_title = $cliente_factura_name . ' - ' . $numero_factura . ' - ' . $publish_date;

    // Update the post
    wp_update_post( array(
        'ID'         => $post_id,
        'post_title' => $new_title,
        'post_name'  => sanitize_title($new_title) // Update slug
    ));

    // Re-hook this function
    add_action('save_post', 'update_cc_invoice_title_on_save');
}
add_action( 'save_post', 'update_cc_invoice_title_on_save' );

// Hook into the save action. add_action( 'save_post', 'update_cc_invoice_title_on_save' );

ochoarobert1 commented 8 months ago

Oh man! That's a real good idea!! thanks! I'll give it a test this weekend and left my comments here.