Git Product home page Git Product logo

Comments (1)

cibulka avatar cibulka commented on August 12, 2024

OK, let me answer my own question. :) This is my current approach:

  1. inc/sample.php (or your custom file): Add custom paramater req in fields array to fields that you wish to be required and set it to true. Such as:
array( // Text Input
    'label' => 'Text Input', // 
    'desc' => 'A description for the field.', // description
    'id' => $prefix.'text', // field id and name
    'type' => 'text' // type of field
    'req' => true // CUSTOM REQ PARAMETER
),
  1. meta_box.php: Get the data and set new variable (basically repeat pattern from lines 20 to 28).
$req = isset($field['req']) ? $field['req'] : null;
  1. meta_box.php: Run another case/switch statement and do whatever you want to do with the results.
switch($req) {
    case 'true':
        echo' This is required.';
    break;
}
  1. meta_box.php /optional/: I like to add "req" and "unreq" class to table rows to be able to do some fancy jQuery validation magic.
function meta_box_callback() {
        // Nonce
    // Begin the field table and loop
    echo '<table class="form-table meta_box">';
    foreach ( $this->fields as $field) {
        if (isset($field['req'])) { $req = 'req'; } else { $req = 'unreq'; } // required check (cibulka)
            
        if ( $field['type'] == 'section' ) {
            // req class (cibulka)
            echo '<tr class="'.$req.'">[...]</tr >';
        } else {
            // req class (cibulka)
            echo '<tr class="'.$req.'">[...]</tr>';
        }
    } // end foreach
    echo '</table>'; // end table
}

Does it make sense?

SIDENOTE (for PHP beginners like me): I like to add comments to code, that clearly state what are my edits of default library. You can cmd+F you edits easily, which is essential for debugging (you never know what you can mess up) and updating.

from reusable-custom-wordpress-meta-boxes.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.