4

On the wordpress admin edit posts page, I want to display some custom content and script, underneath the meta box.

I want to hand code this as the various plugins I could use (advanced custom fields for instance) take too much control away, and I don't need an actual field to be saved.

So for example, I want to add this to the edit posts page:

<div class='mydiv'>
  <img src='someImage.png' alt='someImage'/>
  <script type='text/javascript'>alert('hello world!');</script>
</div>

That's pretty much it. Nothing fancy or that contains any editable content.

What hook or function do I need to use to achieve this?

Any help greatly appreciated

8

As found here:

http://codex.wordpress.org/Function_Reference/add_meta_box

/**
 * Calls the class on the post edit screen
 */
function call_someClass() 
{
    return new someClass();
}
if ( is_admin() )
    add_action( 'load-post.php', 'call_someClass' );

/** 
 * The Class
 */
class someClass
{
    const LANG = 'some_textdomain';

    public function __construct()
    {
        add_action( 'add_meta_boxes', array( &$this, 'add_some_meta_box' ) );
    }

    /**
     * Adds the meta box container
     */
    public function add_some_meta_box()
    {
        add_meta_box( 
             'some_meta_box_name'
            ,__( 'Some Meta Box Headline', self::LANG )
            ,array( &$this, 'render_meta_box_content' )
            ,'post' 
            ,'advanced'
            ,'high'
        );
    }


    /**
     * Render Meta Box content
     */
    public function render_meta_box_content() 
    {
        ?>
        <div class='mydiv'>
          <img src='someImage.png' alt='someImage'/>
          <script type='text/javascript'>alert('hello world!');</script>
        </div>
        <?php

    }
}
  • 3
    Love it when people use my Codex samples - proves that not all is for nothing :) - even when my sample code could be better. +1 – kaiser Mar 30 '12 at 15:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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