Forums

[resolved] Post ID in Add Meta Box plugin (11 posts)

  1. toneburst
    Member
    Posted 1 year ago #

    Really stupid newbie question:

    I'm writing a very simple plugin to add a meta box to the Edit Post panel, based on the example code at
    http://codex.wordpress.org/Function_Reference/add_meta_box

    I need to get the post ID inside the function that draws the contents of the meta box, ( named 'myplugin_inner_custom_box()' in the example) so I can have checkboxes checked or not-checked based on saved custom meta values for the post.

    Anyone any tips?

    Cheers,

    a|x

  2. toneburst
    Member
    Posted 1 year ago #

    OK, got it, thanks:

    global $post;
    $post_id = $post->ID;

    a|x

  3. Mark / t31os
    Moderator
    Posted 1 year ago #

    Note this part on the codex page..

    $callback_args
    (array) (optional) Arguments to pass into your callback function. The callback will receive the $post object and whatever parameters are passed through this variable.

    Default: null

    So bearing that in mind, update this..

    function myplugin_inner_custom_box() {

    to..

    function myplugin_inner_custom_box( $post ) {

    ..and $post will contain what you'd assume it does.. ;)

  4. toneburst
    Member
    Posted 1 year ago #

    Great, thanks Mark!

    a|x

  5. toneburst
    Member
    Posted 1 year ago #

    I must say I'm still a bit confused about what variables are available at any given point.

    a|x

  6. Mark / t31os
    Moderator
    Posted 1 year ago #

    It depends what your callback function is hooked onto, every hook in WordPress passes along differing data to the callback functions, some pass on 1 variable(string/array/object), others as many as 4 or 5, it's down to the given hook(there's no universal answer).

  7. toneburst
    Member
    Posted 1 year ago #

    Thanks Mark. Sorry if this is a stupid questions, but is there a single place where all the hooks and filters and their variables are documented? Or is it just a case of digging around in the sourcecode?

    a|x

  8. esmi
    Theme Diva & Forum Moderator
    Posted 1 year ago #

    Since I'm not sure if Mark is around - quick answer... Some are documented but I personally find it much easier to dive into the source code which usually includes some very basic documentation.

  9. toneburst
    Member
    Posted 1 year ago #

    Thanks. TextWrangler's Multi-File find is my friend :D

    a|x

  10. Mark / t31os
    Moderator
    Posted 1 year ago #

    There's a list of action and filter hooks on the following pages.

    http://codex.wordpress.org/Plugin_API/Action_Reference
    http://codex.wordpress.org/Plugin_API/Filter_Reference

    And a general overview of hooks, actions/filter, and what not, can be found here.
    http://codex.wordpress.org/Plugin_API

    I also answered a filter/action related question recently which may help broaden your understanding(bearing in mind i'm not the greatest teacher, it may help all the same).
    http://wordpress.org/support/topic/add_action-function-values

    I'm always around, just briefly sometimes.. ;)

  11. toneburst
    Member
    Posted 1 year ago #

    Thanks for all your help guys. I've actually produced a couple of WP plugins in the past, but (like many I suppose), I tend to jump in at the deepend with these coding projects, and I'm still a bit shaky on some of the basics. I'm sure I've been doing some really stupid things, so your advice and encouragement is much appreciated.

    Cheers,

    a|x

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags