Custom HTML + PHP callback to display value on Settings Page (MB AIO)

Support MB Settings Page Custom HTML + PHP callback to display value on Settings Page (MB AIO)Resolved

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #27712
    JordanJordan
    Participant

    Hi! I'm building a settings page where user enters data that is processed somewhere else in a code block. On that page (settings), I'm trying to display field group data using Custom HTML with a PHP callback.
    I've entered the following function in "Code Snippets". What I want is the $index variable to display on the page so the user knows his settings' result in backend. So I'm entering 'mb_quote_index_return' in the "PHP callback" line of the "Custom HTML" field, but nothing shows.
    Can someone tell me where I got it wrong?
    Thanks!

    CODE (mb_quote_index_return):

    <?php
    function get_mb_settings_field( $field_id ) {
        return rwmb_meta( $field_id, ['object_type' => 'setting'], 'options' );
    }
    date_default_timezone_set("America/New_York");
    $quotes = get_mb_settings_field( 'pensees_quotes' );
    $max = count($quotes);
    
    $lastIndex = get_mb_settings_field( 'start_index' );
    $lastTime = get_mb_settings_field( 'start_date' );
    $lastTimeA = strtotime($lastTime);
    $gap_s = strtotime('now') - $lastTimeA;
    $gap_d = floor($gap_s / (60<em>60</em>24));
    $index = ($lastIndex - 1) + $gap_d;
    $rem = $index;
    
    while ($rem >= $max) { 
      $rem = $rem - $max;
    }
    
    $index = $rem;
    
    echo "Pensée: ".($index + 1)."/".$max;
    ?>
    #27713
    JordanJordan
    Participant

    I realized my above question isn't well formulated: the code above works well in an Oxygen code block, so this isn't the issue. Now what I want to do is access the same table's data from a Custom Fields Group and display it on a settings page using custom html. For example, here's how I tried to output the value of ' start_index ' through
    1) php callback: http://prnt.sc/125qd5s
    2) html/php: http://prnt.sc/125qfo0

    Yet the settings page outputs nothing: http://prnt.sc/125qhps

    What I really want to display is the $index variable in the above code. Any help understanding how the custom html works would be greatly appreciated (I did read the documentation but could not figure it out.)

    Thanks!

    #27739
    Long NguyenLong Nguyen
    Moderator

    Hi Jordan,

    Thank you for reaching out.

    The field type custom_html only accepts the callback function name. It also does not support passing the argument on this setting.

    If you use content setting, it only accepts HTML code, not support PHP code. See my screenshot https://share.getcloudapp.com/z8u6G19r.

    Get more details on the documentation https://docs.metabox.io/fields/custom-html/.

    #27741
    JordanJordan
    Participant

    Thanks! So if I understand, I must wrap everything in a custom function that will echo the settings_field value. Where should I store that custom function so it is accessible via callback? (I use Oxygen which disables function.php).

    #27746
    Long NguyenLong Nguyen
    Moderator

    Hi,

    You can add the custom function in a custom plugin or use the plugin Code Snippets https://wordpress.org/plugins/code-snippets/

Viewing 5 posts - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.