I am having some problems including files for my custom meta boxes. I have a structure like so:


  • cmb

    • teaser_meta_box
      • load.php
      • initiate.php
      • display.php
      • save.php

So within my post-meta.php I have the following includes:

// Load Meta Boxes
function add_custom_meta_box()
    require_once dirname( __FILE__ ) . '/cmb/teaser_meta_box/load.php';
add_action('add_meta_boxes', 'add_custom_meta_box');

// Initiate Meta Box Fields
require_once dirname( __FILE__ ) . '/cmb/epteaser_meta_box/initiate.php';

// Display Meta Box Fields
require_once dirname( __FILE__ ) . '/cmb/epteaser_meta_box/display.php';

// Save Meta Box Fields
function save_custom_meta($post_id)
global $meta_fields_epteaser, $post; // Need to put name of each meta field here

// verify nonce  
if (!wp_verify_nonce($_POST['custom_meta_box_nonce'], basename(__FILE__)))
    return $post_id;
// check autosave  
    return $post_id;
// check permissions  
if ('page' == $_POST['post_type']) {
    if (!current_user_can('edit_page', $post_id))
    return $post_id;
} elseif (!current_user_can('edit_post', $post_id)) {
    return $post_id;

    require_once dirname( __FILE__ ) . '/cmb/epteaser_meta_box/save.php';

add_action('save_post', 'save_custom_meta');

When I try this the meta boxes do not work how they should, however I do not get any errors saying includes are wrong etc. If I try to put the code directly into the file for initiating and displaying the meta box it then works.

Any ideas as to why these two includes dont seem to work when included?

Thanks Robert

1 Answer 1


Debugging in WordPress

The following could/should be set in the wp-config.php file of your local(!!) installation - never do this on a live site, especially not when you got caching activated!

define( 'WP_CACHE',            false  );

// Show the development files for scripts/stylesheets and don't combine them
define( 'COMPRESS_CSS',        false  );
define( 'SCRIPT_DEBUG',        true );
define( 'COMPRESS_SCRIPTS',    false  );
define( 'CONCATENATE_SCRIPTS', false  );
define( 'ENFORCE_GZIP',        false );

// PHP and WP internal debug output + log
error_reporting( E_ALL );
@ini_set( 'display_errors', 1 );
define( 'SAVEQUERIES',      true );
define( 'WP_DEBUG',         true );
define( 'WP_DEBUG_LOG',     true ); // file: ~/wp-content/debug.log
@ini_set( 'log_errors',     'On' );
@ini_set( 'error_log',      WP_CONTENT_DIR.'/php_error.log' );
define( 'WP_DEBUG_DISPLAY', true );

Then always wrap your calls inside the following (assuming you're ignoring the warnings and using it on your live installation):

  1. Turn of all caching solutions - else some visitor could still see your debug output.
  2. Wrap your debug inside if ( is_user_logged_in() ) { /* debug here */; }
  3. Even better: Only for admins: if ( current_user_can( 'manage_options' ) ) { /* debug here */; }

WordPress File Path & URI/URL functions

Path & URL

// The path to your current file
plugin_dir_path( __FILE__ );

// The URL to your current file
plugin_dir_url( __FILE__ );

Those ↑ come already trailing-slashed.

If you need only the foldername, wrap it inside basename().

If you need the admin URL, use get_admin_url( get_current_blog_id() ). Even easier is using self_admin_url(), which determines by itself if you're in a network, single blog or on a user network-admin page.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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