Skip to content

Instantly share code, notes, and snippets.

@raftaar1191
Last active February 6, 2023 19:40
Show Gist options
  • Save raftaar1191/955454be353832f5af7c479e544d17f6 to your computer and use it in GitHub Desktop.
Save raftaar1191/955454be353832f5af7c479e544d17f6 to your computer and use it in GitHub Desktop.
Carbon Fields knows if these is a hook that triggers before the field is saved and has both the new and old values
/**
* Get the value before the save run
*/
function carbon_fields_should_save_field_value_callback( $save, $value, $field ) {
$fields_id = 'crb_text';
error_log( print_r( carbon_get_theme_option( $fields_id ), true ) . "\n", 3, WP_CONTENT_DIR . '/debug_new1.log' );
error_log( print_r( $value, true ) . "\n", 3, WP_CONTENT_DIR . '/debug_new1.log' );
return $save;
}
add_filter( 'carbon_fields_should_save_field_value', 'carbon_fields_should_save_field_value_callback', 1, 3 );
/**
* Do not delete the value before it get saved
*/
add_filter( 'carbon_fields_should_delete_field_value_on_save', '__return_false' );
@raftaar1191
Copy link
Author

raftaar1191 commented Feb 6, 2023

Fields registered

https://docs.carbonfields.net/quickstart.html

use Carbon_Fields\Container;
use Carbon_Fields\Field;

add_action( 'carbon_fields_register_fields', 'crb_attach_theme_options' );
function crb_attach_theme_options() {
    Container::make( 'theme_options', __( 'Theme Options' ) )
        ->add_fields( array(
            Field::make( 'text', 'crb_text', 'Text Field' ),
        ) );
}

add_action( 'after_setup_theme', 'crb_load' );

function crb_load() {
    require_once( 'vendor/autoload.php' );
    \Carbon_Fields\Carbon_Fields::boot();
}
```
`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment