0

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');

1 Answer 1

2

You just need to check for that custom field and return that instead of post content.

function displayCustomContent() {    
    if (!is_front_page()) return $content;
    // how to replace main post content with custom content ?
    global $post;
    $special_content = get_post_meta($post->ID,'page_builder',true);
    if (!empty($special_content)) { 
        // remove_all_filters('the_content');
        return $special_content;
    }
    return $content;
}

The only other thing you might want to do is run your filter first...

add_filter('the_content', 'displayCustomContent', 1);

... and then uncomment remove_all_filters('the_content'); inside the callback function. That should reduce unnecessary overhead and also help to avoid having other content filters alter your content.

5
  • thank you, that is what i want :) and if i want to do it with page type, only need check global $post, $page;
    – Chameron
    Commented May 27, 2013 at 19:05
  • i want it apply for all post/page type. Simple replace with special content if it exists on this post/page.
    – Chameron
    Commented May 27, 2013 at 19:16
  • I do not understand the question in the previous comment but you can add or remove as many conditions as you need to make it work the way you want.
    – s_ha_dum
    Commented May 27, 2013 at 19:18
  • i have updated this code, you can see and let me know the different the if (is_page() || is_single()) and if (!is_front_page()) return $content;
    – Chameron
    Commented May 27, 2013 at 19:28
  • In an earlier version of your question you mentioned that you wanted to "display this content in front page" so I use !is_front_page-- "not is front page"-- to return the default content for all pages except the front page. is_page() || is_single() does something similar but different. That will be true for either a page or a single post display. The Codex entries for those functions will give you the details.
    – s_ha_dum
    Commented May 27, 2013 at 19:52

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.