Jay Littles
Jay Littles

Reputation: 5

Wordpress Custom Meta Box Not Showing

Trying to add meta boxes to my admin and found a code here to make them. Made some changes to the code, applied it to my site and the meta boxes are not showing at all, on any post or page types. Code below:

add_action('admin_init');

function admin_init() {
    add_meta_box("credits_meta", "Mixtape Info", "credits_meta", "mixtape", "normal", "low");
}

function credits_meta() {
    global $post;
    $custom = get_post_custom($post->ID);
    $dj = $custom["DJ"][0];
    $embed = $custom["embed code"][0];
    $tracklisting = $custom["tracklisting"][0];
    ?>;
    <label>DJ:</label>
    <input name="DJ" value="<?php echo $dj; ?>"/>
    <p><label>Embed:</label><br />
    <textarea cols="50" rows="5" name="embed code"><?php echo $embed; ?></textarea></p>
    <p&><label>Tracklisting:</label><br />
    <textarea cols="50" rows="5" name="tracklisting"><?php echo $tracklisting; ?></textarea></p>
    <?php 

}

Is it something obvious I'm missing? I copied and pasted the example in the link and got the same results.

Upvotes: 0

Views: 1835

Answers (3)

Steve
Steve

Reputation: 11

I got stuck on this for two days - I fixed the problem by adding ...

wp_nonce_field( basename( FILE ), 'your_plugin_name_nonce' );

... to my form in my metabox.

Upvotes: 0

Tanmay Patel
Tanmay Patel

Reputation: 1810

<?php
function credits_meta() {
    global $post;
    $custom = get_post_custom($post->ID);
    $dj = "a";
    $embed = "b";
    $tracklisting = "c";
    ?>
    <label>DJ:</label>
    <input name="DJ" value="<?php echo $dj; ?>"/>
    <p><label>Embed:</label><br />
    <textarea cols="50" rows="5" name="embed code"><?php echo $embed; ?></textarea></p>
    <p&><label>Tracklisting:</label><br />
    <textarea cols="50" rows="5" name="tracklisting"><?php echo $tracklisting; ?></textarea></p>
    <?php 
}
?>

Upvotes: 1

Ohgodwhy
Ohgodwhy

Reputation: 50777

add_action('admin_init'); This is wrong.

add_action expects the first argument to be the function hook, and the 2nd argument to be the function to execute when the hook is called, such as:

add_action('admin_init', 'admin_init');

But even that's wrong, because you're going to get an error about trying to redeclare a previously declared function, so instead, it should be something like

add_action('admin_init', 'my_admin_init');

Where my is the namespace of your application.

Or use classes. This might exist in a file called my_class.php

class my_class {
    public function my_admin_init(){
        //do work
    }
}

Require this file in your functions.php

require_once('path/to/my_class.php');

Instantiate the class

$my_class = new my_class;

Now call the function on admin_init

add_action('admin_init', array($my_class, 'my_admin_init'));

Upvotes: 0

Related Questions