How to add meta boxes to Pages

I’m almost complete whit my wp theme an I’m now translating it with the WPML plugin. This is working out great (even all my custom post types and meta boxes 🙂 ), but of course there have to be a problem.
I have added meta boxes to one specific page and have target it like this:

add_action('admin_init','my_meta_init');

function my_meta_init()
{
    $post_id = $_GET['post'] ? $_GET['post'] : $_POST['post_ID'] ;


    // checks for post/page ID
    if ($post_id == '17')
    {
        add_meta_box('meta_webdesign', 'Webdesign', 'my_meta_webdesign', 'page', 'normal', 'high');
        add_meta_box('meta_grafisk', 'Grafisk design & Identitets design', 'my_meta_grafisk', 'page', 'normal', 'high');
        add_meta_box('meta_illustrasjon', 'Illustrasjon', 'my_meta_illustrasjon', 'page', 'normal', 'high');
    }

But now I need to also target the English version of the page (id 348), how do I do that?

Read More

Ps. I have tried this: if ($template_file == ‘page-tjenester.php’) (the Norwegian and English version both use the same template, but then all the meta boxes disappear.

I would be very grateful for help with this 🙂

Related posts

Leave a Reply

3 comments

  1. I’ve got this working finally ($template_file == ‘page-tjenester.php’), so now my meta boxes shows up only when I use this specific page-template!

    This is the final code if someone is interesting;

    add_action('admin_init','my_meta_init');
    function my_meta_init()
    {
    
    $post_id = $_GET['post'] ? $_GET['post'] : $_POST['post_ID'] ;
    $template_file = get_post_meta($post_id,'_wp_page_template',TRUE);
    
    if ($template_file == 'page-tjenester.php') { // this will only show on pages that have been saved with the page-tjenester.php template
    
            add_meta_box('meta_webdesign', 'Webdesign', 'my_meta_webdesign', 'page', 'normal', 'high');
            add_meta_box('meta_grafisk', 'Grafisk design & Identitets design', 'my_meta_grafisk', 'page', 'normal', 'high');
            add_meta_box('meta_illustrasjon', 'Illustrasjon', 'my_meta_illustrasjon', 'page', 'normal', 'high');
        }
    }
    

    Not hard at all (not sure what I did wrong when I tried this before)

  2. Use the icl_object_id function of WPML. It will resolve automatically the id depending of the current language.

    Another option and IMO much more reliable one would be to do the check based on the slug of the page. It would cover you in the case you accidentally deletes the post/page concerned. With an ID check you would be forced to go and edit the code source again, with a slug check you’d only have to recreate a page with the same slug. Just my 2 cents though.

  3. You could try Limon

    in that case would the code look like this for page-posttype. in that case you dont have to think of the hooks and what hooks to use 🙂

        // Create Metabox
    $custom_meta_box = new MetaBox();
    $custom_meta_box->  create('meta_webdesign', 'Webdesign', 'my_meta_webdesign', 'page');
    // Add some fields
    $custom_meta_box->  addTextField('Design', 'tf_webdesign');
    
    function my_meta_webdesign( $meta_fields )
    {
        echo $meta_fields['tf_webdesign'];
    }
    

    https://bitbucket.org/DanelK/limon-project/src/a2d5e081249a/_README_EXAMPLES_

    there are of course some more methods that you could find useful.