• Resolved Cezar Ayran

    (@ayrancd)


    I have a custom post type and I’d like to save somewhere the latest update… I thought about creating a field under Settings and just update its value when the update is complete and then display that value using an admin notification inside of my custom post type back-end page.

    Or maybe, add a custom field to the post type page (not the item) and just update and display it, not sure if it is possible.

    This is what I have for the settings page:

    add_action('admin_init', 'my_general_section');  
    function my_general_section() {  
        add_settings_section(  
            'inventory_update_date', // Section ID 
            'Inventory Update', // Section Title
            'my_section_options_callback', // Callback
            'general' // What Page?  This makes the section show up on the General Settings Page
        );
        
        add_settings_field( // Option 1
            'inventory_update', // Option ID
            'Inventory Update', // Label
            'my_textbox_callback', // !important - This is where the args go!
            'general', // Page it will be displayed (General Settings)
            'inventory_update_date', // Name of our section
            array( // The $args
                'inventory_update' // Should match Option ID
            )  
        ); 
        
        register_setting('general','option_1', 'esc_attr');
    }
    
    function my_section_options_callback() { //section description
        echo '';  
    }
    
    function my_textbox_callback($args) {  // Textbox Callback
        $option = get_option($args[0]);
        echo '<input type="text" id="'. $args[0] .'" name="'. $args[0] .'" value="' . $option . '" />';
    }

    This code displays a custom section/field on this page /wp-admin/options-general.php but it doesn’t save its value, what am I missing? Any better idea? Tks!

Viewing 1 replies (of 1 total)
  • Thread Starter Cezar Ayran

    (@ayrancd)

    I found the error and fixed my code:

    • Creating a custom field in the settings page (WordPress will save it automatically). I made it read only because I am going to use custom code from another page to update it
    add_action('admin_init', 'my_general_section');  
    function my_general_section() {  
        add_settings_section(  
            'inventory_update_date', // Section ID 
            'Inventory Update', // Section Title
            '', // this would be the section description, just add the function name here
            'general' // What Page?  This makes the section show up on the General Settings Page
        );
        
        add_settings_field( // Option 1
            'inventory_update', // Option ID
            'Date', // Label
            'my_textbox_callback', // !important - This is where the args go!
            'general', // Page it will be displayed (General Settings)
            'inventory_update_date', // Name of our section
            array( // The $args
                'inventory_update' // Should match Option ID
            )  
        ); 
        
        register_setting('general','inventory_update', 'esc_attr');
    }
    
    function my_textbox_callback($args) {  // Textbox Callback
        $option = get_option($args[0]);
        echo '<input type="text" id="'. $args[0] .'" name="'. $args[0] .'" value="' . $option . '" readonly />';
    }
    • Updating the field using PHP
    update_option('inventory_update', current_time('m/d/Y h:i A'));
    • Displaying the custom field
    get_option('inventory_update')
Viewing 1 replies (of 1 total)
  • The topic ‘Custom setting post type field’ is closed to new replies.