I'm currently working with WPAlchemy class to create a metabox in a custom post type (Events from EventEspresso plugin more precisely).

This metabox should be able to call the media uploader and insert images for some custom metas.

It works pretty well, i'm able to call the media uploader and persist data from textfields. But, after opening the media uploader, selecting the image and clicking on 'Insert to Post'. The image link goes to WP Editor textarea instead of my textfield in the custom metabox.

Thanks guys. Hope its clear enough and sorry for the probably not very well written english.

  • Have you contacted the WPAlchemy developers? What did they say?
    – Tom J Nowell
    Feb 3, 2015 at 12:39
  • @TomJNowell, didnt , yet. I was hoping to fix it before that point. But maybe that's easier indeed. Feb 3, 2015 at 12:45

2 Answers 2


I managed to solve it. The problem was in using a custom post-type with Metabox.php / MediaAccess.php (both from WPAlchemy class)

Supposedly you can use custom post-types with WPAlchemy since, when instantiating a WPAlchemy_MetaBox object you can pass some optional parameters to enable that kind of costumization. Like this:

$mb = $my_custom_metabox = new WPAlchemy_MetaBox(array
    'id' => '_palestrantes_metabox',
    'title' => 'Palestrantes',
    'types' => array('post','espresso_events'),
    'template' => get_stylesheet_directory() . '/metaboxes/custom-meta.php'

But within MetaBox.php, the _init function uses some validation code to proceed:

559   // must be creating or editing a post or page
560   if ( ! WPAlchemy_MetaBox::_is_post() AND ! WPAlchemy_MetaBox::_is_page()) return;

Both functions _is_post() and _is_page(), end up calling the

static function _get_current_post_type()
    $uri = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : NULL ;
    if ( isset( $uri ) ) {
        $uri_parts = parse_url($uri);
        $file = basename($uri_parts['path']);

        if ($uri AND in_array($file, array('post.php', 'post-new.php'))) {
            $post_id = WPAlchemy_MetaBox::_get_post_id();

            $post_type = isset($_GET['post_type']) ? $_GET['post_type'] : NULL ;

            $post_type = $post_id ? get_post_type($post_id) : $post_type ;

            if (isset($post_type)) {
                return $post_type;
            else {
                // because of the 'post.php' and 'post-new.php' checks above, we can default to 'post'
                return 'post';
    return NULL;

The problem here is validating through the URI, it supposes that every post or page will have the 'post.php' or 'post-new.php' in its URI. Which in my case turned out being:


So to fix it (not proud btw). I had to add another validation in both Metabox.php / MediaAccess.php to also allow the execution when 'page=espresso_events' and 'action=edit'

Well that's it. Hope it helps. Thanks.


The following fix worked perfectly for me:

Open MediaAccess.php and replace the line 368:

Old line:

$('[class*="<?php echo $this->button_class_name; ?>"]').on('click', function()

New line of code:

$('body').on('click', '[class*=<?php echo $this->button_class_name; ?>]', function()

Solution found in this link: https://github.com/maniaks/wpalchemy/commit/9e55cd9c3071b57b83ff1e8bc86564478d2eb873

  • Hacking a theme or plugin that you don't maintain is bad practice.
    – s_ha_dum
    Jan 5, 2016 at 17:42

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