Monday, 27 May 2013

Displaying custom filed content instead of post content

Displaying custom filed content instead of post content

Plugin allow to add a custom field in post/page.
In post/page, i create a custom field, so user can input content on this field.
And i want if user input to this field, the post/page will display this content on screen, instead the main content that input via post/page editor.
Anybody can let me know how to do this ?
It is a plugin, so i can not edit template.
I have tried with add_filter -> the_content , but it seems only apply for post type, how to make it works with both post/page type ?
add_meta_box(
            'st-page-builder',          // Unique ID
            esc_html__( 'Page Builder', 'page-builder' ),       // Title
            'pageBuilderCallBack',      // Callback function
            'post',                 // Admin page (or post type)
            'advanced',                 // Context
            'default'                   // Priority
        );

function pageBuilderCallBack($object, $box) {
    wp_nonce_field( basename( __FILE__ ), 'page-builder-nonce' );
    $page = get_post_meta($object->ID, 'page_builder', true);
    require_once 'form-input.php';
}

function displayCustomContent() {
    // how to replace main post content with custom content ?
    if (is_page() || is_single())
    {
          global $post;

          $pageBuilderContent = trim(get_post_meta($post->ID, 'page_builder', TRUE));

          if ($pageBuilderContent) {
               return $pageBuilderContent;
          }
     }

     return $content;
}

add_filter('the_content', 'displayCustomContent');

No comments:

Post a Comment