• Hi,

    I found a website that provides sample code to add_meta_box functions. After copying and pasting and making some changes so I can get it to work with the “Post” type, instead of a Custom Post Type, it worked like a charm. Started to add some more input fields to save additional data, and now it doesn’t save any data. For that reason I commented out any textboxes that I created, it still won’t save or retrieve the data.

    Here’s the functions.php. Please let me know if you find something wrong.

    /******************************************************************************/
    add_action( ‘add_meta_boxes’, ‘ahng_add_meta_box’ );
    add_action( ‘save_post’, ‘ahng_save_postdata’ );

    function ahng_add_meta_box() {
    add_meta_box(
    ‘ahng_thumb_meta_box’,
    ‘Video Options’,
    ‘ahng_video_options_meta_box_func’,
    ‘post’
    );
    }

    function ahng_video_options_meta_box_func( $post ) {

    // Use nonce for verification
    wp_nonce_field( plugin_basename( __FILE__ ), ‘ahng_noncename’ );

    $mydata = get_post_meta($post->ID, ‘post’, TRUE);

    // The actual fields for data entry
    echo ‘<label for=”ahng_url_thumbnail”>’;
    echo ‘URL to thumbnail:’;
    echo ‘</label>’;
    echo ‘<input type=”text” id=”ahng_url_thumbnail” name=”ahng_url_thumbnail” value=”‘.$mydata[‘ahng_url_thumbnail’].'” size=”50″ />’;
    echo ‘<label for=”ahng_duration_time”>’;
    echo ‘Duration Time: Ex,(1:22)(11:34)(0:25)’;
    echo ‘</label>’;
    echo ‘<input type=”text” id=”ahng_duration_time” name=”ahng_duration_time” value=”‘.$mydata[‘ahng_duration_time’].'” size=”15″ />’;
    //echo ‘

    ‘;
    //echo ‘<label for=”ahng_itunes_aff”>’;
    //echo ‘Itunes Affiliate Link (URL)’;
    //echo ‘</label>’;
    //echo ‘<input type=”text” id=”ahng_itunes_aff” name=”ahng_itunes_aff” value=”‘.$mydata[‘ahng_itunes_aff’].'” size =”75″ />’;
    //echo ‘

    ‘;
    //echo ‘<label for=”ahng_amazon_aff”>’;
    //echo ‘Amazon Affiliate Link (URL)’;
    //echo ‘</label>’;
    //echo ‘<input type=”text” id=”ahng_amazon_aff” name=”ahng_amazon_aff” value=”‘.$mydata[‘ahng_amazon_aff’].'” size =”73″ />’;

    //Add more fields as you need them…
    }

    function ahng_save_postdata( $post_id ) {

    if ( defined( ‘DOING_AUTOSAVE’ ) && DOING_AUTOSAVE )
    echo ‘doing autosave’;
    return;

    if ( !wp_verify_nonce( $_POST[‘ahng_noncename’], plugin_basename( __FILE__ ) ) )
    echo ‘can not verify nonce’;
    return;

    if ( !current_user_can( ‘edit_post’, $post_id ) )
    echo ‘user can not make changes’;
    return;

    $mydata = array();
    foreach($_POST as $key => $data) {
    if($key == ‘ahng_noncename’)
    continue;
    if(preg_match(‘/^nivo/i’, $key)) {
    $mydata[$key] = $data;
    }
    }
    update_post_meta($post_id, ‘post’, $mydata);
    return $mydata;
    }

    ?>

Viewing 6 replies - 1 through 6 (of 6 total)
  • Thread Starter karlorihoo

    (@karlorihoo)

    Actually,
    I just commented out

    if(preg_match(‘/^nivo/i’, $key))

    and everything seems to be working.

    I know this is php related, but does anyone know
    what that function returns or performs?

    Thanks.

    preg_match() is a PHP function that searches strings for patterns called regular expressions. In this case, it is checking the $key variable to see if the string begins with “nivo”.

    The breakdown is as follows:

    / / – is used to define the beginning and end of the regular expression (think of this as brackets)

    ^ – specifies that we want to look at the beginning of the string stored in $key.

    nivo – is simply the pattern of characters we’re looking for

    i – the “i” at the end simply says that we’re willing to accept “nivo” in any case, upper or lower or any combination of such. Thus, “nivo123” or “NIVOqwerty” or “NiVo” etc. will match.

    $key – is the variable we want to search for patterns in.

    I’m not sure what the string “nivo” refers to, but it seems to be a prefix of some sort.

    Regular expressions are extremely powerful. For more information, you can read about the preg_match function here:

    php.net/manual/en/function.preg-match.php

    I tend to skim-read quickly. Upon closer inspection, it appears as if someone renamed all the “name” attributes of your input boxes. For example, the thumbnail input box says:

    name="ahng_url_thumbnail"

    but this likely used to say:

    name="nivo_url_thumbnail"

    Thus, if you simply change

    if(preg_match('/^nivo/i', $key)) {

    to say

    if(preg_match('/^ahng/i', $key)) {

    your code will work as expected.

    This is always the problem with using someone else’s code.

    Thread Starter karlorihoo

    (@karlorihoo)

    Hi,

    Thanks for the detailed explanation in how preg_match works. I looked it up online and still couldn’t get the jist of it. I will make the changes to the code and see if it works.

    Thanks.

    It takes a while for people to get the hang of regular expressions. They’re a very powerful tool for searching and replacing string data, but it can take some mental gymnastics to get the correct syntax. For example, do a Google search for a regular expression to validate email addresses and you’ll find a countless number of suggestions and heated debates among seasoned programmers over the one “true” email address pattern.

    That said, you can use a simple regular expression pattern to determine if someone entered a number into a field that only accepts letters, or to determine if a string contains a valid telephone number or postal code.

    If you’re planning on working with code (whether PHP or Javascript), it would be a good idea to learn how they work. There are probably some great tutorials for beginners available online, and the pattern-matching syntax is usually the same across the various programming languages.

    Thread Starter karlorihoo

    (@karlorihoo)

    OK,

    Will do.

    Thanks.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Add Meta Boxes not saving or retrieving’ is closed to new replies.