0

I've created a plugin, that registers a custom_post_type. This custom_post_type also has custom_fields. The data I've put in the custom_fields should be showed in the post (in the frotend, for the users). I want my plugin to be completly independent of the template. To do this, I need to access the posts in the plugin itself and put the information from the custom_fields on the bottom of the posts. I dont want to modify any of the "normal" posts, just those of the custom_post_type.

Sadly I have no idea how to do this. I've managed to do that with an if(custom_field != '') but had to access the template do so. But thats not a "clean" and "nice" solution I think.

That's why I have some questions:

  1. Is it "good" or "allowed" to alter posts within a plugin, without accessing the template?
  2. If not, how should I alter the posts then?
  3. If so, can you give me any examples how to so? Maybe a link to a tutorial or just some code snippets?

Thank you very much!

1

Use the_content filter to add your fields to the output of the_content() function:

function wpd_content_filter( $content ) {
    if ( 'your_custom_type' == get_post_type() ){
        if( $meta = get_post_meta( get_the_ID(), 'foo_meta', true ) ) {
           $content = $content . $meta;
        }
    }
    return $content;
}
add_filter( 'the_content', 'wpd_content_filter' );
  • Thanks for your help. So i just tried it out, but it doesnt worked. Need I add something to it, except of changing "your_custom_type" and "foo_meta"? – TheOnionMaster May 31 '17 at 18:27
  • Nvm: That is the perfect solution. Thank you very very very much. You really helped me out with that :))))) – TheOnionMaster May 31 '17 at 18:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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