0

I'm using WPAlchemy on a project.

For a template, I need to use a custom metabox which is working fine with a tinyMCE editor.

Code to insert the meta box on the template (this is a piece of code from https://github.com/helgatheviking/WP-Alchemy-Holy-Grail-Theme/ because I end up trying it after a looooog search, but macache).

<div class="my_meta_control metabox">
<p>
<?php $mb->the_field('richfield');

$settings = array(
'textarea_rows' => '10',
'media_buttons' => 'false',
'tabindex' =>2
);

$val = html_entity_decode($mb->get_the_value());   
$id = $mb->get_the_name();

wp_editor($val, $id , $settings );

?> 
</p>
<p class="meta-save"><button type="submit" class="button-primary" name="save"><?php _e('Update');?></button></p>

</div>

I have added the following in functions.php

/*
* Recreate the default filters on the_content
* this will make it much easier to output the meta content with proper/expected       formatting
*/
add_filter( 'meta_content', 'wptexturize' );
add_filter( 'meta_content', 'convert_smilies' );
add_filter( 'meta_content', 'convert_chars' );

//use my override wpautop
if(function_exists('override_wpautop')){
add_filter( 'meta_content', 'override_wpautop' );
} else {
add_filter( 'meta_content', 'wpautop' );
}
add_filter( 'meta_content', 'shortcode_unautop' );
add_filter( 'meta_content', 'prepend_attachment' );

and for the output :

<div class="content-column content-tab content-richtext">
    <?php
    global $page_type_richtext;
    $page_type_richtext->the_meta(); // don't think I need this line
    $richtextcontent = $page_type_richtext->the_value( 'richfield' );
    echo apply_filters('meta_content', $richtextcontent );
    ?>
</div>

But I never manage to have the <p> tags working. I don't understand where i'm wrong.

I have tried a lot of different ways, using the_content filter, or wpautopbut it's not working at all.

The display is perfect in the custom field itself inside the admin page. That's why I'm getting mad. The content is correctly wpautop() when in admin, so I guess the content is properly saved in DB. There must be a way to make it work, but I really don't know. I have find a couple of stack exchange questions about that, like this one but I don't understand how to adapt it. Looks like I should use wpautop at the saving of the post, but this is handled by WPAlchemy, and wp_editor() doesn't need it, in theory as I understood it.

Does anyone give me a hind or a lead ?

2 Answers 2

0

sorry you were having trouble with my "Holy Grail". It looks like you missed an important part in my functions.php sample:

apply_filters('meta_content',$simple_textarea->get_the_value('test_editor'));

You weren't using WP Alchemy's get_the_value method. So instead of:

$richtextcontent = $page_type_richtext->the_value( 'richfield' );

yours should have been:

$richtextcontent = $page_type_richtext->get_the_value( 'richfield' );

You were using the_value which echos out the value and therefore doesn't give you a chance to run it through the filters.

The meta_content filters are not useless, but can be redundant. You can use the_content filters, but a lot of plugins hook into the_content... (such as most social sharing plugins) and if you use apply_filters('the_content',$sometext) in a lot of places you can end up with a page full of social buttons! I did once, which is why I decided it was cleaner to duplicate the_content.

I'm sure you aren't still working on this project, but hopefully this will help anyone else who ends up on this question.

Cheers!

1
  • Well, I make it works using get_post_metabut your answer tell me why it wasn't working in the first place. So I give your the answer !
    – Simon
    Commented Nov 28, 2012 at 19:31
0

Ok I sort it out.

To let you know :

  • The code to invocate the custom meta box is correct

  • The filter part in function.php is perfectly useless -> all we have to do is use apply_filters with the $tag the_content which already use all the needed filters to properly echoing rich text content.

  • So the problem was in using the built-in custom way WPAlchemy retrieves values. I just used core functions of Wordpress to get values (WPAlchemy is very well documented, so that was easy) and tadaaam! it worked. I guess the method $page_type_richtext->the_value( 'richfield' ) just destroy breaklines, so there was no way the filter could insert <p> tag without these informations.

The correct way to output the data is, in my case :

<div class="content-column content-tab content-richtext">
    <?php
    global $page_type_richtext; // accessing meta data in global space 
    $meta = get_post_meta($post->ID, $page_type_richtext->get_the_id(), TRUE); // use get_post_meta to retrieve content 
    echo apply_filters('the_content', $meta['richfield'] ); // echo data processed with the_content filter
    ?>
</div>

Thanks.

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.