Can’t get metabox value (a url) returned using WPAlchemy

I’m using the WP Alchemy class to add custom metaboxes to templates in the admin panel and pull results from those metaboxes into the theme front-end. Here is the relevant code in content-link.php (in the loop – in Twenty Eleven Theme).

<a href="<?php display_url_info(); ?>"><img src="http://localhost/wordpress/wp-content/uploads/2011/08/external-link-red03.png"/></a>   

Right now the link shows just the permalink instead of the url in the metabox. If I hardcode a url in there instead, the custom url shows up on the post. I’ve verified it being properly stored under _custom_meta in the db (and successfully implemented metabox display in other templates. Here’s the value in the db:

Read More
a:1:{s:5:"links";a:1:{i:0;a:2:{s:6:"domain";s:25:"http://stackexchange.com/";s:13:"post_link_url";s:48:"http://wordpress.stackexchange.com/questions/ask";}}}

Here’s my plugin file:

$basic_metabox = new WPAlchemy_MetaBox (

    array (
    'id' => '_custom_meta',
    'title' => 'Basic Meta',
    'template' => ABSPATH . 'wp-content/themes/beernews-child-theme/custom/basic-meta.php',
    'types' => array('post','link')
    )

);

   add_filter('beernews-child-theme_post', 'display_url_info');

function display_url_info() {

    // usually needed
    global $basic_metabox;

    // get the meta data for the current post
    $basic_metabox->the_meta(); ?>

    <?php if ($basic_metabox->get_the_value('post_link_url')) {

    echo $basic_metabox->the_value('post_link_url');

}

}

Related posts

Leave a Reply

2 comments

  1. Try changing your display_url_info function to

    <?php
    add_filter('beernews-child-theme_post', 'display_url_info');
    
    function display_url_info() {
        // usually needed
        global $basic_metabox;
        $basic = $basic_metabox->the_meta(); 
    
        if($basic['post_link_url']) {
            echo $basic['post_link_url'];
        } else {
            echo 'ERROR : "post_link_url" empty';
        }
    }
    ?>
    

    And not to be a jerk, but I would like to point out that WP Alchemy is not a plugin but rather a PHP Class. I just do not want other people to get confused.

  2. Not sure why you are using the filter exactly …

    I would do the following in the theme file:

    <?php global $basic_metabox; /* this is just needed once */ ?>
    <?php if ($basic_metabox->have_value('post_link_url')) { ?>
    <a href="<?php $basic_metabox->the_value('post_link_url'); ?>"><img src="http://localhost/wordpress/wp-content/uploads/2011/08/external-link-red03.png"/></a> 
    <?php } ?>
    

    Also note that get_the_value() and the_value() are different, the first returning a value and the second echoing out a value.