PHP code example of ssovit / wp-util

1. Go to this page and download the library: Download ssovit/wp-util library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

ssovit / wp-util example snippets


$admin = new \Sovit\Utilities\Admin_Setting();
$admin->set_page_id("my-setting-page")
        ->set_capability('manage_options')
        ->set_page_title(esc_html__('My Setting'))
        ->set_menu_title(esc_html__('My Setting'))
        ->set_setting_key("my_setting_name")
        ->set_icon('dashicons-admin-links');

add_filter("sovit/settings/{$page_id}/tabs",function($tabs){
    $tabs["my_tab"]=[
        "label"=>__("My Tab","textdomain")
    ];
    return $tabs;
});

add_filter("sovit/settings/{$page_id}/tabs",function($tabs){
    $tabs["my_tab"]=[
        "label"=>__("My Tab","textdomain"),
        "render_callback"=>"my_tab_callback"
    ];
    return $tabs;
});
function my_tab_callback(){
    echo "My tab Content";
}

add_filter("sovit/settings/{$page_id}/tabs",function($sections){
    $sections["general"]=[
        "label"=>__("General Settings","textdomain")
    ];
    return $sections;
});

add_filter("sovit/settings/{$page_id}/sections",function($sections){
    $sections["general_setting"]=[
        "label"=>__("General Settings","textdomain"),
        "render_callback"=>"my_section_callback"
    ];
    return $sections;
});
function my_section_callback(){
    echo "My Section Content";
}

add_filter("sovit/settings/{$page_id}/fields",function($fields){
    $fields["fruit"]=[
        "label"=>__("Fruit","textdomain"),
        'section' => "general_setting",
        'tab' => "general",
        'type'    => 'select',
        "options" =>  [
            "apple" => __("Apple", "textdomain"),
        ],
        'desc' => esc_html__('Select a fruit', "textdomain"),
        // There are other options based on field types
        // see lib/Controls.php
    ];
    return $fields;
});

add_filter("sovit/settings/{$page_id}/fields",function($sections){
    $fields["fruit"]=[
        "label"=>__("Fruit","textdomain"),
        "render_callback"=>"my_custom_field",
        // .. other field data
    ];
    return $fields;
});
function my_custom_field($field=array()){
    echo "<pre>".print_r($field,true)."</pre>"; // print field data
}

add_filter("sovit/settings/{$page_id}/fields",function($sections){
    $fields["age"]=[
        "label"=>__("Age","textdomain"),
        "render_callback"=>"my_custom_field",
        "validate_callback"=>"my_field_validation",
        "sanitize_callback"=>"my_field_sanitization",
        // other field options
    ];
    return $fields;
});
function validate_callback($value){
    return is_int($value) && $value>5; // return true if age is integer and greater than 5
}
function sanitize_callback($value){
    return sanitize_text_field($value);
}