Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am trying to create a metabox input field in a custom post type and it does not work even if I followed the instructions the best I could. I am not a PHP dev so I guess it could be just a small thing that is missing or it is wrong.

By not working I mean, I does not show at all in the WP UI.

Here is the code:

<?php

function add_post_type($name, $args = array()) {
    add_action('init', function() use($name, $args) {

            $upper = ucwords($name);
            $name = strtolower(str_replace(' ','_',$name));

            $args = array_merge(

            array(
            'public'=> true,
            'label' => "All $upper" . 's',
            'labels' => array('add_new_item' => "Add New $upper"),
            'support' => array('title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments', 'custom-fields'),
            'taxonomies' => array('post_tag','category')
            ),

            $args

            );

            register_post_type('$name', $args);
        });

}


//now we create and register a taxonomy
function add_taxonomy($name, $post_type, $args = array()) {
    $name = strtolower($name);

    add_action('init', function() use($name, $post_type, $args) {
            $args = array_merge(
                array(
                'label' => ucwords($name),

                ),
                $args
            );
                register_taxonomy($name, $post_type, $args);
    }); 
}

/************************************************************
  Now we add the names of the custom post type and taxonomies
*************************************************************/


add_post_type('snippet', array(
            'supports' => array('title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments', 'custom-fields'),
            'taxonomies' => array('post_tag')
));


add_taxonomy('language', 'snippet');

/************************************************************
  Creating Metaboxes
*************************************************************/

add_action('add_meta_boxes', function() {
    add_meta_box(
        'er_snippet_info',
        'Snippet Info',
        'er_snippet_info_cb',
        'snippet',
        'normal',
        'high'
    );
});

function er_snippet_info_cb() {
    global $post;
    $url = get_post_custom($post->ID); 

    ?>

    <label for="er_associated_url">Associated URL: </label>
    <input type="text" name="er_associated_url" id="er_associated_url" value="<?php echo $url; ?>" />

    <?php
}
    add_action('save_post', function () {
        global $post; 
        if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) return;
        //security check 
        if ($_POST && !wp_verify_nonce($_POST['er_nonce'], _FILE_)) {
                if ( isset ($_POST['er_associated_url']) ) {
            update_post_meta($post->ID, 'er_associated_url', $_POST['er_associated_url']);  
            }
       }

    });
?>

The other issue is that the Tag is not named even if the function is there.

share|improve this question
2  
A question should not rely on a link to an external website. Put the code into your question please. –  toscho Oct 30 '12 at 2:33
    
@toscho sorry about that, everytime i was adding the code was the editor was removing some part of it. –  Eri Mehmeti Oct 31 '12 at 4:37
add comment

2 Answers 2

In the code, you are trying to pass functions as parameters in add_action(). That syntax works in javascript, but not so well in php. Try doing it with passing the function name instead in add_action()

share|improve this answer
    
Lambda functions are working fine in PHP 5.3+. –  toscho Oct 31 '12 at 3:32
add comment

This may help. It worked for me

function my_plugin_add_meta_box()
{
    $type_name = array('custom_post_type_name');
    foreach ( $type_name as $custom_post_type ) {
            add_meta_box('my_plugin_meta_box_id', __('Meta Box Name'), 'my_plugin_meta_box_callback', $custom_post_type);
    }
}
add_action( 'add_meta_boxes', 'my_plugin_add_meta_box' );

and then you can add input boxes in the my_plugin_meta_box_callback function. Hope this helps some one.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.